G'day ns550,

If you attempt to use telldir on an invalid directory handle, you'll get a "telldir() attempted on invalid dirhandle" warning (see perldiag). You can promote that warning to an error with FATAL (see the warnings pragma). That error can now be captured and tested.

I created some test files:

$ ls -al pm_1208018_test_dir* pm_1208018_test_dir: total 0 drwxr-xr-x 3 ken staff 102 Jan 30 07:08 . drwxr-xr-x 20 ken staff 680 Jan 30 07:43 .. -rw-r--r-- 1 ken staff 0 Jan 30 07:08 pm_1208018_test_file pm_1208018_test_dir_closed: total 0 drwxr-xr-x 2 ken staff 68 Jan 30 07:40 . drwxr-xr-x 20 ken staff 680 Jan 30 07:43 .. pm_1208018_test_dir_empty: total 0 drwxr-xr-x 2 ken staff 68 Jan 30 07:29 . drwxr-xr-x 20 ken staff 680 Jan 30 07:43 ..

I used a standard alias of mine:

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -E'

I started with a simple, one-liner test. I then added more and more tests: the result wrapped around four lines. Here it is; expanded for ease of viewing. I've also annotated the output in situ.

$ perle ' open my $fh, "<", "pm_1208018_test_dir/pm_1208018_test_file"; opendir(my $dh, "pm_1208018_test_dir"); opendir(my $eh, "pm_1208018_test_dir_empty"); opendir(my $ch, "pm_1208018_test_dir_closed"); closedir $ch; my $nh = "42"; my $uh; my %h = (fh => $fh, dh => $dh, nh => $nh, eh => $eh, uh => $uh, ch + => $ch); for (keys %h) { use warnings FATAL => "io"; eval { my $x = telldir $h{$_}; say "$_ is a dirhandle"; 1 } or say "$_ is not a dirhandle" } ' fh is not a dirhandle # fh = filehandle nh is not a dirhandle # nh = not a handle (just the number 42) eh is a dirhandle # eh = dirhandle to "pm_1208018_test_dir_empty +" dh is a dirhandle # dh = dirhandle to "pm_1208018_test_dir" ch is not a dirhandle # ch = closed handle uh is not a dirhandle # uh = undefined handle (declared but uninitia +lised)

[Note that "pm_1208018_test_dir_empty" isn't technically empty. On *nix machines it shows the "." and ".." entries. I'm not sure how that works on MSWin. I've left it in as a potential edge case.]

— Ken


In reply to Re: Testing for a directory handle by kcott
in thread Testing for a directory handle by ns550

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.