develooper Front page | perl.perl5.porters | Postings from March 2006

Re: [perl #38790] readdir() only returns one result when used with Fatal.pm

Thread Previous | Thread Next
From:
Yitzchak Scott-Thoennes
Date:
March 23, 2006 05:31
Subject:
Re: [perl #38790] readdir() only returns one result when used with Fatal.pm
Message ID:
20060323133046.GA2184@efn.org
On Thu, Mar 23, 2006 at 02:03:57PM +0100, Rafael Garcia-Suarez wrote:
> Tom Hukins (via RT) wrote:
> > Fatal.pm and readdir() do not play well together.  Run the following
> > script in a directory containing several files.  It only prints out '.'.
> > Then comment out the 'use Fatal' line and run the script again.  It now
> > prints out all the files in the directory.
> > 
> > #!/usr/bin/perl
> > 
> > use strict;
> > use warnings;
> > 
> > use Fatal qw(readdir);
> > 
> > my $start_dir = '.';
> > opendir(my $dir, $start_dir);
> > my @subdir = readdir $dir;
> > closedir $dir;
> > print "@subdir\n";
> 
> Fatal will replace readdir() by this function :
> 
>     sub (*) {
> 	    local($", $!) = (', ', 0);
> 	    CORE::readdir($_[0]) || croak "Can't readdir(@_): $!";
>     }
> 
> As you see, this construct forces readdir to be called in scalar
> context; which is why it returns only one element, as documented
> in perlfunc.
> 
> We could add a wantarray() before that, but that would alter the proper
> behaviour of other weird code like this :
> 
>     use Fatal qw(open);
>     my @useless_array = open my $fh, "non-existent-file";
> 
> Other ideas ?

*Is* there any way to detect a failed list-context readdir()?  If not,
Fatal should continue to do what it is doing and the limitation should
be documented.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About