perl

Page 66

54

Chapter 4 Operators

C. D.

’adder’ gt ’add’ # true ’10’ lt ’9’ # true

In example A, == requires a numeric context, thus both strings are converted into 0. Both sides are equal and evaluates to true (although you would receive a warning if -w is enabled). In example B, Perl will stop after checking the first character since ‘a’ is greater than ‘A’. Beware that in the ASCII table capital characters have smaller ASCII codes than the small letter counterparts! In example C, because the first three characters are the same and Perl cannot yet deduce whether ‘adder’ is greater than ‘add’ the longer string shall be considered greater. In example D, since we compare with lt, ‘1’ is less than ‘9’, therefore, the comparison evaluates to true. This example and example A illustrate why in Perl we need 2 sets of comparison operators. Because Perl is loosely typed and does type conversions automatically, there should be a method for Perl to know whether you would like to compare them as numbers or strings.

4.2.5

Logical Operators

Logical operators include the following: Operator || or && and ! not xor

Description Logical OR Logical AND Logical NOT, i.e. negation Logical XOR — Exclusive OR

The logical operators performs Boolean logic arithmetic. We have seen how to do a test using the comparison and equality operators. But what if you would like to carry out two or more tests and check if all of them are true? Boolean algebra can do it rather easily. However, it is out of the scope of this tutorial somehow for me to teach you the specifics of Boolean algebra, and I would focus on how to use the Perl logical operators only. You may discover that there are two sets of OR, AND and NOT operators. ||, && and ! refer to the C-Style version. If you know C/C++, these operators would look familiar to you, and are continued to be supported in Perl. Perl also has its own set, consisting of or, and, not and xor. Note that Perl gives you an extra xor logical operator, that is not available in C/C++. The two sets differ only by precedence (which you will learn in the next section). The C-style operators have higher precedence, while the Perl operators have the lowest precedence among all the Perl operators. In the above example, we would like to see if the results of both tests are true. We can then use either logical AND operator to do it. The result would be true only if both tests are true, and false if otherwise. The logical OR operators return false only when both tests are false, and true if otherwise. The logical NOT operator toggles the truth value. For example, !(13 < 25) is false, because it inverts the truth value of 13 < 25, which is true. The exclusive or operator returns true if exactly one of the two operands is false. That is, one is true while the other is false. Here is the truth table of the logical operators we have covered so far: Here are several examples to conclude:


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.