ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы.



Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: 4.2 Data Types and VariablesChapter 4
The Perl Language
Next: 4.4 Special Variables
 

4.3 Statements

A simple statement is an expression evaluated for its side effects. Every simple statement must end in a semicolon, unless it is the final statement in a block.

A sequence of statements that defines a scope is called a block. Generally, a block is delimited by braces, or { }. Compound statements are built out of expressions and blocks. A conditional expression is evaluated to determine whether a statement block will be executed. Compound statements are defined in terms of blocks, not statements, which means that braces are required.

Any block can be given a label. Labels are identifiers that follow the variable-naming rules (i.e., they begin with a letter or underscore, and can contain alphanumerics and underscores). They are placed just before the block and are followed by a colon, like SOMELABEL here:

SOMELABEL: {
  ...statements...
  }
By convention, labels are all uppercase, so as not to conflict with reserved words. Labels are used with the loop-control commands next, last, and redo to alter the flow of execution in your programs.

4.3.1 Conditionals and Loops

The if and unless statements execute blocks of code depending on whether a condition is met. These statements take the following forms:

if (expression) {block} else {block}

unless (expression) {block} else {block}

if (expression1) {block}
elsif (expression2) {block}
  ...
elsif (lastexpression) {block}
else {block}

4.3.1.1 while loops

The while statement repeatedly executes a block as long as its conditional expression is true. For example:

while (<INFILE>) {
    print OUTFILE, "$_\n";
}
This loop reads each line from the file opened with the filehandle INFILE and prints them to the OUTFILE filehandle. The loop will cease when it encounters an end-of-file.

If the word while is replaced by the word until, the sense of the test is reversed. The conditional is still tested before the first iteration, though.

The while statement has an optional extra block on the end called a continue block. This block is executed before every successive iteration of the loop, even if the main while block is exited early by the loop control command next. However, the continue block is not executed if the main block is exited by a last statement. The continue block is always executed before the conditional is evaluated again.

4.3.1.2 for loops

The for loop has three semicolon-separated expressions within its parentheses. These three expressions function respectively as the initialization, the condition, and the re-initialization expressions of the loop. The for loop can be defined in terms of the corresponding while loop:

for ($i = 1; $i < 10; $i++) {
    ...
}
is the same as:
$i = 1;
while ($i < 10) {
    ...
}
continue {
    $i++;
}

4.3.1.3 foreach loops

The foreach loop iterates over a list value and sets the control variable (var) to be each element of the list in turn:

foreach var (list) {
    ...
}
Like the while statement, the foreach statement can also take a continue block.

4.3.1.4 Modifiers

Any simple statement may be followed by a single modifier that gives the statement a conditional or looping mechanism. This syntax provides a simpler and often more elegant method than using the corresponding compound statements. These modifiers are:

statement if EXPR;
statement unless EXPR;
statement while EXPR;
statement until EXPR;
For example:
$i = $num if ($num < 50); # $i will be less than 50
$j = $cnt unless ($cnt < 100); # $j will equal 100 or greater
$lines++ while <FILE>;
print "$_\n" until /The end/;
The conditional is evaluated first with the while and until modifiers except when applied to a do {} statement, in which case the block executes once before the conditional is evaluated. For example:
do {
    $line = <STDIN>;
    ...
} until $line eq ".\n";
For more information on do, see Chapter 5, Function Reference.

4.3.1.5 Loop control

You can put a label on a loop to give it a name. The loop's label identifies the loop for the loop-control commands next, last, and redo.

LINE: while (<SCRIPT>) {
    print;
    next LINE if /^#/;      # discard comments
    }
The syntax for the loop-control commands is:
last label
next label
redo label
If the label is omitted, the loop-control command refers to the innermost enclosing loop.

The last command is like the break statement in C (as used in loops); it immediately exits the loop in question. The continue block, if any, is not executed.

The next command is like the continue statement in C; it skips the rest of the current iteration and starts the next iteration of the loop. If there is a continue block on the loop, it is always executed just before the conditional is about to be evaluated again.

The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed.

4.3.1.6 goto

Perl supports a goto command. There are three forms: goto label, goto expr, and goto &name.

The goto label form finds the statement labeled with label and resumes execution there. It may not be used to go inside any construct that requires initialization, such as a subroutine or a foreach loop.

The goto expr form expects the expression to return a label name.

The goto &name form substitutes a call to the named subroutine for the currently running subroutine.


Previous: 4.2 Data Types and VariablesPerl in a NutshellNext: 4.4 Special Variables
4.2 Data Types and VariablesBook Index4.4 Special Variables