The if Statement
To control whether statements are executed based on a condition in a Perl program, you usually use an if statement. The syntax of an if statement is as follows:
if (expression) block
The statement works like this: If the expression evaluates to true, the block of code is run. If the expression is false, the block of code is not run. Remember that the block includes the braces. Consider this example:
if ( $r == 5 ) { print 'The value of $r is equal to 5.'; }
The expression being tested is $r == 5. The == symbol is an equality operator. If the two operands on either side$r and 5are numerically equal to one another, the expression is considered to be true, and the print statement is executed. If $r is not equal to 5, the print statement is not executed.
The if statement can also run one set of statements if a condition is true and another set of statements if it's not. That structure is called an if-else statement. The syntax looks like this:
if (expression) #If expression is true... block1 # ...this block of code is run. else block2 # Otherwise this block is run.
The first block, block1, is run only if the expression is true; if the expression is not true, block2, following the else, is run. Now consider this example:
$r=<STDIN>; chomp $r; if ($r == 10) { print '$r is 10'; } else { print '$r is something other than 10...'; $r=10; print '$r has been set to 10'; }
CAUTION
In the preceding example, notice that to assign a value to $r, I used the assignment operator, =. To test the value of $r, I used the numeric equality test operator, ==. Do not confuse them in your programs, because debugging can be very difficult. Remember that = assigns a value and == tests for equality. If you use the -w option to turn warnings on, Perl can sometimes warn you if you have made this error.
Yet another way of structuring an if statement is to check multiple expressions and run code depending on which expressions are true:
if (expression1) # If expression1 is true ... block1 # ...run this block of code. elsif (expression2) # Otherwise, if expression2 is true... block2 # ...Run this block of code. else block3 # If neither expression was true, run this.
You can read the preceding block like this: If the expression labeled expression1 is true, then the block block1 is run. Otherwise, control falls to the elsif and expression2 is tested; if it's true, then block2 is run. If neither expression1 nor expression2 is true, then block3 is run. The following is an example of real Perl code that demonstrates this syntax:
$r=10; if ($r==10) { print '$r is 10!'; } elsif ($r == 20) { print '$r is 20!'; } else { print '$r is neither 10 nor 20'; }
The Other Relational Operators
So far, you've been comparing numeric quantities in your if statements with the equality operator, ==. Perl actually has quite a few operators for comparing numeric values, most of which are listed in Table 3.1.
Table 3.1 Numeric Relational Operators
Operator |
Example |
Explanation |
---|---|---|
== |
$x == $y |
True if $x equals $y |
> |
$x > $y |
True if $x is greater than $y |
< |
$x < $y |
True if $x is less than $y |
>= |
$x >= $y |
True if $x is greater than or equal to $y |
<= |
$x <= $y |
True if $x is less than or equal to $y |
!= |
$x != $y |
True if $x is not equal to $y |
To use these operators, you can simply put them in anywhere that your program needs to test relations between numeric values. An example of the use of these operators in an if statement is shown in Listing 3.1, which you can type in and run (do not type the line numbers"1:" and so forth).
Listing 3.1 A Small Number Guessing Game
1: #!/usr/bin/perl -w 2: 3: $im_thinking_of=int(rand 10); 4: print "Pick a number:"; 5: $guess=<STDIN>; 6: chomp $guess; # Don't forget to remove the newline! 7: 8: if ($guess>$im_thinking_of) { 9: print "You guessed too high!\n"; 10: } elsif ($guess < $im_thinking_of) { 11: print "You guessed too low!\n"; 12: } else { 13: print "You got it right!\n"; 14: }
The various parts of the program work as follows:
Line 1: This line is the standard first line of a Perl program; it indicates the interpreter you want to run and the -w switch to enable warnings. See Hour 1, "Introduction to the Perl Language"; your first line may need to look slightly different.
Line 3: The (rand 10) function picks a number between 0 and 10, and the int()function truncates it so that only integers 0 to 9 are assigned to $im_thinking_of.
Lines 4-6: This line asks the user for the guess, assigns it to $guess, and removes the trailing newline character.
Lines 8-9: If $guess is greater than the number in $im_thinking_of, then these lines print an appropriate message.
Lines 10-11: Otherwise, if $guess is less than the number in $im_thinking_of, these lines print that message.
Lines 12-13: The only choice left is that the user guessed the number.
The operators in Table 3.1 are used only for testing numeric values. Using them to test nonalphabetic data results in behavior that you probably don't want. Consider this example:
$first="Simon"; $last="simple"; if ($first == $last) { # == is not what you want! print "The words are the same!\n"; }
The two values $first and $last actually test equal to each other. The reason was explained in Hour 2, " Perl's Building Blocks: Numbers and Strings": If nonnumeric strings are used when Perl is expecting numeric values, the strings evaluate to zero. So the preceding if expression looks something like this to Perl: if ( 0 == 0 ). This expression evaluates to true, and that's probably not what you wanted.
NOTE
If warnings are turned on, trying to test two alphabetic values (simple and Simon in the preceding snippet) with == will generate a warning message when the program runs to alert you to this problem.
If you want to test nonnumeric values, you can use another set of Perl operators, which are listed in Table 3.2.
These operators decide "greater than" and "less than" by examining each character left to right and comparing them in ASCII order. This means that strings sort in ascending order: most punctuation first, then numbers, uppercase, and finally lowercase. For example, 1506 compares less than Happy, which compares less than happy.
Table 3.2 Alphanumeric Relational Operators
Operator |
Example |
Explanation |
---|---|---|
eq |
$s eq $t |
True if $s is equal to $t |
gt |
$s gt $t |
True if $s is greater than $t |
lt |
$s lt $t |
True if $s is less than $t |
ge |
$s ge $t |
True if $s is greater than or equal to $t |
le |
$s le $t |
True if $s is less than or equal to $t |
ne |
$s ne $t |
True if $s is not equal to $t |
What Truth Means to Perl
Up to this point, you've been reading about "if this expression is true..." or "...evaluates to true...," but you haven't seen any formal definition of what Perl thinks "true" is. Perl has a few short rules about what is true and what is not true, and the rules actually make sense when you think about them for a bit. The rules are as follows:
The number 0 is false.
The empty string ("") and the string "0" are false.
The undefined value undef is false.
Everything else is true.
Make sense? The only other point to remember is that when you're testing an expression to see whether it's true or false, the expression is simplifiedfunctions are called, operators are applied, math expressions are reduced, and so onand then converted to a scalar value for evaluation to determine whether it is true or false.
Think about these rules, and then take a look at Table 3.3. Try to guess whether the expression is true or false before you look at the answer.
Table 3.3 True or False Examples
Expression |
True or False? |
---|---|
0 |
False. The number 0 is false. |
10 |
True. It is a nonzero number and therefore true. |
9>8 |
True. Relational operators return true or false, as you would expect. |
-5+5 |
False. This expression is evaluated and reduced to 0, and 0 is false. |
0.00 |
False. This number is another representation of 0, as are 0x0, 00, 0b0, and 0e00. |
"" |
False. This expression is explicitly mentioned in the rules as false. |
" " |
True. There's a space between the quotes, which means they're not entirely empty. |
"0.00" |
True. Surprise! It's already a string, but not "0" or "". Therefore, it is true. |
"00" |
True also, for the same reason as "0.00" |
"0.00" + 0 |
False. In this expression, 0.00+0 is evaluated, the result is 0, and that's false. |
Until now, you've seen only expressions with relational operators as the conditions in if statements. Actually, you can use any expression that will evaluate to true or false the way you would want:
# The scalar variable $a is evaluated for true/false if ($a) { ... } # Checks the length of $b. If nonzero, the test is true. if (length($b)) { .... }
Recall from Hour 2, "Perl's Building Blocks: Numbers and Strings," that the assignment operator = returns a valuethe value that was assigned. That value, of course, is also true or false:
$a = 1; $b = 2; print qq(The statement "$a = $b" is ); if ($a = $b) { # value is 2, therefore true print "true"; } else { print "false"; }
This code prints The statement "1 = 2" is true. Now you see why using = when you meant == is such a pitfall.
The value undef is a special value in Perl. Variables that have not yet been set have the value of undef, and some functions return undef on failure. It's not 0, and it's not a regular scalar value. It's kind of special. In a test for truth, undef always evaluates to false. If you try to use the undef value in a math expression, it's treated as though it were 0.
Using variables that haven't been set yet is usually a sign of a programming error. If you're running your Perl programs with warnings enabled, the value undef in an expression or as an argument to some functions causes Perl to generate the warning Use of uninitialized value.
Logical Operators
When you're writing programs, you sometimes need to code something like the following: Do this if $x is true and if $y is true, but not if $z is true. You can code this example into a series of if statements, but it's not pretty:
if ($x) { if ($y) { if ($z) { # do nothing } else { print "All conditions met.\n"; } } }
Perl has a whole class of operators for connecting together true and false statements like this, called logical operators. The logical operators are shown in Table 3.4.
Table 3.4 Logical Operators
Operator |
Alternative Name |
Example |
Analysis |
---|---|---|---|
&& |
and |
$s && $t |
True only if $s and $t are true |
$q and $p |
True only if $q and $p are true |
||
|| |
or |
$a || $b |
True if $a is true or $b is true |
$c or $d |
True if $c is true or $d is true |
||
! |
not |
! $m |
True if $m is not true |
not $m |
True if $m is not true |
Using the operators in Table 3.4, you could rewrite the previous snippet much more concisely as follows:
if ($x and $y and not $z ) { print "All conditions met.\n"; }
Expressions connected with logical operators are evaluated from left to right, until a value of true or false can be determined for the entire expression. Examine the following code:
1: $a=0; 2: $b=1; 3: $c=2; 4: $d=""; 5: if ($a and $b) { print '$a and $b are true'; } 6: if ($d or $b) { print 'either $d or $b is true'; } 7: if ($d or not $b or $c) 8: { print '$d is true, or $b is false or $c is true'; }
Lines 1-4: These lines give the variables default values.
Line 5: $a is evaluated first. It is false, so the and expression cannot possibly be true. $b is never evaluated; it doesn't have to be, because the truth of the expression is known after evaluating $a. The print is not executed.
Line 6: $d is evaluated first. It is false. Even if $d is false, the expression might still be truebecause it contains a logical orso $b is examined next. $b turns out to be true; therefore, the expression is true, and the print happens.
Line 7: $d is evaluated first. It is false. But although $d is false, the expression might still be trueas seen in line 4because it contains a logical or. Next, the truth of $b1, so trueis negated, so this expression becomes false. The truth of the or statement cannot be determined yet, so $c is evaluated. $c turns out to be true, so the whole expression is true, and the print happens.
This behaviorstopping the evaluation of a logical expression as soon as the truth can be determinedis called short-circuiting. This feature is used by Perl programmers to construct simple flow-control statements out of logical operators and to avoid the if statement entirely:
$message="A and B are both true." ($a and $b) or $message="A and B are not both true.";
In the preceding example, if either $a or $b is false, the right side of the or must be evaluated, and the message is changed. If both $a and $b are true, the or must be true, and it's not necessary to evaluate the right side. The truth value of the entire expression isn't used at all; this example uses the short-circuit side effects of the and and or operators to manipulate $message.
CAUTION
The || operator and or aren't completely alike. They differ in that || has higher precedence than or. This means that in an expression, || tends to be evaluated sooner than or. This is similar to multiplication having higher precedence than addition in normal mathematical expressions. The same caution applies to &&/and, and !/not. If you're unsure, use parentheses to guarantee the order in which the expression will be evaluated.
An interesting property of Perl's logical operators is that they don't simply return true or false. They actually return the last value evaluated. For example, the expression 5 && 7 doesn't just return trueit returns 7. This allows constructs like this:
# Set $new to old value if $old is true, # otherwise use the string "default". $new=$old || "default";
which is a little more concise than the code
$new=$old; if (! $old) { # was $old empty (or false)? $new="default"; }
This trick can make your code less readable. Understanding how it works can be helpful, though, if you are going to be looking at much Perl code written by others.