Computers are very efficient at decision making (of the decisions, they are programmed to make) and at repeating a task. Control structures allows the programmers define the decisions and to iterate.
if/elsif/else:
This structure has the following syntax:
if (condition) {Code;}
if (condition) {Code;}else {Code;}if (condition) {Code;}elsif {Code;}.... else {Code;}
Unlike C++, {} are not optional under any condition. They must be used even if they are followed by a single statement:
if($color eq "red") {
print "red";
} elsif {
print "white";
} else {
print "blue";
}
unless:
We just ifs and all kinds of elses. Now suppose you don't want the if. You only want the else. You can use unless:
unless(condition) {Code;}
unless ( $a < 13) {
# do something
}
while:
The while loop repeats a bunch of statements until as long as the condition specified is true:
while (Condition)
{
Code;
}
until:
until is to while what unless is to if. In other words, it does the opposite of else. It iterates over a statement block until something is true.
unless (condition)
{
Code;
}
do{} while/until:
In a while loop or the do loop, the condition is tested at the top. This means that if the condition is not true from the first place (or true in case of do), the code inside would never be executed. A lot of times you would want the code to run at least once before the condition is test. For such a scenario, there is do while and do until loops. The while and until act like they are supposed to. The only difference is that they are at the bottom instead of at the top.
do {
Code;
} while(Expression)$stops = 0;
do {
$stops = 0;
print "Next stop? ";
chomp($location =);
} until $stops > 5 || $location eq 'home';
for:
The perl for loop acts much like C's for loop:
for (Declare / Initialize; Condition; Increment / Decrement)
{
# Code;
}for( $i = 0; $i <= $#array; $i++ ) {
print $array[$i]; # print each element of array, one per loop
}
If you look carefully, you would see that there are three fields inside the for( ) loop. The leftmost one can be used for initialization. If you do not wish to initialize anything, leave it blank but do not omit the semicolon. You can leave any or all of the three fields blank. The middle field is the condition. As long as this condition is true, the loop would continue to run. As soon as it becomes false, the loop is exited. The rightmost field can be used to increment or decrement values.
foreach:
The foreach statement takes a list of values and assigns them one at a time to a scalar variable, executing a block of code with each successive assignment.
foreach $i (@list) {
# code
}@array = (1, 2, 3);
foreach $b (reverse @a) {
print $b;
}
The following is also possible because of an implied $_:
foreach (reverse @a) {
print;
}
next and last:
The next and last operators allow you to modify the flow of your loop. The next operator would allow you to skip to the end of your current loop iteration, and start the next iteration. The last operator would allow you to skip to the end of your block, as if your test condition had returned false.
@array = ( 1 .. 9 );
foreach $item (@array) {
if ($item == 3 ) {
next;
}
if ($item == 7 ) {
last;
}
print $item, "\s";
}# 1 2 4 5 6
Take a look at the result. The number 3 is missing because next operator interrupted the loop before the print statement. 7, 8, and 9 are missing because last interrupted before the print statement.
Perl also provides Labels but I do not recommend anyone to use them.