in reply to readline succeeds but sets $! = EBADF

According to the discussion of this thread, I think we can agree that you should not check $! when readline returns a defined value.

The good news is that it seems you can trust $! when readline returns undef. Thus, you can most likely follow the example of perlfunc:

$! = 0; $line = <$file>; if (!defined($line) && $!) { die "error readline: $!"; } elsif (!defined($line)) { print "reached eof\n"; } else { $line=~/(.*)/; print qq(read a line: "$1"); }

I say this because it seems that perl cleares errno when it reads eof, at least starting from version 5.8.1. Try this:

echo $'three\nshort\nlines' > a; perl -we 'open $f, "<", "a"; for(0..3) { $!=10000; $l = <$f>; $s = $! +; warn +(defined($l)?$l=~/(.*)/&&qq/"$1"/:"undef"), " $s\n"; }'
You should get something like
"three" Bad file descriptor "short" Unknown error 10000 "lines" Unknown error 10000 undef undef
The error codes after the first three reads can be anything (so you might get a different error), but note that the error is indeed cleared when eof is read.

I've tried the same with perl 5.8.0, and have seen that it does not clear $!. This is because clearing $! is done in the PerlIOUnix_read funciton (perlio.c), and that function has changed between 5.8.0 and 5.8.1 as tye has noted.

I am still not sure whether perl always clears errno on eof or only in some conditions (it seems likely that it happens only with some kind of filehandles), so you shouldn't take granted what I've just said above.