anthski has asked for the wisdom of the Perl Monks concerning the following question:
Apologies in advance for my second question of the day, however having been a lurker of perlmonks for years, I thought I'd bite the bullet and make more use of the resources available. Twice. In one day.
My question is: how can I check whether a directory handle has been closed or not?
The reason I ask is that I've written a common module for use in my scripts which attempts to cleanup any open file or directory handles before the script exits. This means that I have methods like openFile() which pushes the filehandle into an array which I can then iterate over at a later point (i.e when cleanup() is called) to ensure that the filehandle was indeed closed.
For example:
sub openFile { my ($filename, $mode) = @_; use FileHandle; my $filehandle = FileHandle->new($filename,$mode); if (defined $filehandle) { push(@openedFileHandles, $filehandle); return $filehandle; } return 0; }
and I open my files like
my $logfile = openFile("application.log",">>");
From reading various nodes on perlmonks, I picked up that I can check if a filehandle is open/present by using fileno(), so this lets me do something like
foreach my $filehandle (@openedFileHandles) { if (fileno($filehandle)) { $filehandle->close(); } }
when I'm cleaning up.
But I get the impression that fileno() doesn't work for directory handles. If that is the case, how can I check if a directory handle is still open or present?
I'm opening directories in a similar fashion to openFile(), except using DirHandle instead of FileHandle
Any thoughts appreciated. Actually, if anyone has any suggestions on what a good cleanup module should do, or whether there is something like this already available, that'd also be extremely helpful information.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cleaning up filehandles and directoryhandles
by ikegami (Patriarch) on Aug 10, 2005 at 05:10 UTC | |
by anthski (Scribe) on Aug 10, 2005 at 07:51 UTC | |
|
Re: Cleaning up filehandles and directoryhandles
by graff (Chancellor) on Aug 10, 2005 at 01:44 UTC | |
by Tanktalus (Canon) on Aug 10, 2005 at 02:03 UTC | |
by Bob9000 (Scribe) on Aug 10, 2005 at 10:32 UTC | |
by anthski (Scribe) on Aug 10, 2005 at 07:57 UTC | |
|
Re: Cleaning up filehandles and directoryhandles
by anonymized user 468275 (Curate) on Aug 10, 2005 at 09:12 UTC |