Logical Operators
Logical operators, also known as “short-circuit” operators, allow the program
to make decisions based on multiple criteria without using nested
if statements. They are known as
short-circuit operators because they skip (short circuit) the evaluation
of their right argument if they decide the left argument has already
supplied enough information to decide the overall value. This is not
just for efficiency. You are explicitly allowed to depend on this
short-circuiting behavior to avoid evaluating code in the right argument
that you know would blow up if the left argument were not “guarding” it.
You can say “California or bust!” in Perl without busting (presuming you
do get to California).
Perl actually has two sets of logical operators: a traditional set borrowed from C and a newer (but even more traditional) set of ultralow-precedence operators borrowed from BASIC. Both sets contribute to readability when used appropriately. C’s punctuational operators work well when you want your logical operators to bind more tightly than commas, while BASIC’s word-based operators work well when you want your commas to bind more tightly than your logical operators. Often they work the same, and which set you use is a matter of personal preference. (For contrastive examples, see the section Logical and, or, not, and xor in Chapter 3.) Although the two sets of operators are not interchangeable due to precedence, once they’re parsed, the operators themselves behave identically; precedence ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access