in reply to Re: Testing for a directory handle
in thread Testing for a directory handle

Unfortunately using -f and -d doesn't work if someone has already opened them as handles, instead it just throws an error saying 'The dirfd function is unimplemented'

Replies are listed 'Best First'.
Re^3: Testing for a directory handle
by syphilis (Archbishop) on Jan 29, 2018 at 05:06 UTC
    The dirfd function is unimplemented

    This only happens (for me) if the handle is a dirhandle:
    C:\_32>perl -le "opendir $rd, 'special' or die $!; print 'dir' if -d $ +rd; print 'file' if -f $rd;" The dirfd function is unimplemented at -e line 1. C:\_32>perl -le "opendir RD, 'special' or die $!; print 'dir' if -d RD +; print 'file' if -f RD;" The dirfd function is unimplemented at -e line 1. C:\_32>perl -le "opendir RD, 'special' or die $!; print 'dir' if -d \* +RD; print 'file' if -f \*RD;" The dirfd function is unimplemented at -e line 1.

    With a filehandle it seems fine:
    C:\_32>perl -le "open $rd, 'try.pl' or die $!; print 'dir' if -d $rd; +print 'file' if -f $rd;" file C:\_32>perl -le "open RD, 'try.pl' or die $!; print 'dir' if -d RD; pr +int 'file' if -f RD;" file C:\_32>perl -le "open RD, 'try.pl' or die $!; print 'dir' if -d \*RD; +print 'file' if -f \*RD;" file
    Surely this behaviour with dirhandles is a serious bug that should be reported to p5p ?
    I can see nothing in the -X documentation that allows such behaviour, and I can see plenty there that disallows it.

    Cheers,
    Rob

      I will send an email to perlbug@perl.org with a link to this post

        I will send an email to perlbug@perl.org

        The error message first appears in perl-5.8.9.
        In 5.8.8 (and probably earlier) -f and -d calls on a directory inevitably returned false, without warning or error.
        So I guess that making them fatal and providing a relevant error message was just their way of dealing with what is apparently an issue specific to Windows.
        (No such problem exists on Linux.)

        So ... I'm not sure that the porters will be all that keen to do anything about this shortcoming - in which case the bug is in the documentation. We'll just have to wait and see.
        If it's not something that's going to be fixed then it's something that, along with its workaround, should be prominently documented in both the perlport and the -X docs.

        The Windows workaround to -d HANDLE that comes to my mind is that you do:
        eval{-d $handle}; if($@ =~ /dirfd function is unimplemented/) { # then you know $handle is a dirhandle } elsif($@) { die $@; # a different problem } else { # it's safe to call -X on $handle }
        But others might know of better workarounds.

        Cheers,
        Rob