Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Efficiency of multiple if statements

by Masem (Monsignor)
on May 09, 2001 at 00:58 UTC ( #78958=note: print w/replies, xml ) Need Help??

in reply to Efficiency of multiple if statements

At least, if not to combine the regexes (since one is a affrimative case, one negative), but to combine everything into one if statement:
if ( ( -M "$logdir/$f" > 3.0 ) && ( $f =~ /^(ab|def)/i ) && ( $f !~ /gz\b/ ) ) { system ( "/usr/local/bin/gzip", "$logdir/$f" ); }
The perl interpreter is smart enough to recongize when it can short-circuit the above operation when it encounters a false statement; that is, if the -M operative fails, the regexs should never be performed. Not only that, this, IMO, is much easier to read and understand ("Oh, this system call only happens when these 3 conditions are met..."), and much more maintainable.

Mind you, I'm assuming here that you want to take no alternate actions if those if statements fail. If you do, you need seperate else statements as your code above is ready for.

Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://78958]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2021-04-17 11:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found