Category: Perl

Files and directories

Reading and writing to files The symbol table is a little esoteric, so let’s get back to practicalities. How do you mess about with files and directories in Perl? A simple example: #!/usr/bin/perl use strict; use warnings; open my $INPUT, “<“, “C:/autoexec.bat” or die “Can’t open C:/autoexec.bat for reading $!\n”; open my $OUTPUT, “>”, “C:/copied.bat” …

Continue reading

Symbol table

Symbols That’s pretty much everything for hashes, except for one topic usually missed out from introductory tutorials (possibly rightly!) This post will tell you a little about the innards of what you’ve been doing when you create variables. It’s not really necessary to know this stuff to be able to use Perl for day-to-day stuff, …

Continue reading

More hashes and sorting

Hash manipulation: each, delete, exists Before we got side-tracked with subroutines and conditionals, we covered arrays in some detail, and saw the various functions, like push and pop that you can torture them with. The functions for torturing hashes are our next port of call. Perl stores the key => value pairs of a hash …

Continue reading

Conditionals

Conditionals: if and unless We’re on the home-straight in understanding the code from the last post now: #!/usr/bin/perl use strict; use warnings; my @peas = qw/chick mushy split/; while ( my $type = pop @peas ) { print “$type peas are “, flavour( $type ), “.\n”; } sub flavour { my $query = shift @_; my …

Continue reading

Bondage, discipline and subroutines

Lexical my variables and use strict; You may have noticed a little thing I slipped in the last script: the keyword my in the chomp. my is a very important keyword, although you’ll note that it doesn’t seem to make any difference if you delete it and run the program. What my does is pin …

Continue reading

Prettier loops and nicer code

Idiomatic Perl The original code we were studying is very ugly, and Perl makes writing short, diabetogenic code easy. It also makes writing shoddy, abominable code easy too, but I wouldn’t recommend this as a course of action. A much more attractive way of writing the horrible code from the last page is: #!/usr/bin/perl print …

Continue reading

Simple loops

Loops Hopefully you’re now happy with the fundamental data types in Perl: scalars, arrays and hashes, so most of the script below shouldn’t be too mysterious: #!/usr/bin/perl print “What is your name?\n”; $name = <STDIN>; chomp $name; @beans = ( “adzuki”, “haricot”, “mung” ); print( “\@beans contains “, scalar @beans, ” members: @beans\n” ); for …

Continue reading

Hashes

Hashes Perl actually has two sorts of array. Simple arrays like those we’ve already discussed are simply called arrays. The second sort of array is called an associative array, or hash. They’re very similar to dictionaries, if you’re a Python programmer. Hashes are created with a % for %we_ran_out_of_sensible_symbols, and contain pairs of data (ah, maybe …

Continue reading

Arrays and slices

Types You should now be able to create very simple scripts that can take input from the keyboard, assign it to scalar variables, and echo it back to the screen with print. Time for some technicalities. Perl is often (and wrongly!) termed a weakly typed language. For those of you coming from bondage and discipline …

Continue reading

Input and output

Input The “not a hello world script” script is a little dull. Let’s try something a more useful: #!/usr/bin/perl print “What is your name?\n”; $name = <STDIN>; chomp( $name ); print “$name likes beans.\n”; Don’t forget the semicolons at the end of each statement. The first line of this program should be obvious now, but …

Continue reading

Load more