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

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

Thread Next
Matt Sergeant
April 29, 2008 04:27
[perl #53504] Bug in if(open(my $fh,...)) { } scoping
Message ID:
# New Ticket Created by  Matt Sergeant 
# Please include the string:  [perl #53504]
# in the subject line of all future correspondence about this issue. 
# <URL: >

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  

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


   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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About