Perl control structures
The basic control structures of Perl are similar to those used in C and Java, but they have been extended in several ways. LoopsIn the following, label is an optional identifier terminated by a colon, and block is a sequence of one of more Perl statements surrounded by braces. All looping constructs except for the C-style label for ( expr1 ; expr2 ; expr3 ) block This is the so-called C-style label for var ( list ) block label for var ( list ) block continue block label foreach var ( list ) block label foreach var ( list ) block continue block In label while ( expr ) block label while ( expr ) block continue block label until ( expr ) block label until ( expr ) block continue block The label block label block continue block The label block construct is a bit of an oddity: Perl treats a bare block – with or without a label – as a loop that is executed once. This means that the loop control keywords can be used to restart the block or to exit it prematurely; a bare block can also have a Loop control keywordsPerl provides three loop control keywords that all accept an optional loop label as an argument. If no label is specified, the keywords act on the innermost loop. Within nested loops, the use of labels enables control to move from an inner loop to an outer one, or out of the outer loop altogether. The loop control keywords are treated as expressions in Perl, not as statements like in C or Java.
Conditional statementsif ( expr ) block if ( expr ) block else block if ( expr ) block elsif ( expr ) block ... else block unless ( expr ) block unless ( expr ) block else block unless ( expr ) block elsif ( expr ) block ... else block where block is a sequence of one of more Perl statements surrounded by braces. The controlling expressions are evaluated in a Boolean context: The numeric value 0, the strings "" and "0", and the undefined value Evaluating an empty array in scalar context yields my @a=(); unless (@a) { print "a is empty" } Statement modifiersPerl also provides variants of the loop and conditional constructs that work on a simple statement (an expression evaluated for its side-effects) instead of a block: statement if expr; statement unless expr; statement while expr; statement until expr; statement for list; statement foreach list; The do block while expr; do block until expr; In these constructs, the condition is tested after the block is executed, so the block always executes at least once. These modifiers cannot be nested, so the following is illegal statement if expression for list; #ERROR and should be written as one of: ( expression ) and ( statement ) for list; for ( list ) { statement if expression } do { statement if expression } foreach list; gotoThere are two forms of goto in Perl: goto label and goto &subroutine The first form is generally deprecated, and is only used in rare situations. For example, when attempting to preserve error status in open(A, "<", $filea) or goto fail;
open(B ,">", $fileb) or goto fail;
print B <A> or goto fail;
close A or goto fail;
close B or goto fail;
return 1;
fail: $reason = "In copy: $?"; return 0;
The second form is called a tail call, and is used to enhance the performance of certain kinds of constructs where Perl's default stack management would perform non-optimally. For example: sub factorial {
my $n = shift;
my $total = shift(@_) || 1;
if ($n > 1) {
@_ = ($n-1,$total*$n);
goto &factorial;
} else {
return $total;
}
}
This form is also used to create aliases for subroutines with minimal overhead. This can help reduce "Out of Memory" errors (or high memory usage in general) found often in repeating the same subroutine. External linksPerl Programming/Flow control at Wikibooks Information related to Perl control structures |