As stated in perlstyle, it is very important that the
debugging message includes all of the important
information, including the filename. (And for
those who think I sound like a broken record on this,
spend a couple of hours late at night trying to figure
out which file out of several thousand accessed by a
script you have never seen broke the production run,
and then tell me that insisting on having the filename
is unreasonable...)
A few incidental points. The first is that people
seeking to slurp a file usually don't want just the first
line. And secondly if you make a habit of scoping file
handles tightly, there is no need to close them. Your
preferences may vary, but I usually don't bother. And
finally, as chip says, if you have the choice in 5.6.0+,
it is better to use the 3-arg open. See Two-arg open() considered dangerous for
details and discussion.
sub slurp_file {
my $file_name = shift;
open(my $fh, "<", $file_name) or confess("Cannot read '$file_name':
+$!");
wantarray ? <$fh> : join '', <$fh>;
}
| [reply] [d/l] |
| [reply] |