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

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

Thread Previous | Thread Next
From:
KONOVALOV, Vadim ** CTR **
Date:
April 28, 2008 00:41
Subject:
RE: bug in if(open(my $fh,...))
Message ID:
D16F05D0CAAA234BA2B753B80DE6100D8855A9@DEEXC1U02.de.lucent.com
> (I did post this via perlbug and to perlbug@perl.org 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 {...};

BR,
Vadim.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About