This is a commented transcript of a perl demonstration. Perl can be called either by specifying a file containing a perl program or by giving perl commands directly on the command line following the -e option: perl SCRIPTFILE perl -e COMMANDS The first version can also be used in shebang style: #!/usr/bin/perl COMMANDS #!/usr/bin/env perl COMMANDS VARIABLES Three main variable types: scalars, arrays, and hashes (associative arrays). First character of variable name determines type: $scalar, @array, %hash In ordinary variable name, $, @, or % is followed by a letter or underscore and possibly further letters, digits, and underscores. (There are lots of predefined variables that do not follow this rule, e.g., $_, $/, $., $12) SCALARS Scalar variables may contain strings, integer or floating point numbers. $xyz = 3.2; print $xyz; »»» 3.2 ««« print $xyz, "\n"; »»» 3.2 ««« Literal strings can be written with single or double quotes; variables and backslash sequences are interpolated within double quotes. print "$xyz * $xyz equals ", $xyz * $xyz, "\n"; »»» 3.2 * 3.2 equals 10.24 ««« print '$xyz * $xyz equals ', $xyz * $xyz, '\n'; »»» $xyz * $xyz equals 10.24\n ««« If a numeric term is used in a string context or vice versa, it is converted on the fly: print $xyz . " * " . $xyz . " equals " . ($xyz * $xyz) . "\n"; »»» 3.2 * 3.2 equals 10.24 ««« ARRAYS Array names start with @; individual components of arrays (and hashes) are scalars, hence their names start with $. Like in C, array indices start with 0 (at least, that's the default). @a = (11,22,33,44); print @a; »»» 11223344 ««« print $a[0]; »»» 11 ««« Array slices: @b = @a[2 .. 3]; print @b; »»» 3344 ««« @b = @a[1,3]; print @b; »»» 2244 ««« @b = @a[0,2..3]; print @b; »»» 113344 ««« Arrays can be used as stacks (both from the small and from the large side): @a = (11,22,33,44); pop(@a); push(@a,"forty-four"); print @a; »»» 112233forty-four ««« shift(@a); unshift(@a,"eleven"); print @a; »»» eleven2233forty-four ««« Arrays or lists can be assigned to lists of variables. The lengths of the lhs list and the rhs list need not agree. Note that the first array variable on the lhs gobbles everything that's left. ($b1,$b2,$b3) = @a; print "$b1 $b2 $b3"; »»» eleven 22 33 ««« ($c, @d) = @a; print $c; »»» eleven ««« print @d; »»» 2233forty-four ««« (@d, $c) = @a; print @d; »»» eleven2233forty-four ««« (@d, $c) = (@a,@a); print @d; »»» eleven2233forty-foureleven2233forty-four ««« print $c; »»» ««« Some array operations: print reverse @a; »»» forty-four3322eleven ««« print join(", ", @a); »»» eleven, 22, 33, forty-four ««« HASHES %h = ("alpha" => 1, "beta" => 2, "gamma" => 3, "delta" => 4); print $h{"gamma"}; »»» 3 ««« $h{"epsilon"} = 5; The keys and values functions yield the list of all hash keys and hash table entries (in unspecified order). @a = keys %h; print join(", ", @a); »»» gamma, epsilon, delta, alpha, beta ««« @a = values %h; print join(", ", @a); »»» 3, 5, 4, 1, 2 ««« print %h; »»» gamma3epsilon5delta4alpha1beta2 ««« @a = %h; print join(", ", @a); »»» gamma, 3, epsilon, 5, delta, 4, alpha, 1, beta, 2 ««« SCALAR CONTEXT VS. LIST CONTEXT Many operations in Perl are heavily overloaded and yield different results depending on whether they are called in a context where a scalar is expected or where a list is expected. In a list context, comma is the tupling operator: @a = (11,22,33,44); print @a; »»» 11223344 ««« In a scalar context, comma behaves like in C, yielding the last expression: $a = (11,22,33,44); print $a; »»» 44 ««« In a scalar context, an array variable evaluates to the length of the array: @a = (11,22,33,44); $b = @a; print $b; »»» 4 ««« CONTROL STRUCTURES: Almost like in C, but note that braces are not optional: if (COND) { BLOCK } elsif (COND) { BLOCK } else { BLOCK } while (COND) { BLOCK } for ($i = 0 ; $i <= 10 ; $i++) { BLOCK } The commands "next" and "last" in Perl correspond to "continue" and "break" in C. The foreach loop iterates over all elements of an array (default: assignment to $_). foreach (@array) { BLOCK } %h = ("alpha" => 1, "beta" => 2, "gamma" => 3, "delta" => 4); foreach (keys %h) { print "value of $_ is $h{$_}\n"; } »»» value of gamma is 3 value of delta is 4 value of alpha is 1 value of beta is 2 ««« foreach my $key (keys %h) { print "value of $key is $h{$key}\n"; } »»» value of gamma is 3 value of delta is 4 value of alpha is 1 value of beta is 2 ««« OPERATORS Arithmetic operators: + - * / % Numeric comparison: == != < <= > >= <=> String comparison: eq ne lt le gt ge cmp String operations: . x Bitwise operations: & | ^ Logical operations: && || ! and or xor ("and" and "or" differ from "&&" and "||" only by precedence.) print "abc" x 5; »»» abcabcabcabcabc ««« $a = "1"; $a x= 5; print $a; »»» 11111 ««« FILES AND I/O The "open" command associates a file to a filehandle. open(IN, "output.txt"); In a scalar context, yields one chunk (usually one line) from filehandle IN. $a = ; In a list context, it yields the list of all chunks (lines). @a = ; The following are equivalent: while (defined($_ = )) {...} while ($_ = ) {...} while () {...} The following construction while (<>) { ... } is a shorthand for if (! @ARGV) { unshift(@ARGV, '-'); } while ($ARGV = shift(@ARGV)) { open(ARGV, $ARGV); while () { ... } } it loops over all lines of all files specified on the command line (or all lines from stdin, if no files are specified). The -n option replaces a "while (<>)" loop around the whole program: perl -e 'while (<>) {DOSOMETHING;}' perl -ne 'DOSOMETHING;' The option -p is similar to -n, except that each line is printed: perl -pe 'DOSOMETHING;' The print commands can take a filehandle as an optional argument. Note that there is no comma after the filehandle: print OUT $a, $b, $c; printf OUT FORMATSTRING, $a, $b, $c; open(FH, " length($b) } @array); »»» beta, alpha, gamma, delta ««« REGULAR EXPRESSIONS See next section. MORE INFO can be found in the perl manual (in particular: man perlintro, man perldata, man perlop, man perlfunc, man perlsub, man perlvar). Some items not mentioned here: references, objects, classes, modules.