Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

circuit evaluation 1

Status
Not open for further replies.

sureshp

Programmer
Aug 28, 2000
79
Hi friends,

Can you explain me about circuit evaluation in PERL and where it is used.

Suresh.
 
short circuit boolean evaluation (scbe - I can't be bothered to type *that* again) is when you have some code like this

$x=2;
$y=3;

if($x > 1 && $y == 3)

the $y == 3 will not get evaluated if the interpreter is using scbe

does Perl use scbe? don't know to be honest.. but you can find out with this bit of code

sub x{
2;
};

sub y{
print "doesn't do scbe.....\n";
3;
};

if( &x > 1 && &y == 3){
print "this line will always get printed\n";
}

try using 'and' as well as '&&'

sorry i can't just tell you -- but I don't have Perl on this machine yet
Mike
michael.j.lacey@ntlworld.com
 
According to the perl man pages, perl DOES use short circuit evaluation on &&, AND, ||, OR. The only difference between the C-style and the others is the precedence (the words have very low precedence). Short circuit evaluation means that perl will only evaluate the second part of the expression if it needs to. In an AND statement, if the first part is false the whole statement is false, so it doesn't need to evaluate the second part. In an OR statement, if the first part is true the whole statement is true, so it doesn't need to evaluate the second part. This is used frequently by experienced perl coders for statements like:
Code:
open(FILE,$filename) or die("Couldn't open file");
If the open is successful (returns true) then the die will NOT be executed. If the open is unsuccessful (returns false) then the die WILL be executed. You'll see this a LOT. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top