Perl Programming/Control flow
Control structures
editThe basic control structures do not differ greatly from those used in the C programming language or Java programming language:
Loops
editwhile ($boolean) {
# do something
}
until ($boolean) {
# do something
}
Though syntactically the same, Perl does not use break and continue to change the flow of loops. Perl provides the following commands: (with C equivalents in comments)
while ($boolean) {
# do something
if($finished) {
last; # equivalent to 'break'
}
if($done) {
next; # equivalent to 'continue'
}
# do some more
}
Note that the statements in a while (or until) loop are not executed, if the Boolean expression evaluates to false (or true, respectively) on the first pass, even when specified at the end of the code block. Therefore the following loops are functionally equivalent: (the same applies to: do {} until)
while ($boolean) {
# something
}
do {
# something
} while ($boolean);
The do {} while and the do {} until loops are technically statement modifiers and not actual control structures. The statements will be executed at least once.
for (my $i = 0; $i < 10; $i++) { # for (initialization; termination condition; incrementing expr) { … }
print "$i\n";
}
foreach my $variable (@list) {
print "$variable\n";
}
$variable is an alias to each element of the @list, starting at the first element on the first pass through the loop. The loop is exited when all the elements in the list have been exhausted. Since $variable is an alias, changing the value will change the value of the element in the list. This should generally be avoided to enhance maintainability of the code.
If $variable is omitted, the default variable $_ will be used.
foreach (@list) {
print "value: $_ \n";
}
Note that for and foreach are actually synonyms and can be used interchangeably.
Blocks may have an optional continue section, which is executed at the end of each iteration.
while ($i<4) {
$i++;
} continue {
print "$i\n";
}
next, redo, last
editWhen inside a loop, there are three keywords that manipulate how the loop is handled.
To start the next iteration, next jumps to the end of the block. If there is a continue block, that part is executed, as is the conditional to resume the loop.
To restart an iteration, redo jumps to the beginning of the block. Neither continue nor the conditional are executed.
To break out of the loop, last jumps outside the end of the block. Neither continue nor the conditional are executed.
given
editUntil version 5.10.1, Perl did not have an equivalent of the switch statement in other programming languages. Starting in that version, it became an experimental feature.
In Perl 5, it first needs to be enabled with one of the following statements:
use feature "switch";
use v5.14;
</blockquote>
<syntaxhighlight lang="perl">
given ($t)
{
when ("one") { say 'one'; }
default { say 'default'; }
}
By default, the expressions in when is matched to what is found in given. In certain exceptional cases, the value may be used directly as a boolean.
if-then statements
editif ($boolean_expression) {
# do something
}
unless ($boolean_expression) {
# do something
}
Statements with else blocks (these also work with unless instead of if)
if ($boolean) {
# do something
} else {
# do something else
}
if ($boolean) {
# do something
} elsif ($boolean) {
# do something else
}
Postfix notation
editControl statements can also be written with the conditional following the statements (called "postfix"). This syntax functions (nearly) identically to the ones given above.
statement if Boolean expression; statement unless Boolean expression; statement while Boolean expression; statement until Boolean expression; statement foreach list;
See also
edit