Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Implicit closing of files

by andreas1234567 (Vicar)
on Jun 17, 2008 at 10:53 UTC ( [id://692465]=note: print w/replies, xml ) Need Help??


in reply to Implicit closing of files

The use of bareword filehandles vs .indirect filehandles is discussed in detail in Perl Best Practices. It says:
One of the most efficient ways for Perl programmers to bring misery and suffering upon themselves and their colleagues is to write this:
open FILE, '<', $filename or croak "Can't open '$filename': $OS_ERROR";
Using a bareword like that as a filehandle causes Perl to store the corresponding input stream descriptor in the symbol table of the current package. Specifically, the stream descriptor is stored in the symbol table entry whose name is the same as the bareword; in this case, it's *FILE. By using a bareword, the author of the previous code is effectively using a package variable to store the filehandle.

If that symbol has already been used as a filehandle anywhere else in the same package, executing this open statement will close that previous filehandle and replace it with the newly opened one. That's going to be a nasty surprise for any code that was already relying on reading input with <FILE>

The advice is
Use indirect filehandles.
open my $FILE, '<', $filename or croak "Can't open '$filename': $OS_ERROR";
Perl Best Practices is a controversial book, but I don't think this section is particularly debated.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Replies are listed 'Best First'.
Re^2: Implicit closing of files
by rovf (Priest) on Jun 17, 2008 at 11:09 UTC
    By using a bareword, the author of the previous code is effectively using a package variable to store the filehandle.

    Hmmmm.... This does not directly relate to my question as about whether or not Perl closes the local file handle properly at the end of the block, but as to the risks of bareword file handles, I think the only risk would be if I call in my block a function, which in turn would use a non-localized filehandle of the same name.

    The file handle I as using, certainly can not close another open file handle of the same name, because it is localized.

    -- 
    Ronald Fischer <ynnor@mm.st>
      The file handle I [am] using [..] is localized.
      Sorry, I overlooked that.
      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://692465]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found