Perl Decision Making
Decision making statements come in two forms in Perl, ‘if’ statements and 'unless' statements. Both allow a decision to be made between two or more options.
‘if’ Statements
In its most basic form, an ‘if’ statement executes a group of statements if an expression evaluates to true. Its basic syntax is as follows.
if (expression) { # Statement(s) will execute if the expression is true. }
The following example checks whether the values of two variables are the same and displays a message if they are.
my $a = 10; my $b = 10; if ($a == $b) { print "a is equal to b"; }
This can be extended to execute a statement or statements if the expression is false as follows.
if (expression) { # Statement(s) will execute if the expression is true. } else { # Statement(s) will execute if the expression is false. }
This example adds an ‘else’ statement to the one above to output a message if ‘a’ and ‘b’ are not equal.
my $a = 10; my $b = 10; if ($a == $b) { print "a is equal to b"; } else { print "a is not equal to b"; }
The ‘if’ statement can be further extended with the use of ‘elsif’. Any number of ‘elsif’ statements can be used to extend the decision making process.
if (expression) { # Statement(s) will execute if the expression is true. } elsif (expression) { # Statement(s) will execute if the first expression is false # and the second expression is true. } else { # Statement(s) will execute if both the expressions are false. }
Again, the above example, that compares the variables ‘a’ and ‘b’ can be extended to check if they are equal, then check if ‘a’ is greater than ‘b’ and if neither of the conditions are true, output a third message.
my $a = 10; my $b = 10; if ($a == $b) { print "a is equal to b"; } elsif ($a > $b) { print "a is greater than b"; } else { print "a is less than b"; }
‘if’ statements can also be nested one inside another.
if (expression) { # Statement(s) will execute if the expression is true. if (expression) { # Statement(s) will execute if the expression is true. } }
Below are some examples of ‘if’ statements using the logical operators discussed in the previous section on Operators. It should be noted that Perl, unlike a lot of languages, does not provide a Boolean type for True and False, however, any non-zero number is considered a 'true' value, whilst zero and null are considered 'false' values.
my $a = 1; my $b = 1; my $c = 0; if ($a and $b) { # Display a message if 'a' and 'b' are true. print "Condition 1 is true\n"; } if ($c or $b) { # Display a message if 'c' or 'b' are true. print "Condition 2 is true\n"; } if ($c and $b) { # Display a message if 'c' and 'b' are true. print "Condition 3 is true\n"; } else { # Display a message if 'c' and/or 'b' are not true. print "Condition 3 is not true\n"; } if (not ($c and $b)) { # Display a message if 'c' and/or 'b' are not true. print "Condition 4 is true\n"; }
The results of running the above will be as follows.
Condition 1 is true Condition 2 is true Condition 3 is not true Condition 4 is true
'unless' Statements
In its most basic form, an ‘unless’ statement executes a group of statements if an expression evaluates to false. Its basic syntax is as follows.
unless (expression) { # Statement(s) will execute if the expression is false. }
The following example checks whether the values of two variables are the same and displays a message if they aren't.
my $a = 10; my $b = 11; unless ($a == $b) { print "a is not equal to b"; }
This can be extended to execute a statement or statements if the expression is true as follows.
my $a = 10; my $b = 11; unless ($a == $b) { print "a is not equal to b"; } else { print "a is equal to b"; }
As with 'if' statements, an 'unless' statement can be further extended using 'elsif'. Another similarity is the ability to nest one 'unless' statement inside another.