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 5—are 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 code if a condition is true and other code if it's not. That structure is called an if-else statement. The syntax looks like this:

if (expression)       #If expression is true…
    BLOCK             # …this block of code is run.
   else
    BLOCK             # Otherwise this block is run.

The block following the expression is run only if the expression is true; the block following the else is run if the expression is not true. 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 an assignment operator, =. To test the value of $r, I used an equality operator, ==. They are very different operators and do different things. Do not confuse them in your programs because debugging can be very difficult. Remember that = assigns a value and == tests for equality.


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 testing the equality of numeric values, most of which are listed in Table 3.1.

Table 3.1. Numeric Relational Operators
OperatorExampleExplanation
==$x == $yTrue if $x equals $y
>$x >$yTrue if $x is greater than $y
<$x <$yTrue if $x is less than $y
>=$x >= $yTrue if $x is greater than or equal to $y
<=$x <= $yTrue if $x is less than or equal to $y
!=$x != $yTrue 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, such as an if statement as shown in Listing 3.1.

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!
";
10:   } elsif ($guess < $im_thinking_of) {
11:                print "You guessed too low!
";
12:   } else {
13:               print "You got it right!
";
14:   }

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 int() 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!
";
}

The two values stored in $first and $last actually test equal to each other. The reason was explained in Hour 1: 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.

Table 3.2. Alphanumeric Relational Operators
OperatorExampleExplanation
eq$s eq $tTrue if $s is equal to $t
gt$s gt $tTrue if $s is greater than $t
lt$s lt $tTrue if $s is less than $t
ge$s ge $tTrue if $s is greater than or equal to $t
le$s le $tTrue if $s is less than or equal to $t
ne$s ne $tTrue if $s is not equal to $t

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 greater than Happy, which compares greater than happy.

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 simplified—functions are called, operators are applied, math expressions are reduced, and so on—and 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
ExpressionTrue or False?
0False. The number 0 is false.
10True. It is a nonzero number and, therefore, true.
9>8True. Relational operators return true or false as you would expect.
−5+5False. This expression is evaluated and reduced to 0, and 0 is false.
0.00False. 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 in 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" + 0False. In this expression, 0.00+0 is evaluated, the result is 0, and that's false.

Until now, with if statements you've seen only relational operators. 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)) { ….  }

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. When testing for truth—in an if statement—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, using the value undef in an expression or passing the undefined value 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.
";
       }
   }
}

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
OperatorAlternate NameExampleAnalysis
&&and$s && $tTrue only if $s and $t are true
  $q and $pTrue only if $q and $p are true
||or$a || $bTrue if $a is true or $b is true
  $c or $dTrue if $c is true or $d is true
!not! $mTrue if $m is not true
  not $mTrue 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.
";
}

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 snippet:

1:   $a=0; $b=1; $c=2; $d="";
2:
3:   if ($a and $b) {  print '$a and $b are true'; }
4:   if ($d or $b) { print 'either $d or $b is true'; }
5:   if ($d or not $b or $c)

6:         { print '$d is true, or $b is false or $c is true'; }

Line 1: This line gives the variables default values.

Line 3: $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 4: $d is evaluated first. It is false. Even if $d is false, the expression might still be true—because it contains a logical or—so $b is examined next. $b turns out to be true; therefore, the expression is true, and the print happens.

Line 5: $d is evaluated first. It is false. Even if $d is false, the expression might still be true—as seen in line 4—because it contains a logical or. Next, the truth of $b—1, so true—is 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 behavior—stopping the evaluation of a logical expression as soon as the truth can be determined—is 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 true, it 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";
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.79.121