open my $FH, "/tmp/test.txt" || die "Failed to open /tmp/test.txt: $!\ +n";
BTW: This statement from the OP has a bug. The relatively high precedence of the || logical-or operator means that the truth value of the file name is logical-ored with the value returned by the die built-in (which, of course, never returns). Because a file name will almost always be true ('0' is the only one I can think of that would be false), the short-circuiting of the || operator means that die will almost never be evaluated and the statement will seem to 'work'. The or logical operator has a sufficiently low precedence to make this really work. (See perlop.)
So the correct version of this idiom is something like
open my $FH, "/tmp/test.txt" or die "Failed to open /tmp/test.txt: $!\n";
or (less elegantly IMHO)
open(my $FH, "/tmp/test.txt") || die "Failed to open /tmp/test.txt: $!\n";
Update: This bug is hinted at by the fact that in the deparse of toolic's reply, the die sub-expression has vanished! This is because the literal file name "/tmp/test.txt" is always true, so the compiler softly and silently optimizes the die away.
In reply to Re: $_[0] fails for file handle?
by AnomalousMonk
in thread $_[0] fails for file handle?
by sophate
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |