Perl has a variety of control statements. These are the one that allow you to control how your program runs and what statements get control of your steps.
The first statatement that we will cover is the if statement. The "if" statement allows you to set a condition. The synatx is:
if (condition){
statement
}
The logic is as follows, if the condition returns a true value then execute the statements in the block, otherwise do not execute anything. The "if" statement goes beyond that allows you to have an else statement:
if (condition){
statement
}
else {
statement
}
The else part of the above if statement will be executed only if the condition in our initial if statement is false. We can also improve this and add more flexibility by having multiple if statements:
if (condition){
statement
}
elsif (condition){
statement
}
else {
statement
}
Now we can run multiple test and choose from a variety of options as to what to execute.
Another type of control statements that perl has are loop statements, these allow to execute blocks of instructions a certain number of times. Let's take a look at some of these statements:
The "while" loops can be used in a couple of ways. We can test for our condition at the beginning of the loop or test our condition at the end of the loop. The syntax for testing the condition at the beginning of the loop is as follows:
while (condition){
statements
}
The condition will be tested first, if it is true then the statements will be executed otherwise they will be skipped. The syntax for testing the condition at the end is as follows:
do {
statements
} while (condition);
If you decide test at the end of the loop you will have the statements executed at least once.
Another type of loop is the "until" loop. Unlike the while loop, the "until" loop is used to execute certain statements while some condition is false. Just like the while loop we can test the condition at the beginning and at the end.
until (condition){
statatements
}
If we want to test the condition of the loop at end:
do{
statements
}until (condition);
Another type of loop which is very powerful is the for loop, unlike the while and until loops where we do not know the number of times we will be iterating through the statements, the for loop iterates through a specified number of loops (normally). The syntax is:
for( INITIALIZATION; CONDITION; INCREMENT/DECREMENT){
statements
}
The initialization expression is executed first, it can be used ot initalize the variables inside the loop, the condition expression is used as the test which will let us know if we should exit the loop or not, and the increment/decrement operator is used to increase our initial value or any other value we choose.
The last type of loops that I will discuss is the "foreach" loop, since one of the most powerful things in perl is the manipulation of arrays we of course have a loop that allows us to cycle through an array or hash. The syntax for the "foreach" loop is:
foreach LOOP_VAR (ARRAY){
statements
}
The LOOP_VAR variable is optional, if none is indicated then the default variable $_ is used. ARRAY is the name of our array, if we are using a hash we need to be able to retrieve a scalar value so we would have to use the function "keys" or "values" along with the hash in order to be able to retrieve a scalar value. The value retrieved is stored in LOOP_VAR which we can then manipulate in our statements.
There are also certain keywords which we can use within loops, I will mentioned them quickly and explain how they can be used.
last: jumps out of the current statement block
next: skips the rest of the statement block and continues with the next iteration of the loop
redo: restarts the statement block without testing the condition or doing the increment/decrement operation.
goto: jumps to a specified label
![]() |
![]() |
![]() |