Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: use of last

by Eily (Monsignor)
on Jul 20, 2015 at 08:20 UTC ( [id://1135413]=note: print w/replies, xml ) Need Help??


in reply to use of last

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1135413]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found