10.07.2015 Views

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

Beginning Web Development With Perl : From Novice to ... - Nabo

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

APPENDIX ■ PERL BASICS 301We’ll see this opera<strong>to</strong>r used when we look at sorting things, where we have <strong>to</strong> knowwhether something goes before, after, or in the same place as something else.Boolean Opera<strong>to</strong>rsAs well as being able <strong>to</strong> evaluate the truth and falsehood of some statements, we can alsocombine such statements. For example, we may want <strong>to</strong> do something if one number is biggerthan another and another two numbers are the same. The combining is done in a very similarmanner <strong>to</strong> the bitwise opera<strong>to</strong>rs we saw earlier. We can ask if one value and another value areboth true, or if one value or another value are true, and so on.The opera<strong>to</strong>rs even resemble the bitwise opera<strong>to</strong>rs. To ask if both truth values are true, wewould use && instead of &.So, <strong>to</strong> test whether 6 is more than 3 and 12 is more than 4, we can write6 > 3 && 12 > 4To test if 9 is more than 7 or 8 is less than 6, we use the doubled form of the | opera<strong>to</strong>r, ||:9 > 7 || 6 > 8To negate the sense of a test, however, use the slightly different opera<strong>to</strong>r !. This hasa higher precedence than the comparison opera<strong>to</strong>rs, so use parentheses. For example, thistests whether 2 is not more than 3:!(2>3)while this one tests whether !2 is more than 3:!2>32 is a true value. !2 is therefore a false value, which gets converted <strong>to</strong> 0 when we do a numericcomparison. We’re actually testing if 0 is more than 3, which has the opposite effect <strong>to</strong> what wewanted.Instead of those forms, &&, ||, and !, we can also use the slightly easier-<strong>to</strong>-read versions,AND, OR, and NOT. There’s also XOR, for exclusive or (one or the other but not both are true),which doesn’t have a symbolic form. However, you need <strong>to</strong> be careful about precedence again:#!/usr/bin/perl -w# bool6.plprint "Test one: ", 6 > 3 && 3 > 4, "\ n";print "Test two: ", 6 > 3 and 3 > 4, "\ n";This prints, somewhat surprisingly, the following:$ perl bool6.plUseless use of a constant in void context at bool6.pl line 5.Test one:Test two: 1$We can tell from the presence of the warning about line 5 and from the position of theprompt that something is amiss (or least Unix users can—Windows users need <strong>to</strong> be a bitmore alert since Windows au<strong>to</strong>matically adds a newline character at the end of the program so

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!