Agreed. The cascade of tests with and is preferable to my eye.

Just a note that there is a difference between and vs &&. The difference is order of precedence. BrowserUK used the word form so that he didn't have to worry about order of precedence so much. You can also use parentheses with either form to ensure the precedence is correct. The original example is in danger of accidentally mixing up precedence, and getting a completely wrong result.

A third alternative, in some cases, is the "quick fail." This is a series of reasons to break out of a loop or return from a subroutine early.

while (<>) { next if /^\s*\#/; # comment next if /^\s*$/; # empty line do_the_work($_); }
is equivalent to
while (<>) { if (not /^\s*\#/ and not /^\s*$/) { do_the_work($_); } }

--
[ e d @ h a l l e y . c c ]


In reply to Re: Re: Nested testing of many conditions--a better way? by halley
in thread Nested testing of many conditions--a better way? by bradcathey

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.