develooper Front page | perl.perl5.porters | Postings from August 2008

Re: How to tell whether readline got an error or EOF

Ed Avis
August 2, 2008 10:17
Re: How to tell whether readline got an error or EOF
Message ID:
Tom Christiansen <tchrist <at>> writes:

>Clearly you must 
>    (1) clear errno before the getc / readline 
>and (2) use IO::Handle and inspect STDIN->error() to determine 
>        whether your undef means eof or error, and only if it does
>	say there's an error might errno be of any use to you.

Thanks.  I wasn't aware that it was sometimes necessary to assign $! =
0 explicitly.

Also I hadn't expected errno to get set on EOF, since EOF is not a
true error condition.  But if the rule is to never look at $! unless
error() returns true, that's okay.

>Something like this should work better than what you're doing:
>    use IO::Handle;
>    $! = 0;
>    unless (defined($ch = getc(STDIN))) {
>	print "STDIN has ", STDIN->error() 
>				?  "had an error: $!\n";
>				:  "hit EOF\n";
>    } 

How do error() and clearerr() work with tied filehandles?  They are
not mentioned in perltie(1).

Ed Avis <> Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About