ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
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:
By convention, labels are all uppercase, so as not to conflict with reserved words. Labels are used with the loop-control commandsSOMELABEL: { ...statements... }
next
, last
, and redo
to alter
the flow of execution
in your programs. 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}
The while
statement repeatedly executes a block as long as
its conditional expression is true.
For example:
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.while (<INFILE>) { print OUTFILE, "$_\n"; }
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.
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:
is the same as:for ($i = 1; $i < 10; $i++) { ... }
$i = 1; while ($i < 10) { ... } continue { $i++; }
The foreach
loop iterates over a list value and sets the
control variable (var
) to be each element of the list in turn:
Like theforeach var (list) { ... }
while
statement, the foreach
statement can also
take a continue
block.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:
For example:statement if EXPR; statement unless EXPR; statement while EXPR; statement until EXPR;
The conditional is evaluated first with the$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/;
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:
For more information ondo { $line = <STDIN>; ... } until $line eq ".\n";
do
, see Chapter 5, Function Reference.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
.
The syntax for the loop-control commands is:LINE: while (<SCRIPT>) { print; next LINE if /^#/; # discard comments }
If the label is omitted, the loop-control command refers to the innermost enclosing loop.last label next label redo label
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.
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.