in reply to Re^2: "readdir() attempted on invalid dirhandle" error?
in thread "readdir() attempted on invalid dirhandle" error?

But why on earth would Perl not return an error when opening (not opendiring) a directory?

As others have shown, there's nothing intrinsically invalid about open on a directory. However I think Perl is definitely capable of giving a more informative error message when you try to use a filehandle in a context that requires a directory handle. I'd recommend opening a github issue tagged as 'Wishlist' suggesting that there's an opportunity for improvement here.

Update: the issue now exists as #22394.

Replies are listed 'Best First'.
Re^4: "readdir() attempted on invalid dirhandle" error?
by ikegami (Patriarch) on Jul 11, 2024 at 13:45 UTC

    File handles and directory handles are stored in different slots of the glob. From Perl's perspective, the problem isn't that the provided handle is the wrong kinda of handle; the problem is that the provided glob didn't contain a dir handle.

      Except open and opendir doesn't let you use both slots anymore.

      5.10: Warns when warnings enabled.
      5.12: Warns by default.
      5.26: "This will be a fatal error in Perl 5.28" to the warning.
      5.28: Fatal.