The notion you have here is 'short-circuiting' the evaluation. The 'if' evaluation works from the left and stops as soon as it determines that the final value does not depend on any of the clauses to the right. (One false with a string of and'd clauses is all you need. Ditto for one true and or'd clauses. It's the mixed logics that get you every the time....)
if (($x eq 'c'}&&($y eq 'd')&&($z eq 'e')){ }
If $x ='One', then the tests for $y and $z will be skipped, it does not matter what they are, the result is a big False. If you arrange the clauses in your compound 'if' in the order (roughly) of frequency, you will get the most speed out of it.

That said, from a maintenance point of view, compound booleans are a pain to muck with unless the original author did something nice with the indentation. For example --

if ( ($x eq 'a') && ($y eq 'b') && ($z eq 'c') ) { }
I much prefer to cope with a switch statement or a cascade of if/elsif/else's when I have to trouble shoot some old code. It make the logic a bit clearer (to my mind), and makes adding another condition much easier.

----
I Go Back to Sleep, Now.

OGB


In reply to Re: In what order is a compound IF evaluated? by Old_Gray_Bear
in thread In what order is a compound IF evaluated? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.