The Perl Tutorial: What's Perl?(5)

Control Statements

Perl has a variety of control statements, which allow you to control how your program runs, and what statements control each step.

The first statement that we'll cover is the if statement. The if statement allows you to set a condition. The syntax is:


if (condition) {
STATEMENTS
}

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, allowing you to have an else statement:


if (condition) {
STATEMENTS
} else {
STATEMENTS
}

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){
STATEMENTS
} elsif (condition){
STATEMENTS
} else {
STATEMENTS
}

Now we can run multiple tests, and choose from a variety of options as to what to execute.

Another type of control statement that Perl offers are loop statements, which allow us to execute blocks of instructions a certain number of times. Let's take a look at some of these statements:

While loops can be used in a couple of ways. We can test for our condition at the beginning or end of the loop. The syntax for testing the condition at the beginning of the loop is:


while (condition) {
STATEMENTS
}

The condition will be tested first. If it is true then the statements will be executed, otherwise, they will be skipped. If the statements are executed, then the condition is tested again to see if they should be executed again. This continues until the condition is false.

The syntax for testing the condition at the end is:


do {
STATEMENTS
} while (condition);

If you decide test at the end of the loop, the statements will execute 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. But, just like the while loop, we can test the condition at the beginning and at the end of the loop:


until (condition) {
STATEMENTS
}

If we want to test the condition of the loop at end:


do {
STATEMENTS
} until (condition);

Another very powerful loop is the for loop. Unlike the while and until loops, where we don't know how many times we'll reiterate the statements, the for loop is normally used to iterate through a specific number of loops. The syntax is:


for (INITIALIZATION; CONDITION; INCREMENT/DECREMENT) {
STATEMENTS
}

The initialization expression is executed first. It can be used to initialize the variables inside the loop. The condition expression is used as the test that will tell us whether 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 loop that we'll discuss is the foreach loop. Since one of the most powerful things in Perl is the manipulation of arrays, we must, 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're using a hash, we'll need to be able to retrieve a scalar value, so we'd 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 (or $_), which we can then manipulate in our statements.

There are also certain keywords that we can use within loops:


last: jumps out of the loop immediately

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

File Manipulation