This subroutine is passed an array of file handles to close, which it closes one by one using the foreach
loop:
sub closef
{
foreach(@_) {
my $fh = shift;
close $fh;
}
}
Where all can this simple subroutine be modified to make it better?
Should I use close shift
instead of doing it in two lines?
The most concise way is to use lexical filehandles which automatically close when they go out of scope:
sub firstline {
open( my $in, shift ) && return scalar <$in>;
# no close() required
}
See perldoc perlopentut.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With