Closed jessesheidlower closed 1 month ago
Given that the two-argument form to open
is indifferent as to the spelling of any variable provided as the first argument ...
$ perl -e 'open (my $dh, "/etc") or die "Could not open dir for reading: $!\n";'
[# no output ]
$ touch /tmp/foo
$ perl -e 'open (my $dh, "/tmp/foo") or die "Could not open dir for reading: $!\n";'
[# no output ]
$ mkdir -p /tmp/bar
$ perl -e 'open (my $dh, "/tmp/bar") or die "Could not open dir for reading: $!\n";'
[# no output ]
... open
has no way of determining what a user's intent actually was.
So that leaves only the possibility of providing a more specific error warning for readdir
.
$ perl -we 'open (my $dh, "/tmp/foo") or die "Could not open dir for reading: $!\n"; my @files = readdir($dh);'
readdir() attempted on invalid dirhandle $dh at -e line 1.
or, without warnings, but checking for success of readdir
:
$ perl -e 'open (my $dh, "/tmp/foo") or die "Could not open dir for reading: $!\n"; my @files = readdir($dh) or die($!);'
Inappropriate ioctl for device at -e line 1.
I'm not persuaded that the readdir
warning message is insufficient.
I'm not persuaded that the
readdir
warning message is insufficient.
Well we have at least one example of a user for whom it wasn't sufficient.
I'm not sure if the full glob or just the dirhandle is available at this point, but if it's possible I think it would be helpful to have a "did you use open() instead of opendir()" style message when the glob has an open filehandle.
(If we have only the dirhandle, perhaps there's a way to find back the (likely) glob from it, though I appreciate that's likely to be both harder and less canonical.)
I'm not sure if the full glob or just the dirhandle is available at this point,
On the face of it, looks like the glob should be available and it should be possible to test for IoIFP(sv)
: https://github.com/richardleach/perl5/blob/blead/pp_sys.c#L4314
Module:
Description If you mistakenly call
open
(rather thanopendir
) on a directory, and then try to read from what you thought was a directory handle, you will get an unclear error. This could be improved.(This was inspired by this Perlmonks discussion.)
Steps to Reproduce
output:
readdir() attempted on invalid dirhandle $dh at test.pl line 7.
Expected behavior A not-unreasonable expectation would be an error or warning when trying to call
open
on a directory, but it seems like there's nothing intrinsically invalid about doing this. However, when callingreaddir
on a filehandle, perhaps a better error could be reported.Perl configuration