Good day Perl monks

I was wondering if it was possible to test if a variable was a folder handle. I could not find any documentation or forum posts so far. I have written some code which works as shown below. I was wondering if there was a better way to perform the folder testing section as my code seems inelegant.

Also, I am aware that the file testing section could be improved but there is already plenty of comment on that in the following posts

http://www.perlmonks.org/?node_id=980665 Best way to check if something is a file handle?

https://stackoverflow.com/questions/3214647/what-is-the-best-way-to-determine-if-a-scalar-holds-a-filehandle

https://stackoverflow.com/questions/3807231/how-can-i-test-if-i-can-write-to-a-filehandle/4200474#4200474

#!perl use strict; use warnings; use Data::Dumper; use File::Temp qw(tempfile tempdir); use Scalar::Util qw(looks_like_number openhandle); my $fh = tempfile(); #my $dh = tempdir(); opendir(my $dh, '.') || die "Can't opendir: $!"; my $string = 'path\to\file\name'; print Dumper handleRef($fh); print Dumper handleRef($dh); print Dumper handleRef($string); sub handleRef { my ($handle) = @_; my ($fh, $dataTypeHash) = @_; # Folder test (may be possible to improve)... my $folderType = eval { no warnings; telldir ($fh); }; if (looks_like_number($folderType)){ $folderType = 1; } else { $folderType = 0; } return 'FOLDERHANDLE' if ($folderType); # File test... my $handleType = openhandle($handle); return 'FILEHANDLE' if (openhandle($handle)); # Everything else... return ref \$fh; }

In reply to 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.