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
Will work, and you can make it work with your code by doubling the curly brackets:{ open my $fh, "<", "some_file" or last; }
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:
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.while (<>) { if (/INVALID/) { last; } }
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.
|
|---|