- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

2.7 Conditional and looping constructs

Perl has most of the usual conditional and looping constructs. As of Perl 5.10, it even has a case/switch statement (spelled given/when). See 4.11 for more details.

The conditions can be any Perl expression. See the list of operators in the next section for information on comparison and boolean logic operators, which are commonly used in conditional statements.

if ( condition ) {
} elsif ( other condition ) {
} else {
There's also a negated version of it:
unless ( condition ) {
This is provided as a more readable version of if (!condition). Note that the braces are required in Perl, even if you've only got one line in the block. However, there is a clever way of making your one-line conditional blocks more English like:
# the traditional way
if ($zippy) {
    print "Yow!";
# the Perlish post-condition way
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;
while ( condition ) {
There's also a negated version, for the same reason we have unless:
until ( condition ) {
You can also use while in a post-condition:
print "LA LA LA\n" while 1;          # loops forever
Exactly like C:
for ($i = 0; $i <= $max; $i++) {
The C style for loop is rarely needed in Perl since Perl provides the more friendly list scanning foreach loop.
foreach (@array) {
    print "This element is $_\n";
print $list[$_] foreach 0 .. $max;
# you don't have to use the default $_ either...
foreach my $key (keys %hash) {
    print "The value of $key is $hash{$key}\n";

For more detail on looping constructs (and some that weren't mentioned in this overview) see 4.

ISBN 9781906966027Perl Language Reference ManualSee the print edition