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

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

Thread Previous | Thread Next
Michael G Schwern
June 20, 2009 12:30
Re: improving $! usability (was: Re: 1b7a0411f8b7cab [...] seterrno)
Message ID:
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<> 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:
>> <>
> 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.)


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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About