http://qs321.pair.com?node_id=78958


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 - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain