topics that matter; ideas worth sharing

share a tip, submit a link, add something new

Logical Operators

Be the first to comment | 1I like it!
September 27, 2001, 12:00 AM —  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.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff
Featured Sponsor

Get a broad understanding of important regulations and how you can make sure your site is in adherence.





Learn how VeriSign SGC-enabled SSL Certificates can help improve site security and customer confidence in the free white paper, "How to Offer the Strongest SSL Encryption." In this paper you will learn the differences between weak and strong encryption and what they mean for your site's performance.

Get VeriSign's free white paper: "The Latest Advancements in SSL Technology" and learn about the benefits of strong SSL encryption, Extended Validation (EV) SSL and security trust marks and what these SSL offerings can do for your site.

Now with Extended Validation (EV) SSL available from VeriSign, you can show your customers that they can trust your site. Learn about EV SSL benefits in this free VeriSign white paper.

More Resources