My first impression, and what I'd do in any case, is that you should change the whole logic around. It may be worth to spend some time on this, but I don't have any and I'm leaving within seconds; so, said this, you just can't use a code block like that and you may be interested in do instead. But then, you may still adopt a syntax similar to the above and gain a lot in readability like thus:
my $list;
unless (open $list, '>', $whatever) {
# ...
$NONFATALERROR = 1;
}
All in all I like logical operators for flow control very much. But one should never abuse them... | [reply] [d/l] |
I notice that quite a few people use:
my $somevar;
open $somevar, ">", "somefile" or die "unable to open: $!\n";
I have always used:
open FILE, ">", "MYFILE" or die "unable to open:$!\n";
...
close (FILE)
after reading open, I realize that when in a sub the first is example is good because you dont have to worry about closing the filehandle - it simply goes out of scope. Does the same hold true when in MAIN? Do I not have to cose the fh? Or am I missing something?
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
--Ralph Waldo Emerson
| [reply] [d/l] [select] |
Yes, Perl closes open file handles when the program exits. That is the final point at which things can go out of scope.
The real advantage of storing file handles in variables is that you don't accidentally share a file handle. With global handles, this could be a problem. By using my, the variable is limited in scope.
Even though file handles in my variables close when their scopes end, it's considered good form to manually close the handle (and check the return status). Just because Perl could do something for you doesn't mean you should let it.
Phil
| [reply] |
after reading open, I realize that when in a sub the first is example is good because you dont have to worry about closing the filehandle - it simply goes out of scope. Does the same hold true when in MAIN? Do I not have to cose the fh? Or am I missing something?
I second that, but it's not limited to subs which introduce nothing but a "lexical scope", so the keyword here is the latter. For example you may have
for (@files) { #
open my $fh, '<', $_ or
(warn "$_: $!\n"), next;
push @first_lines, scalar <$fh>;
}
Or, consider the following example script that exploits open as a cheap way to parallelize a series of possibly slow operations:
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 <net>\n" unless @ARGV == 1;
# Rudimentary check on the argument
(my $net=shift) =~ s/\.0+$/./
or die "Supply an IP of the form <#.#.#.0>\n";
print 'Begun pings at ' . localtime, "\n";
my @p;
open $p[@p], '-|', "ping -w 5 -c 5 $net$_"
or die "Couldn't start ping for $net$_: $!\n" for 1..255;
undef $/;
print map <$_>, @p;
__END__
Note:
In any case, to answer your question: yes! "lexical filhandles" are closed automatically on scope exit. But there are situations in which you may need to explicitly close them. If I don't need to, I don't; others' opinion may vary: see e.g. philcrow's reply.
Also, a filehandle can hold more than regular files, and there's stuff you still have to explicitly close. | [reply] [d/l] [select] |
| [reply] |
Let me make one suggestion that may seem obvious to someone that has not been looking at this code before (You know that phenomenon where you look at the same block of code for hours trying to find the bug and then finally go ask someone to help you out and they point out the obvious error before you finish explaining the problem?). If you have an if ( ! some_test() ), it is generally more readable to say unless ( some_test() ). So using this logic
if ( ! $NONFATALERROR == 0 ) {
...
}
becomes
unless ( $NONFATALERROR == 0 ) {
...
}
which is really
unless ( ! $NONFATALERROR ) {
...
because any non-zero value (undef evaluates to 0 in this context) is TRUE. Using the same aurgument against unless ( ! some_test() ) as we did against if ( ! some_test() ), we then get
if ( $NONFATALERROR ) {
...
}
It's nitpicking, to be sure, but minor changes can have major benefit for anyone that has to maintain the code later, even the original author.
Ivan Heffner
Sr. Software Engineer, DAS Lead
WhitePages.com, Inc.
| [reply] [d/l] [select] |