develooper Front page | perl.perl5.porters | Postings from June 2013

[perl #118651] readdir() should not set $! on the first "eof" read.

Thread Next
From:
Leon Timmermans via RT
Date:
June 29, 2013 15:15
Subject:
[perl #118651] readdir() should not set $! on the first "eof" read.
Message ID:
rt-3.6.HEAD-2552-1372518945-537.118651-15-0@perl.org
On Thu Jun 27 15:17:07 2013, felipe@felipegasper.com wrote:
> readdir() should not set $! on the first empty read when there's no
> actual OS error; otherwise, it's impossible to check for errors.
> 
> The below will not print anything, which allows for due error checking
> on the <$dh>:
> 
> ===========
> > perl -MFile::Temp -E'my $node = File::Temp::tempfile(); open my $h,
>    "<", $node or die $!; local $!; 1 while <$dh>; print $!'
> ===========

That line has a bug -Mstrict would have caught ;-)

> The following, though, prints an error:
> 
> ===========
> > perl -MFile::Temp -E'my $dir = File::Temp::tempdir( CLEANUP => 1 );
>    opendir my $dh, $dir or die $!; local $!; 1 while readdir $dh;
>    print $!'
> Bad file descriptor
> ===========
> 
> ...which makes it impossible to rely on error-checking on the
>    readdir() since
> there is always an error at the normal end of iterating through the
>    directory.

You're correct, it explicitly sets errno to EBADF if it doesn't already
have a value.

Quite frankly, I'm not seeing the point of it either. It's dating back
to perl 4.0 patch 14 from 1991, a 1200 line patch that does contain
"certain perl errors should set EBADF so that $! looks better", but I
think in case of readdir it's an accident of implementation.

> There's no error set if readdir() is called in list context; however,
>    this has
> memory complications if the directory is large.

Indeed.

Leon

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=118651

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