![](/WDVL/Website/Design/images/spacer.gif) |
Using the if, elsif, else and unless Control Statements
Using the if, elsif, else and unless Control
Statements
Be careful with your usage of "eq" versus
"=". Within an "if" test, if you write $name =
"Selena Sol", you will actually be assigning "Selena
Sol" to the variable $name rather than comparing it
to the value "Selena Sol". Since this action will be
performed successfully, the if test will always test to
true and the statement block will always be
performed even if $name did not initially equal
"Selena Sol". |
- The if test also provides for alternatives: the "else" and
the "elsif" control statements. The elsif alternative adds a
second check for truth and the else alternative defines a
final course of action for every case of failed if or elsif
tests. The following code snippet demonstrates the usage
of if, elsif, and else.
if ($name eq "Selena Sol")
{
print "Hi, Selena.\n";
}
elsif ($name eq "Gunther Birznieks")
{
print "Hi, Gunther\n";
}
else
{
print "Who are you?\n";
}
- Obviously, the else need not perform a match since it is a
catch-all control statement.
- The "unless" control statement works like an inverse "if"
control statement. Essentially it says, "execute some
statement block unless some condition is true". The
unless control statement is exemplified in the code below:
unless ($name eq "Selena")
{
print "You are NOT Selena!\n";
}
Additional Resources:
Statement Blocks
Table of Contents
foreach
| ![](/WDVL/Website/Design/images/spacer.gif) |
|