A statement block in Perl is enclosed by curly parentheses:
{ statement1; statement2; }
The semicolon on the last statement in a block being optional.
So an if statements may be written as:
if ( $hour <= 11 ) { $eat = "breakfast"; } elsif ($hour >= 19 ) { $eat = "supper"; } else { $eat = "lunch"; } if ( $hungry ) { print "Time for $eat\n" }
Any conditional expression must, of course, evaluate to true or false. Perl evaluates the expression to be a string in scalar context. An empty string or a string containing the single character "0" (zero) is false, everything else being true:
0 # converts to "0" so false 1 # converts to "1" so true "" # an empty string so false "1" # not "" or "0" so true "00" # not "" or "0" so true - beware! "0.00" # not "" or "0" so true - beware! 0.00 # converts to "0" so false 0.01 # converts to "0.01" so true 1-1 # calculates 0 so false 1.0-1.0 # calculates 0.0 so false
while ( condition ) { statement1; statement2; statement3; } |
do { statement1; statement2; statement3; } while condition; |
until ( condition ) { statement1; statement2; statement3; } |
do { statement1; statement2; statement3; } until condition; |
The traditional C/Java style for statement (a pre-tested while loop):
for ( $i = 1; $i <= 10; $i++ ) { print "$i "; }
A shell like foreach loop that traverses a list (array):
@foo = (1, 2, 3, 4, 5); foreach $i ( reverse @foo ) { print $i; }
What happens when you run the two examples above?
In scalar context <STDIN> is read up to and including the newline character.
In an array context, strings are read into the array up to the end of file character, cntrlD (cntrlZ).
Consider this perl script num.pl :
#!/usr/local/bin/perl $i = 0; @foo = <STDIN>; foreach (@foo) { $i++; printf "%6s $_", $i; }
This example illustrates some more Perl shorthand. Omitting the scalar from the foreach statement means that Perl uses the default variable $_.
To line up the print a C/Java style formatted print statement printf is used. The following illustrates how this script could be used to add line numbers into a text file.
bukowski -->cat num.pl | num.pl > num.txt
This will catenate the file num.pl and pipe the output into num.pl which will add line numbers and store the result into a file called num.txt. (Isn't Perl + Unix wonderful?)
A perl function or subroutine or sub has the following syntax:
sub subname { statement1; statement2; }
Subroutines may be defined anywhere in your code or loaded in from other files (modules, packages) via the use keyword. All function parameters (arguments) are passed as a single flat list of scalars and multiple return values are likewise returned to the caller as one flat list of scalars.
All arguments arrive as the list @_. So the first argument arrives as $_[0], the second as $_[1] and so on. @_ is private to the subroutine. If @_ has a value before the subroutine is called, it is saved before the subroutine is invoked and restored on return from the subroutine. As @_ is an array it can by maipulated using any of Perl's array operations.
Parameters are passed by value. Parameters can be passed by reference by using the \ reference operator (a bit like C's & address-of operator) but as variables default to global (within a package space) this is rarely used.
Variables can be made local to a subroutine using the keyword my. Any variable having the same name is preserved on calling the subroutine and restored on leaving. Variables can also be made local to a code block by using the keyword local. Local variables are visible to subroutines called from within the block in which they are declared. The Perl pragma use strict forces declaration of all variables before they can be used.
The default return value from a subroutine (or from any other block of code) is the value of the last expression evaluated. It is safer and better coding practice to explicitly return values.
#!/usr/local/bin/perl -w $sum = shift @ARGV; for ($i = 0; $i <= $sum; $i++) { $bar[$i] = $i } print "sum from 1 to $sum = ", add(@bar), "\n"; sub add { my $sum = 0; foreach (@_) { $sum += $_; } return $sum; }
This example shows:
parameters passed into a subroutine using @_
use of the default variable $_ in a foreach loop
use of a local value in a subroutine
returning a value from a subroutine
handling of program arguments using shift @ARGV - could be shorthanded to shift
Note: Although Perl is very accommodating it will issue a warning if the for loop were to run from any positive value other than zero (and an error for a negative value).
Perl packages are a mechanism to protect different sections of code from accidentally tampering with each other's variables. (A stand-alone Perl program is actually a default package main and scalars, for example, have the name $main::foo.)
A Perl module is a re-usable package defined in a file that has the same name as the package with a .pm file extension. Packages and modules are included into programs with the use keyword.
A Module begins with a package declaration that matches the filename so the following example would be stored in a file MyTime.pm
package MyTime; # ISO format date and time strings in localtime and GMT use Exporter; @ISA = qw(Exporter); @EXPORT = qw(dstamp tstamp dtstamp); local ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst); local $gmt = 0;
subroutines go in here
return 1;
The Exporter package is used (with the @ISA list) in the module to export symbols to other packages that want to use the package. The @EXPORT list contains the names of the symbols (functions and data) that are to be exported from the module.
Variables that have package scope are declared to be local. Such variables could be exprted explicitly by including them in the exported symbol list. Although the variables in this example are not exported they can be accessed outside the package by using their fully qualified name, e.g. $year would be $MyTime::year.
Now this Module can be included into other Perl code with the statement:
use MyTime;
or more explicitly:
use lib "/home/mk05/perl/lib"; use MyTime qw(dstamp, tstamp, dtstamp);
Here dstamp, tstamp and dtstamp are the functions that are to be used. Mechanisms exist to group functions into commonly used sets for easy importing.
A Perl program in cgi-bin can make use of Perl modules in parts of the filesystem that may be outside the public tree (usually public_html). This provides some security against unwanted disclosure of precious intellectual property. The array @INC contains the search (library) paths for including Perl packages and modules. In this example use lib is used to add a directory to the library path.