Perl control structures

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.

Loops

In 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 for-loop can have a continue block that is executed after each iteration of the loop body, before the loop condition is evaluated again.

"label" for ( "expr1" ; "expr2" ; "expr3" ) "block"This is the so-called C-style for loop. 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 false. The third expression is evaluated after each iteration, prior to deciding whether to perform the next. This for loop is the only looping construct that can not have a continue block, but "expr3" is functionally equivalent.

"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 foreach, "var" is a scalar variable that defaults to $_ if omitted. For each element of "list", "var" is aliased to the element, and the loop body is executed once. The keywords for and foreach are synonyms and are always interchangeable.

"label" while ( "expr" ) "block" "label" while ( "expr" ) "block" continue "block" "label" until ( "expr" ) "block" "label" until ( "expr" ) "block" continue "block"The while loop repeatedly executes the loop body as long as the controlling expression is true. The condition is evaluated before the loop body. until is similar, but executes the loop body as long as the condition is false.

"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 continue block.

Loop control keywords

Perl 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. It should be noted that the loop control keywords are treated as expressions in Perl, not as statements like in C or Java.

* The next keyword jumps directly to the end of the current iteration of the loop. This usually causes the next iteration of the loop to be started, but the continue block and loop condition are evaluated first.
* The last keyword immediately terminates execution of the loop identified by the label. The continue block is not executed.
* The redo keyword restarts the current iteration of the loop identified by the label. Neither the continue block nor the loop condition is evaluated.

Conditional statements

if ( "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 undef are false, all other values are true. This means that the strings "0.0", "00", "-0", and "0 but true" are all true, even though their value would be converted to 0 in a numeric context; values like these are sometimes used when a successful operation needs to return 0.

Evaluating an empty array or hash in scalar context yields undef, which is false. Therefore, the following example prints "a is empty": my @a=(); unless (@a) { print "a is empty" }

tatement modifiers

Perl 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" foreach "list";

The while and until modifiers test the controlling expression before executing the statement, just like their loop counterparts. However, they are not considered actual loops, so the loop control keywords next, last and redo cannot be used with them. They have special semantics when combined with the do keyword:

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";

goto

There are two forms of goto in Perl: goto labeland goto &subroutineThe first form is generally deprecated, and is only used in rare situations. For example, when attempting to preserve error status in $?, some modules will use goto like so: open(A,"<",$filea) or goto fail; open(B,">",$fileb) or goto fail; print B 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.

See also


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Perl — Desarrollador(es) Larry Wall www.perl.org Información general Paradigma multiparadigma, funcional, im …   Wikipedia Español

  • Control flow — Not to be confused with Flow control. In computer science, control flow (or alternatively, flow of control) refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are… …   Wikipedia

  • Perl — This article is about the programming language. For other uses, see Perl (disambiguation). Perl Paradig …   Wikipedia

  • Perl 6 — Saltar a navegación, búsqueda Perl 6 Paradigma: multiparadigma Apareció en: 2000 Diseñado por: Larry Wall y la comunidad Perl Tipo de dato: Dinámico y estático Implementaciones …   Wikipedia Español

  • Perl 6 — Infobox programming language name = Perl paradigm = Multi paradigm year = 2000 designer = Larry Wall latest release version = pre release latest release date = typing = dynamic, static influenced by = Perl 5, Haskell, Smalltalk influenced =… …   Wikipedia

  • PL/Perl — (Procedural Language/Perl) is a procedural language supported by the PostgreSQL RDBMS.PL/Perl, as a true programming language, allows much more control than basic SQL, including the ability to use loops, advanced control structures, and quite… …   Wikipedia

  • Comparison of revision control software — The following is a comparison of revision control software. The following tables includes general and technical information for notable revision control and software configuration management (SCM) software. This is an incomplete list, which may… …   Wikipedia

  • Common Lisp — Paradigm(s) Multi paradigm: procedural, functional, object oriented, meta, reflective, generic Appeared in 1984, 1994 for ANSI Common Lisp Developer ANSI X3J13 committee Typing discipline …   Wikipedia

  • Programming language — lists Alphabetical Categorical Chronological Generational A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that… …   Wikipedia

  • goto — This article is about the programming statement. For other uses, see Goto (disambiguation). goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one way transfer of… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”