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

[perl #53504] Bug in if(open(my $fh,...)) { } scoping

Thread Next
From:
Matt Sergeant
Date:
April 29, 2008 04:27
Subject:
[perl #53504] Bug in if(open(my $fh,...)) { } scoping
Message ID:
rt-3.6.HEAD-6155-1209452221-799.53504-75-0@perl.org
# New Ticket Created by  Matt Sergeant 
# Please include the string:  [perl #53504]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=53504 >


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.

(you can confirm with system(qq(ls -l /proc/$$/fd)) on Linux)

Using:

   do { my $fh;
   if (open($fh, "/etc/passwd")) {
     ...
   }
   }

Does the right thing and closes the file handle at the end of the  
scope, but that's ugly.


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