| Previous | Table of Contents | Next |
If you want to test the equality of numbers rather than strings, use the == operator.
% perl -e if (6 == 6) { print true\n; }
RESULT: true
% perl -e if (6 == 5.2) { print true\n; }
ne is the opposite of eq, and!= is the opposite of ==.
% perl -e if (yes ne no) { print true\n; }
RESULT: true
% perl -e if (6 != 5.2) { print true\n; }
RESULT: true
% perl -e $x = 8; if ($x == 8) { print true\n; }
RESULT: true
One common error (possibly the most common) among both novice and seasoned Perl programmers is using == when = is called for and vice versa. Dont do it! Remember:
== vs =
== tests two numeric values; = assigns a value to a variable.
== vs eq>
== tests numbers; eq tests strings.
You can also compare one quantity to another. The arithmetic comparators, shown in Table 1-2, should all be familiar.
| The expression... | ...evaluates to TRUE if |
|---|---|
| x > y | the number x is greater than the number y. |
| x < y | the number x is less than the number y. |
| x >= y | the number x is greater than or equal to the number y. |
| x <= y | the number x is less than or equal to the number y. |
There are analogous string operators, as shown in Table 1-3.
| The expression... | ...evaluates to TRUE if |
|---|---|
| word1 gt word2 | the string word1 comes after the string word2. |
| word1 lt word2 | the string word1 comes before the string word2. |
| word1 ge word2 | the string word1 comes after, or is equal to, word2. |
| word1 le word2 | the string word1 comes before, or is equal to, word2. |
Question: Is Hello less than or greater than hello? Or are they equal? The answer might surprise you: Hello comes before hello because the string comparators dont use dictionary order. They use ASCII order, in which uppercase letters precede lowercase letters. (Theres a table of ASCII characters in Appendix B.)
Youve learned that double quotes interpolate scalars and that scalars can be identified by their dollar sign. Sometimes youll just want to print a plain dollar sign, using a backslash to prevent Perl from trying to interpolate variables.
print Thatll be \$8.50, please.\n;
See that backslash in front of the dollar sign? Its there to keep Perl from thinking that $8 is a variable. If Perl sees $8.50, it interpolates $8, which is undefined, yielding
Thatll be .50, please.
Because you want to print the dollar sign, you need to let Perl know by backslashing it. Theres a simple rule about when to backslash characters inside double-quoted strings: Backslash weird characters (@, $, etc.) to get their literal meanings and backslash normal characters (n, r, t, etc.) to get their weird meanings (linefeed, carriage return, and tab, respectively).
Now lets make the if statement a bit more complex by adding an else clause, as shown in Listing 1-9.
Listing 1-9 tickets2: An if...else statement
#!/usr/bin/perl -w
print Would you like to see the 11:00am showing of Cape Fear? ;
$answer = <>;
chomp $answer;
if ($answer eq yes) {
print Thatll be \$8.50, please.\n;
} else {
print Okay- but youre missing a great movie. Have a nice day!\n;
}
The if statement in tickets2 contains an else clause. As before, if ($answer eq yes), the program asks for money. But if that condition isnt satisfied, the other print() statement executes. The statements following an else clause must be enclosed within curly braces, just like the statements following if.
% tickets2 Would you like to see the 11:00am showing of Cape Fear? no Okay- but youre missing a great movie. Have a nice day!
tickets2 assumes that the user typed no if he or she didnt type yes. What if you want your program to complain if the user said neither yes nor no? In other words
Then you need an if...elsif...else statement, shown in Listing 1-10.
Listing 1-10 tickets3: An if...elsif...else statement
#!/usr/bin/perl
print Would you like to see the 11:00am showing of Cape Fear? ;
$answer = <>;
chomp $answer;
if ($answer eq yes) { # If yes was typed...
print Thatll be \$8.50, please.\n;
} elsif ($answer eq no) { # Otherwise, if no was typed...
print Okay- but youre missing a great movie. Have a nice day!\n;
} else { # Otherwise...
print Im sorry, I didnt understand that.\n;
}
You can have as many elsifs as you want. tickets3 just needs one because there are three possible outcomes (yes, no, and everything else), and two of those are caught by the if clause and the else clause.
% tickets3
RESULT: Would you like to see the 11:00am showing of Cape Fear? maybe
Im sorry, I didnt understand that.
| Previous | Table of Contents | Next |