In Perl, like in C++, all simple statements must end with a semicolon. A compound
statement is a sequence of statements and is commonly referred to as a
block. A block has its own scope, and is
usually surrounded by curly {} braces. Perl uses
control structures such as if ... else and
while in a way that is very similar to other languages
like C, C++ and Java. However, it gives you more flexibility with their syntax that these
other languages.
Conditionals
If statements
An if statement in Perl looks almost identical to the
if statements you are already familiar with. It has the
general form:
if (condition) block { elsif
(condition) block } [ else
block ]
where { x } means that
x can occur zero or more times, and
[ x ] means that
x can occur zero or one time. Note that, contrarily
to C, C++ and Java, the block must be enclosed in curly braces, even if it is a single
statement. In other words,
if ($a > 5) print "TRUE\n";
is illegal in Perl, and instead we need to use
if ($a > 5) { print "TRUE\n"; }
Unless statements
There is also unless statement that works in exactly the
same way as the if statement, except that the first
condition is negated. That is, the statement:
if ($a > 5)
{
print "TRUE\n";
}
elsif ($a == 3)
{
print "MAYBE\n";
}
else
{
print "FALSE\n";
}
is the same as the statement:
unless ($a <= 5)
{
print "TRUE\n";
}
elsif ($a == 3)
{
print "MAYBE\n";
}
else
{
print "FALSE\n";
}
Note that there is no elsunless keyword.
Switch statements
Unlike the if and unless
statements, there is no official switch statement.
Fortunately, due to the flexibility of Perl, there are many ways to create a
switch statement. The easiest, but perhaps not the most
attractive, way to mimic the behaviour of a switch
statement is to use a series of if-elsif statements nested
inside of a for loop. Another alternative, which resembles
C/C++ switch statements more closely, is to use a label, a block, some
if statements, and the last
keyword. The label, which is SWITCH in the
example below, is simply used to identify the block. This facilitates the use of control
statements such as last, which we use here,
next, and redo. The
last keyword is like the C/C++
break keyword. It is used to immediately terminate
execution in the labeled block, or if no label is given, the innermost block.
Here is a sample program:
@factor = ("quadruple", "halve", "double", "triple");
for ($i = 0; $i < @factor.""; $i++)
{
$amount = 1000;
SWITCH: {
if ($factor[$i] eq "double")
{
$amount *= 2;
last SWITCH;
}
if ($factor[$i] eq "triple")
{
$amount *= 3;
last SWITCH;
}
if ($factor[$i] eq "quadruple")
{
$amount *= 4;
last SWITCH;
}
print "I do not know how to $factor[$i].\n";
$amount = 0;
}
print "You wanted to $factor[$i] the amount.\n";
print "The new amount is $amount.\n\n";
}
The output of the program is:
You wanted to quadruple the amount.
The new amount is 4000.
I do not know how to halve.
You wanted to halve the amount.
The new amount is 0.
You wanted to double the amount.
The new amount is 2000.
You wanted to triple the amount.
The new amount is 3000.
For more examples of how to create a switch statement, please consult
the following pages:
http://www.perl.com/CPAN-local/doc/manual/html/pod/perlsyn.html#Basic_BLOCKs_and_Switch_Statemen,
http://language.perl.com/misc/fmswitch, or
http://www.perl.com/CPAN-local/doc/manual/html/pod/perlfaq7.html#How_do_I_create_a_switch_or_case.
Loops
Perl has a while loop and a
for loop that are almost identical to their C++ or Java
counterparts, the only difference being that the body of the loop must be enclosed
between curly braces (just like the blocks in the if
statement). The other two looping constructs that it supports are the
do...until loop, and the
foreach loop.
do...until loops
The do...until loop is similar to the
do...while loop in C++, except that the condition is
negated. Its general form is:
do block until (condition)
which is equivalent to
do block while (!condition)
in C++.
foreach loops
A foreach loop has the form:
foreach $scalar (@array) block
and executes block as many times as there are
elements in @array, with
$scalar successively equal
to the first, second, third, etc, element of the array. It is suggested that you use the
foreach construct in favour of the regular
for and while constructs
whenever it is possible. In most cases, this makes for a smaller program and eliminates
the notorious "off-by-one-error".
Note that the keywords for and
foreach are, in fact, interchangeable. However, for
readability reasons we recommend that you use them as described in this section.
Statement modifiers
Finally, Perl does give statements one unexpected option: they can have one of the
following modifiers, placed after the statement, and not to be confused with the
control structures described above (even though the two are closely related).
Statement Modifiers
| Modifier |
Meaning |
Example |
| if expr |
Simple conditional if |
talk_to('me') if $you_care; |
| unless expr |
Akin to if-not |
call('me') unless $you_want_to_die; |
| while expr |
Evaluates repeatedly as long as expr is true |
$counter++ while -e "$file$counter"; |
| until expr |
Evaluates repeatedly as long as expr is false |
run('you') until $you_drop; |