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.
In reply to Cleaning up filehandles and directoryhandles by anthski
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |