Control
Structures
Statement
Blocks
A
statement block is simply a sequence of statements enclose in curly
braces:
{
first_statement;
second_statement;
last_statement
}
Conditional
Structures (If/elsif/else)
The
basic construction to execute blocks of statements is the if
statement. The if
statement permits execution of the associated
statement block if the test expression evaluates as true. It is
important to note that unlike many compiled languages, it is
necessary to enclose the statement block in curly braces, even if
only one statement is to be executed.
The
general form of an if/then/else type of control statement is as
follows:
if
(expression_one) {
true_one_statement;
}
elsif (expression_two) {
true_two_statement;
}
else {
all_false_statement;
}
For
convenience, Perl also offers a construct to test if an expression is
false:
unless
(expression) {
false_statement;
}
else {
true_statement;
}
Note
that the order of the conditional can be inverted as well:
statement
if (expression);
statement
unless (expression);
The
“ternary” operator is another nifty one to keep in your bag of
tricks:
$var
= (expression) ? true_value : false_value;
It
is equivalent to:
if
(expression) {
$var
= true_value;
}
else {
$var
= false_value;
}
Loops
Perl
provides several different means of repetitively executing blocks of
statements.
While
The
basic while loop tests
an expression before executing a statement block
while
(expression) {
statements;
}
Until
The
until loop tests an
expression at the end of a statement block; statements will be
executed until the expression evaluates as true.
until
(expression) {
statements;
}
Do
while
A
statement block is executed at least once, and then repeatedly until
the test expression is false.
do
{
statements;
}
while (expression);
Do
until
A
statement block is executed at least once, and then repeatedly until
the test expression is true.
do
{
statements;
}
until (expression);
For
The
for loop has three
semicolon-separated expressions within its arentheses. These
expressions function respectively for the initialization, the
condition, and re-initialization expressions of the loop. The for
loop
for
(initial_exp; test_exp; reinit_exp) {
statements;
}
This
structure is typically used to iterate over a range of values. The
loop runs until the
test_exp
is false.
for
($i; $i<10;$i++) {
print
$i;
}
Foreach
The
foreach statement is
much like the for statement
except it loops over the elements of a list:
foreach
$i (@some_list) {
statements;
}
If
the scalar loop variable is omitted, $_ is
used.
Labels
Any
statement block can be given a label. Labels are identifiers that
follow variable naming rules. They are placed immediately before a
statement block and end with a colon:
SOMELABEL:
{
statements;
}
You
can short-circuit loop execution with the directives next
and last:
·
next skips the
remaining statements in the loop and proceeds to the next iteration
(if any)
·
last immediately
exits the loop in question
·
redo jumps to the
beginning of the block (restarting current iteration)
Next
and last can be used in conjunction with a label to specify a loop by
name. If the label is omitted, the presumption is that next/last
refers to the innermost enclosing loop. Usually deprecated in most
languages, the goto expression is nevertheless supported by Perl. It
is usually used in connection with a label
goto
LABEL;
to
jump to a particular point of execution.
No comments :
Post a Comment