Logical Operators

Be the first to comment | 3I like it!
September 26, 2001, 11:00 PM —  ITworld — 

Perl's operators that perform logical OR/AND/NOT operations come in two
forms: a high precedence symbolic form -- ||, &&, ! -- and a low
precedence form -- or, and, not (respectively).

Logical AND and OR are binary operators often used to combine
expressions in a conditional statement:

if ($value > 5 && $value < 10) {
print "$value is between 5 and 10 exclusive\n";
}

if (lc($input) eq 'q' || lc($input) eq 'quit'){
warn "Quitting application now\n";
clean_up();
exit;
}

The NOT operator is a unary operator that returns the negated
(opposite) truth value of its argument:

if ( not $done ) { # also: if(!$done){
print "We are not finished\n";
}

Often we can use either the high or low precedence forms, however,
occasionally precedence matters. Consider the following mistaken
expression and how the logical test is actually parsed:

$a=0;
$b=1;
if (not $a && not $b) {
print "\$a and \$b are false\n";
}

The programmer wanted to test that both $a AND $b were false. If $a is
false, then not($a) would be true, and similarly for $b. This
expression obviously fails because the precedence of && is much higher
than the precedence of 'not'. The expression is actually parsed as:

if ( not ($a && (not $b)) ) {

Not what was intended. We could fix this in a few different ways:
Either use the lower precedence form of AND ('and') or the higher
precedence form of 'not':

if ( !$a && !$b) {

if (not $a and not $b) {

A more interesting aspect of the logical AND and OR is that they short-
circuit their second operand if they do not need to check it to
determine the truth or falsity of the entire expression:

$a = 0;
$b = 1;
if ($a and $b) { print "Both a and b are true\n" }

In the above situation, Perl knows that for the AND operation to be
true, both sides must be true. If the left side is false, then Perl
doesn't bother checking the right side (it already knows the whole
expression must be false). This short-circuit, or lazy evaluation,
comes in handy in various situations outside of conditional tests, one
of which you should be familiar with:

open(FILE, $file) or die "Can't open $file: $!";

The open() function returns a true value when it succeeds. Perl knows
that only one expression must be true for an OR operation to succeed,
so if the left expression is true Perl ignores the right-hand side. In
this case, that means the right hand side is only evaluated if the file
could not be opened (in which case, the die() function is called).

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Free stuff

Win an Amazon Kindle!
This month's giveaway gadget - Amazon's Kindle - will keep you entertained on the long trip home to visit family and friends over the holidays. Enter the drawing now!

Applied Security Visualization
By Raffael Marty
Published by Addison-Wesley Professional
Learn more!

 

IT Manager's Handbook
By Bill Holtsnider and Brian D. Jaffe
Published by Morgan Kaufmann
Learn more!

 

Windows Vista Resource Kit
By Mitch Tulloch, Tony Northrup, and Jerry Honeycutt
Published by Microsoft Press
Learn more!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources