develooper Front page | perl.perl5.porters | Postings from April 2008

RE: bug in if(open(my $fh,...))

Thread Previous | Thread Next
KONOVALOV, Vadim ** CTR **
April 28, 2008 00:41
RE: bug in if(open(my $fh,...))
Message ID:
> (I did post this via perlbug and to but both appear  
> to have gone into a black hole)
> The following code:
>    if (open(my $fh, "/etc/passwd")) {
>      ...
>    }
> Leaves the file open at the exit of the scope (in fact to the end of  
> the program). But $fh is out of scope, meaning the file should be  
> closed.

May be this is because condition part of 'if' is within outer scope?
I guess that this is done for convenience - that is people could easily
write similar to your construct.

unless (open my $fh, "....") {
  die "couldn't open";

# which is similar to 
open my $fh, "..." or die "....";

# use $fh here

BTW if (condition) {...} expands to condition && do {...};


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About