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

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

Thread Previous | Thread Next
From:
John W. Krahn
Date:
April 29, 2008 04:05
Subject:
Re: bug in if(open(my $fh,...))
Message ID:
20080429060650.1110.qmail@lists.develooper.com
Matt Sergeant wrote:
> (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.

Perl seems to think that it is closed:

$ perl -le'
if ( open my $fh, "<", "/etc/passwd" ) {
     print "opened" if fileno $fh;
     }
print "still opened" if fileno $fh;
'
opened


If it were still opened then fileno() would return true.

$ perl -le'
if ( open my $fh, "<", "/etc/passwd" ) {
     print "opened" if fileno $fh;
     }
close $fh;
0 == system "ls", "-l", "/proc/$$/fd" or die $?;
'
opened
total 0
lrwx------ 1 john john 64 2008-04-28 23:04 0 -> /dev/pts/0
lrwx------ 1 john john 64 2008-04-28 23:04 1 -> /dev/pts/0
lrwx------ 1 john john 64 2008-04-28 23:04 2 -> /dev/pts/0
lr-x------ 1 john john 64 2008-04-28 23:04 3 -> /etc/passwd


But close()ing the file doesn't seem to remove the /proc entry?


$ perl -v

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi



John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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