in reply to Implicit closing of files
One of the most efficient ways for Perl programmers to bring misery and suffering upon themselves and their colleagues is to write this:The advice isUsing 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.open FILE, '<', $filename or croak "Can't open '$filename': $OS_ERROR";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>
Use indirect filehandles.Perl Best Practices is a controversial book, but I don't think this section is particularly debated.open my $FILE, '<', $filename or croak "Can't open '$filename': $OS_ERROR";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Implicit closing of files
by rovf (Priest) on Jun 17, 2008 at 11:09 UTC | |
by andreas1234567 (Vicar) on Jun 17, 2008 at 11:14 UTC |