Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 4.1 Statement BlocksChapter 4
Control Structures
Next: 4.3 The while/until Statement

4.2 The if/unless Statement

Next up in order of complexity is the if statement. This construct takes a control expression (evaluated for its truth) and a block. It may optionally have an else followed by a block as well. In other words, it looks like this:

if (some_expression) {
} else {

(If you're a C or Java hacker, you should note that the curly braces are required. This eliminates the need for a "confusing dangling else" rule.)

During execution, Perl evaluates the control expression. If the expression is true, the first block (the true_statement statements above) is executed. If the expression is false, the second block (the false_statement statements above) is executed instead.

But what constitutes true and false? In Perl, the rules are slightly weird, but they give you the expected results. The control expression is evaluated for a string value in scalar context (if it's already a string, no change is made; but if it's a number, it is converted to a string[1] ). If this string is either the empty string (with a length of zero) or a string consisting of the single character 0 (the digit zero), then the value of the expression is false. Anything else is true automatically. Why such funny rules? Because they facilitate branching on an emptyish versus nonempty string, as well as on a zero versus nonzero number, without having to create two versions of interpreting true and false values. Here are some examples of true and false interpretations:

[1] Internally, this isn't quite true. But it acts as if this is what it does.

0       # converts to "0", so false
1-1     # computes to 0, then converts to "0", so false
1       # converts to "1", so true
""      # empty string, so false
"1"     # not "" or "0", so true
"00"    # not "" or "0", so true (this one is weird, watch out)
"0.000" # also true for the same reason and warning
undef   # evaluates to "", so false

Practically speaking, interpretation of values as true or false is fairly intuitive. Don't let us scare you.

Here's an example of a complete if statement:

print "how old are you? ";
$a = <STDIN>;
if ($a < 18) {
        print "So, you're not old enough to vote, eh?\n";
} else {
        print "Old enough!  Cool!  So go vote!\n";
        $voter++; # count the voters for later

You can omit the else block, leaving just a then part, as in:

print "how old are you? ";
$a = <STDIN>;
if ($a < 18) {
        print "So, you're not old enough to vote, eh?\n";

Sometimes, you want to leave off the then part and have just an else part, because saying "do that if this is false," is more natural than saying "do that if not this is true." Perl handles this case with the unless variation:

print "how old are you? ";
$a = <STDIN>;
unless ($a < 18) {
        print "Old enough!  Cool!  So go vote!\n";

Replacing if with unless is in effect saying, "If the control expression is false, do..." (An unless can also have an else, just like an if.)

If you have more than two possible choices, add an elsif branch to the if statement, like so:

if (some_expression_one) {
} elsif (some_expression_two) {
} elsif (some_expression_three) {
} else {

Each expression (here, some_expression_one, some_expression_two, and some_expression_three) is computed in turn. If an expression is true, the corresponding branch is executed, and all remaining control expressions and corresponding statement blocks are skipped. If all expressions are false, the else branch is executed (if there is one). You don't have to have an else block, but having one is always a good idea. You may have as many elsif branches as you wish.

Previous: 4.1 Statement BlocksLearning Perl on Win32 SystemsNext: 4.3 The while/until Statement
4.1 Statement BlocksBook Index4.3 The while/until Statement