Actually last will only work on loops. A single block, without any kind of flow control (if, do while, for) counts as a loop that executes only once. So

{ open my $fh, "<", "some_file" or last; }
Will work, and you can make it work with your code by doubling the curly brackets:
if (/VALID/) {{ # First bracket is for the if, second is for a loop-like block open my $fh, "<", "some_file" or last; }}

last will never allow you to exit an if-block, because it is often used in constructs such as:

while (<>) { if (/INVALID/) { last; } }
Note that it's always possible to name the while loop to provide the name to last and make it clearer what you are leaving.

Edit: completed my last sentence (apparently I forgot what I was saying mid-sentence), and added a condition after the if, so that the code doesn't look so odd.

Edit: BTW, I think that karlgoethebier's version is easier to read.


In reply to Re: use of last by Eily
in thread use of last by morgon

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.