"If" and "unless" are both logical operators, governed by the same syntax. In fact, they are opposites, as "if" tests for something that is true, while "unless" tests for something that isn't true. Nevertheless, you can't combine them in Perl with the same sort of flexibility that you can in English... you can do one, or the other, but you can't combine them to create some sort of weird hybrid expression like you can in English.

For example,

unless (/^#!/) {next READ if (/^\s*#/)};
can be rewritten like this:
unless (/^#!/) { if (/^\s*#/) { next READ; } }
Notice how there's really two logical operations going on, completely separate from one another. First, the "unless", with its code-block, and then (within the "unless" code block) an "if" statement with its own code-block. Makes perfect sense, and the Perl interpreter has no problems.

However, the next example:

if (/^\s*#/) { next READ } unless (/^#!/);
would be rewritten as:
if (/^\s*#/) { next READ } unless (/^#!/);
which doesn't work at all. Where's the code-block for the "unless" statement? Perl doesn't see one, and so throws up its hands because it doesn't know what to do.

If you confuse yourself with the reverse notation, just try to rewrite it in the "proper" way and you'll see very clearly what will and won't work.

Gary Blackburn
Trained Killer


In reply to Re: Help with perl syntax by Trimbach
in thread Help with perl syntax by zzspectrez

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.