#503993 clarify readline() docs about $!

Package:
perl
Source:
perl
Description:
Larry Wall's Practical Extraction and Report Language
Submitter:
Vincent Lefevre
Date:
2010-11-06 06:48:03 UTC
Severity:
minor
#503993#5
Date:
2008-10-30 09:59:02 UTC
From:
To:
The readline function or something like <FILE> sets $! to
"Bad file descriptor" when there are no errors, as shown by
the following Perl script:

#!/usr/bin/env perl
use strict;
my $line;
do
  {
    undef $!;
    $line = <>;
    print "Error: $!\n" if $!;
    print "Line: ", defined $line ? $line : "undef\n";
  }
while (defined $line);

I get:

$ echo foo | ./readline
Error: Bad file descriptor
Line: foo
Line: undef

Note that I don't have this problem under Mac OS X.

#503993#10
Date:
2008-10-30 11:24:10 UTC
From:
To:
I don't think this is a bug. As documented in perlvar.pod,
the value $! is meaningful only immediately after a failure.
Also, errno(3) says a function that does succeed is allowed
to change errno.

#503993#15
Date:
2008-10-30 11:49:48 UTC
From:
To:
The problem with the readline function is that one cannot know
if there's a failure without looking at $! first! Note that the
example tests whether the returned value is defined, but undef
in case of end of file isn't a failure. But what if the result
is not undef (in either scalar or list context) while there's a
failure?

So, either the behavior or the documentation has to be changed.

#503993#20
Date:
2008-10-30 12:29:11 UTC
From:
To:
the result is undef, but getting an undefined result does not guarantee
there was a failure (you have to test $! additionally).

The example in 'perldoc -f readline' does this, but your example
script doesn't.

Yes, I agree the readline documentation could be more explicit about this.