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.
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.
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.
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.