Perl control structures
The basic control structures in Perl are similar to those used in the C or Java programming languages, but they have been extended in several ways.
Loops
label while ( expr ) block label while ( expr ) block continue block label for ( expr1 ; expr2 ; expr3 ) block label foreach var ( list ) block label foreach var ( list ) block continue block
where block is a sequence of one of more Perl statements surrounded by braces:
{ statement(s) }
The label, which is terminated by a colon, is optional, but, if present, can be used by loop control statements.
- The
nextstatement moves to the next iteration of the loop identified by the label. - The
laststatement immediately terminates execution of the loop identified by the label. - The
redostatement restarts the current iteration of the loop identified by the label.
Within nested loops, the use of the label with next, last and redo enables control to move from an inner loop to an outer one, or out of the outer loop altogether.
In the for statement, the semantics are similar to C. The first expression is evaluated prior to the first loop iteration; the second expression is evaluated prior to each iteration and the loop is terminated if it evaluates to boolean false; and the third expression is evaluated after each iteration, prior to deciding whether to perform the next.
The use of a continue block after a while or foreach statement allows code to be executed after each iteration—even if the current iteration has been cut short by a next statement.
In foreach, the var is a scalar variable. It is optional, and, if omitted, the default loop iterator variable $_ can be used instead. The keywords for and foreach are synonyms and are always interchangeable.
In addition, any simple expression (that is, any non-block) can be executed repeatedly by following it by one of the qualifiers:
while expr until expr
which have the expected meaning.
When combined with the do construct, this provides a form of looping:
do block while (expr); do block until (expr);
The test is performed after each iteration, so that execution of the block is guaranteed to take place at least once.
Due to a linguistic foible of the Perl language, these are not regarded as true loops; the next, last and redo statements cannot be used to control a do block.
By itself, do executes a string of statements once, and evaluates to the value of the last statement:
do block;
Conditional statements
if ( expr ) block
unless ( expr ) block
if ( expr ) block else block
if ( expr ) block elsif block else block
where block is a sequence of one of more Perl statements surrounded by braces:
{ statement(s) }
The expression is evaluated in a boolean sense. If it is numeric, any non-zero value is true. If it is a string, any string of non-zero length except "0" is true. "" and "0" are false. "0.0", "00", "-0", and "0 but true" are all true, even though they are all equivalent to zero when used for arithmetic; values like these are sometimes used when a successful operation needs to return 0.
An empty array or hash is evaluated in a scalar context, yielding false:
my @a=(); print "True" if @a;
Nothing is printed in this example.
Statement modifiers
For simple statements, while, until, if, unless and foreach can also be used as statement modifiers:
statement while Boolean expression; statement until Boolean expression; statement if Boolean expression; statement unless Boolean expression; statement foreach list;
The above modifiers cannot be nested, so this would be incorrect:
statement if expression for list; #INCORRECT
and should be written as one of:
expression and statement for list;
for ( list ) { statement if expression } #Preferred form
As noted earlier, the modifiers while and until can be combined with do to create a multiple-statement looping structure.