2. Control Structures

Perl is an iterative language in which control flows from the first statement in the program to the last statement unless something interrupts. Some of the things that can interrupt this linear flow are conditional branches and loop structures. Perl offers approximately a dozen such constructs, which are described below. The basic form will be shown for each followed by a partial example.


statement block

Statement blocks provide a mechanism for grouping statements that are to be executed as a result some expression being evaluated. They are used in all of the control structures discussed below. Statement blocks are designated by pairs of curly braces.

Form: BLOCK

Example:

{ stmt_1; stmt_2; stmt_3; }
if statement

Form: if (EXPR) BLOCK

Example:

if (expression) { true_stmt_1; true_stmt_2; true_stmt_3; }
if/else statement

Form: if (EXPR) BLOCK else BLOCK

Example:

if (expression) { true_stmt_1; true_stmt_2; true_stmt_3; } else { false_stmt_1; false_stmt_2; false_stmt_3; }
if/elseif/else statement

Form: if (EXPR) BLOCK elseif (EXPR) BLOCK . . . else BLOCK

Example:

if (expression_A) { A_true_stmt_1; A_true_stmt_2; A_true_stmt_3; } elseif (expression_B) { B_true_stmt_1; B_true_stmt_2; B_true_stmt_3; } else { false_stmt_1; false_stmt_2; false_stmt_3; }
while statement

Form: LABEL: while (EXPR) BLOCK

The LABEL in this and the following control structures is optional. In addition to description, it also provides function in the quasi-goto statements: last, next, and redo. Perl conventional practice calls for labels to be expressed in uppercase to avoid confusion with variables or key words.

Example:

ALABEL: while (expression) { stmt_1; stmt_2; stmt_3; }
until statement

Form: LABEL: until (EXPR) BLOCK

Example:

ALABEL: until (expression) { # while not stmt_1; stmt_2; stmt_3; }
for statement

Form: LABEL: for (EXPR; EXPR; EXPR) BLOCK

Example:

ALABEL: for (initial exp; test exp; increment exp) { # e.g., ($i=1; $i<5; $i++) stmt_1; stmt_2; stmt_3; }
foreach statement

Form: LABEL: foreach VAR (EXPR) BLOCK

Example:

ALABEL: foreach $i (@aList) { stmt_1; stmt_2; stmt_3; }
last operator

The last operator, as well as the next and redo operators that follow, apply only to loop control structures. They cause execution to jump from where they occur to some other position, defined with respect to the block structure of the encompassing control structure. Thus, they function as limited forms of goto statements.

Last causes control to jump from where it occurs to the first statement following the enclosing block.

Example:

ALABEL: while (expression) { stmt_1; stmt_2; last; stmt_3; } # last jumps to here

If last occurs within nested control structures, the jump can be made to the end of an outer loop by adding a label to that loop and specifying the label in the last statement.

Example:

ALABEL: while (expression) { stmt_1; stmt_2; BLABEL: while (expression) { stmt_a; stmt_b; last ALABEL; stmt_c; } stmt_3; } # last jumps to here
next operator

The next operator is similar to last except that execution jumps to the end of the block, but remains inside the block, rather than exiting the block. Thus, iteration continues normally.

Example:

ALABEL: while (expression) { stmt_1; stmt_2; next; stmt_3; # next jumps to here }
As with last, next can be used with a label to jump to an outer designated loop.

redo operator

The redo operator is similar to next except that execution jumps to the top of the block without re-evaluating the control expression.

Example:

ALABEL: while (expression) { # redo jumps to here stmt_1; stmt_2; redo; stmt_3; }
As with last, next can be used with a label to jump to an outer designated loop.