The center of any imperative programming language is control structures. Although Perl is not purely an imperative programming language, it has ancestors that are very much imperative in nature, and thus Perl has inherited those same control structures. It also has added a few of its own.
As you begin to learn about Perl's control structures, realize that a good number of them are syntactic sugar. You can survive using only a subset of all the control structures that are available in Perl. You should use those with which you are comfortable. Obey the "hubris" of Perl, and write code that is readable. But, beyond that, do not use any control structures that you do not think you need.
The first tool that you need to begin to use control structures is the
ability to write code "blocks". A block of code could be any of the code
examples that we have seen thus far. The only difference is, to make them a
block, we would surround them with {}
.
use strict; { my $var; Statement; Statement; Statement; }
Anything that looks like that is a block. Blocks are very simple, and are much like code blocks in languages like C, C++, and Java. However, in Perl, code blocks are decoupled from any particular control structure. The above code example is a valid piece of Perl code that can appear just about anywhere in a Perl program. Of course, it is only particularly useful for those functions and structures that use blocks.
Note that any variable declared in the block (in the example,
$var
) lives only until the end of that block. With variables
declared my
, normal lexical scoping that you are familiar with in
C, C++, or Java applies.
We have mentioned truth and "true and false" a few times now; however, we have yet to give a clear definition of what truth values are in Perl.
Every expression in Perl has a truth value. Usually, we ignore the truth value of the expressions we use. In fact, we have been ignoring them so far! However, now that we are going to begin studying various control structures that rely on the truth value of a given expression, we should look at true and false values in Perl a bit more closely.
The basic rule that most Perl programmers remember is that 0
, the
empty string and undef
are false, and everything else is true.
However, it turns out that this rule is not actually completely
accurate.
The actual rule is as follows:
Everything in Perl is true, except:
""
(the empty string) and "0"
(the
string containing only the character, 0), or any string expression that
evaluates to either ""
(the empty string) or "0"
.
0
.
undef
).
If that rule is not completely clear, the following table gives some example Perl expressions and states whether they are true or not:
Expression | String/Number? | Boolean value |
0 | number | false |
0.0 | number | false |
0.0000 | number | false |
"" | string | false |
"0" | string | false |
"0.0" | string | true |
undef | N/A | false |
42 - (6 * 7) | number | false |
"0.0" + 0.0 | number | false |
"foo" | string | true |
There are two expressions above that easily confuse new Perl
programmers. First of all, the expression "0.0"
is true. This
is true because it is a string that is not "0"
. The only string
that is not empty that can be false is "0"
. Thus, "0.0"
must be true.
Next, consider "0.0" + 0.0
. After what was just stated, one
might assume that this expression is true. However, this expression is
false. It is false because +
is a numeric operator,
and as such, "0.0"
must be turned into its numeric equivalent.
Since the numeric equivalent to "0.0"
is 0.0
, we get the
expression 0.0 + 0.0
, which evaluates to 0.0
, which is the
same as 0
, which is false.
Finally, it should be noted that all references are true. The topic of Perl references is beyond the scope of this book. However, if we did not mention it, we would not be giving you the whole truth story.
The if
and unless
structures are the simplest control
structures. You are no doubt comfortable with if
statements from
C, C++, or Java. Perl's if
statements work very much the same.
use strict; if (expression) { Expression_True_Statement; Expression_True_Statement; Expression_True_Statement; } elsif (another_expression) { Expression_Elseif_Statement; Expression_Elseif_Statement; Expression_Elseif_Statement; } else { Else_Statement; Else_Statement; Else_Statement; }
There are a few things to note here. The elsif
and the else
statements are both optional when using an if
. It should also be
noted that after each if (expression)
or elsif (expression)
,
a code block is required. These means that the {}
's are
mandatory in all cases, even if you have only one statement inside.
The unless
statement works just like an if
statement.
However, you replace if
with unless
, and the code block is
executed only if the expression is false rather than true.
Thus unless (expression) { }
is functionally equivalent to
if (! expression) { }
.
The while
structure is equivalent to the while
structures
in Java, C, or C++. The code executes while the expression remains
true.
use strict; while (expression) { While_Statement; While_Statement; While_Statement; }
The until (expression)
structure is functionally equivalent
while (! expression)
.
The do/while
structure works similar to the while
structure,
except that the code is executed at least once before the condition is
checked.
use strict; do { DoWhile_Statement; DoWhile_Statement; DoWhile_Statement; } while (expression);
Again, using until (expression)
is the same as using
while (! expression)
.
The for
structure works similarly to the for
structure
found in C, C++ or Java. It is really syntactic sugar for the
while
statement.
Thus:
use strict; for(Initial_Statement; expression; Increment_Statement) { For_Statement; For_Statement; For_Statement; }
is equivalent to:
use strict; Initial_Statement; while (expression) { For_Statement; For_Statement; For_Statement; Increment_Statement; }
The foreach
control structure is the most interesting in this
chapter. It is specifically designed for processing of Perl's native data
types.
The foreach
structure takes a scalar, a list and a block, and
executes the block of code, setting the scalar to each value in the list,
one at a time. Consider an example:
use strict; my @collection = qw/hat shoes shirts shorts/; foreach my $item (@collection) { print "$item\n"; }
This will print out each item in collection on a line by itself. Note that
you are permitted to declare the scalar variable right with the
foreach
. When you do this, the variable lives only as long as the
foreach
does.
You will find foreach
to be one of the most useful looping structures
in Perl. Any time you need to do something to each element in the list,
chances are, using a foreach
is the best choice.
Go to the first, previous, next, last section, table of contents.