Subject: | File handle $fh needs closing after file handle has been read... |
Date: | Sun, 02 Mar 2008 18:19:21 +0100 |
To: | bug-Config-File [...] rt.cpan.org |
From: | Peter Valdemar Mørch <swp5jhu02 [...] sneakemail.com> |
Inserting this line:
$fh->close();
after the while() loop closes the file after reading.
Reproducing the bug: Open a particular config file with
read_config_file() on windows using wperl.exe (not perl.exe!) from
strawberry perl. Try to open the config file with gvim on windows, while
the perl process is still running. It will open in read-only mode,
because another process is accessing the file (the wperl.exe process
from before). If I insert the $fh->close() above, the config file opens
in r/w mode, as I would have expected.
I'm wondering why the $fh is not closed when it goes out of scope, but
from experimenting I can see that it does make a difference whether the
explicit $fh->close() is there or not. (So maybe the garbage collector
does close the file in due course, but that isn't fast enough.
read_config_file() leaves the config file open in the mean time.)
Workaround:
my $configFile = 'c:/my.conf';
open my $configHandle, '<', $configFile
or die "Couldn't open $configFile";
my $configContents = join('', <$configHandle>);
close $configHandle;
# You can open a filehandle to a reference to a scalar.
# See perldoc -f open
my $config = read_config_file( \$configContents );
bla bla bla
Peter
--
Peter Valdemar Mørch
http://www.morch.com