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

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

From:
Ed Avis
Date:
August 2, 2008 10:17
Subject:
Re: How to tell whether readline got an error or EOF
Message ID:
loom.20080802T170825-535@post.gmane.org
Tom Christiansen <tchrist <at> perl.com> 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 <eda@waniasset.com>




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About