In Perl, one can often avoid using control blocks, like this:
print "$_\n" foreach(@files);
instead of:
foreach(@files){
print "$_\n";
}
How does this syntax work in the following, more complex case:
die("Not a file: $_") unless -f $_ foreach(@files);
It gives me a syntax error. I'm not trying to write obfuscated code, it's just an unimportant part in the program, and so I want to express it as concisely as possible.
SUMMARIZED ANSWERS:
I can accept only one answer as the accepted answer, but I like the following ones from Chris and Jon best.
This one uses foreach
as I intended, but without the syntax error
:
-f or die "Not a file: $_" foreach @files;
And the following one is at least as good. I like that die
is at the beginning of the statement because that's what the reader's attention should be directed toward:
die("Not a file: $_") for grep {!-f} @files;