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

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

Thread Previous | Thread Next
Rafael Garcia-Suarez
April 28, 2008 00:56
Re: bug in if(open(my $fh,...))
Message ID:
2008/4/28 Matt Sergeant <>:
>  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.

I think that's known. This is mostly because we want to allow this construct:

if (my $x) { } elsif ($x) { }

so, the $x is freed after the closing bracket of the if.

A proper fix would be to add an "invisible" block around the if. That
would slow down all ifs a bit, though.

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