The if Statement and Relational Operators

The if/else statement looks pretty much like the C version of the if/else statement:

if (condition) {
    # body of the if 
}

and

if (condition) {
    # body of the if 
} else {
    # body of the else 
}

The expression must be enclosed in (), and most of the relational operators are the same. About the only difference is that fact that the curly braces are required. In other words, the following is illegal:

if ($size == 20) 
    print "Size is 20
";     # Illegal –– no {}

The elsif Statement

The elsif statement is a shorthand for “else if.” For example:

if ($size == 1) {
    # operators running when $size is equal to 1 
} elsif ($size == –1) {
    # operators running when $size is equal to –1 
} elsif ($size == 0) {
    # operators running when $size is equal to 0 
} else {
    die("Illegal size value"); 
}

The die function prints an error message and aborts the program.

Conditional Operators

Perl has a rich set of conditional operators. In fact, there are two major sets of operators—one for numbers and one for strings. Table 2.1 lists the simple relational operators.

Table 2.1. Simple Relational Operators
Numeric String Meaning
== eq Equal
!= ne Not equal
< lt Less than
<= le Less than equal
> gt Greater than
>= ge Greater than or equal

The string version of the operators compares two strings much like the C function strcmp. The numeric version of the operators compares two values as if they were numbers.

Listing 2.5 shows how this works.

Listing 2.5. cond.pl
use strict; 
use warnings; 

if ("19" <= "101") {       # TRUE (numeric compare) 
    print "19 <= 101
"; 
}   
if ("19" le "101") {       # FALSE (string compare) 
    print "19 le 101
" 
} 

if ("able" eq "baker") {   # FALSE (string compare) 
    print "able eq baker
"; 

} 
if ("able" == "baker") {    # TRUE (numeric compare), 
                            # but warning issued 
    print "able == baker
"; 
}

The last if deserves a closer examination. The problem is that it uses the numeric version of the equality operator (==) rather than the string version (eq). The result is that “able” is converted to a number. The result of this conversion is 0. (That’s because Perl looks at the first character. It’s not a digit, so Perl does the best it can and returns the number 0.) Similarly “baker” goes through a similar conversion, and the result is 0. Because 0 == 0, the conditional is true.

So you have to worry not only about the old C problem of = versus ==,but also about the Perl problem of == versus eq.

This is one place where turning on warnings helps you discover problems in the code. Normally, Perl silently converts non-numeric values such as “able” into numbers. With warnings turned on, this sort of conversion results in a warning message being issued:

Argument "able" isn't numeric in eq at <file> line <line>.

Comparison Operators

The operator <=> (numeric) and cmp (string) return –1 if the first value is less than the second, 0 if they are equal, and 1 if the first value is bigger.

Common Mistake: = Versus ==

In C programming, one of the most common mistakes that new programmers make is using = rather than ==. Perl lets you make the same mistake. The following code is supposed to check whether $answer is 55, but it doesn’t:

if ($answer = 55)       # WRONG

Instead, the code assigns $answer the value 55 and then checks the result to see whether it’s true. The proper way to do this test is

if ($answer == 55)       # RIGHT

To novice programmers it may seem like I’m over emphasizing the = versus == problem. However, I can assure you that if you haven’t made this mistake, you will. (Experienced programmers will read this, chuckle, and say, “Been there. Done that!”)


Common Mistake: Using == for Strings

As mentioned previously, C programmers are used to using == to check whether two values are equal. For example:

if ("Sam" == "Joe") {    # WRONG

However, Perl has two equal operators—string equal (eq) and numeric equal (==). The preceding example uses numeric equal. When “Sam” is converted to a number, the result is 0. The same thing happens when “Joe” is converted. Because 0 == 0, the comparison is true.

When two strings are compared, you need to use the string equal operator (eq):

if ("Sam" eq "Joe") {    # RIGHT


The defined Function

The defined function tests to see whether a variable has a value. Listing 2.6 shows an example.

Listing 2.6. define.pl
use strict; 
use warnings; 

my $name = "Sam";   # A defined variable 
my $value;           # A undefined variable 

if (defined($name)) {
    print "The name is defined as $name
"; 
} 

if (not defined($value)) {
    print "but the value is not defined
"; 
}

Normally, a variable becomes defined when you assign it a value. You can make it undefined by setting it to the special value undef:

use strict; 
use warnings; 
my $name = "Sam";     # $name is defined 
$name = undef;          # $name is now undefined 

if (not defined($name)) {
    print "No name is defined
"; 
}

An alternative way of undefining a variable is to call the undef function:

undef($name);

or

undef $name;

Common Mistake: Using undef as a Function

In the following code fragment, the programmer wanted to print a message if the variable is undefined. But he made a mistake:

if (undef($value)) {     # ERROR here 
    print "Value is not defined
"; 
}

This does not work because the undef function does not test the value of the variable for undefined. Instead it sets the value of the variable to undefined and returns undefined in the process.

The correct way to check whether a variable is not defined is to use the following code:

if (not defined($value)) {     # This works 
    print "Value is not defined
"; 
}


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

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