Hey all,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.