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 06:22
Re: How to tell whether readline got an error or EOF
Message ID:
Ed Avis <eda <at>> writes:

>    my $self = shift;
>    my $underlying_fh = $self->{fh};
>    my $line = <$underlying_fh>;
>    if (not defined $line) {
>        # Either end of file, or reading failed with an error.
>        # How do I tell which is which?
>        #
>    }

>I note that getc() has the same problem: on error it returns undef and sets $!,
>on EOF it returns error and leaves $! with the same value it had before.

OK, I see that I managed to thoroughly confuse myself and what I posted is not
particularly coherent or correct :-(.

I would still like to ask the same questions though:

>% echo x | perl -e 'getc; print $!; print 0; open FH, "nonexistent"; getc; print
>$!; print 0; getc; print $!'
>0No such file or directory0Bad file descriptor
>The first two getc calls succeed because echo prints two characters (x and
>newline), but the second of them, even though it succeeded, leaves $! with the
>leftover error code from the open() call earlier.  The third getc sets $! to
>'Bad file descriptor'.  Is that the magic code for end of file?

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