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

Re: improving $! usability (was: Re: 1b7a0411f8b7cab [...] seterrno)

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
June 20, 2009 12:30
Subject:
Re: improving $! usability (was: Re: 1b7a0411f8b7cab [...] seterrno)
Message ID:
4A3D38CA.9050702@pobox.com
Stepan Kasal wrote:
> Hello,
> 
> On Thu, Jun 18, 2009 at 10:57:10PM -0500, Craig A. Berry wrote:
>> On Thu, Jun 18, 2009 at 9:03 AM, Stepan Kasal<skasal@redhat.com> wrote:
>>> I thought that checking $! immediately after the last read is the
>>> way.
>> But only if the last read does not hit EOF.  In other words, if
>> readline() returns undef AND you are not at EOF, then $! is likely to
>> contain something useful.  Otherwise who knows.  The docs were
>> clarified quite recently:
>>
>> <http://perl5.git.perl.org/perl.git/commitdiff/d947e7a>
> 
> than the following quote from perlfunc might need to be corrected as
> well:
> "Practical hint: you almost never need to use C<eof> in Perl, because the
> input operators typically return C<undef> when they run out of data, or if
> there was an error."
> 
> More seriously speaking, the eof flag might mask an actual error.
> 
> Perhaps the perl interpreter should make sure that eof is unset if an
> error appears, so that we can look at it.
> 
> Or should we rather introduce ferror to perl?

It might be nice to have errors attached to filehandles.  That pushes use more
towards making real IO objects.

But it only helps when there's a filehandle.  unlink(), chdir(), mkdir(),
etc... don't have a filehandle.


> Wouldn't that be
> against the plan to have perl io "a cleaned up version of stdio"?

Is that the plan?


> Better yet, wouldn't it be possible to make sure that $! is not set
> by a successful io operation?
> Either It could be cleared after every successful io operation.
> (Or the value could be saved before the io call and then restored if
> the io was successful, so that backward compatibility with broken
> perl code is maintained.)

YES!

In the earlier errno argument, somebody lamented a possible performance hit
but we already wrap every IO operation anyway.  Then I won't have the real
performance hit of writing ridiculous code like:

                   for (;;) {
                       undef $!;
                       unless (defined( $line = <> )) {
                           last if eof;
                           die $!;
                       }
                       # ...
                   }


-- 
29. The Irish MPs are not after "Me frosted lucky charms".
    -- The 213 Things Skippy Is No Longer Allowed To Do In The U.S. Army
           http://skippyslist.com/list/

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