(Note: I cross-posted some of this on comp.lang.perl.moderated yesterday, but still no replies there...)

Please enlighten. What is the best-practice way to handle read errors when using <> or readline? What I'd really like is something like:
while ($data = <$fh>) { # do something with $data } if ($fh->READLINE_ERROR) { # but this method doesn't exist # handle readline error } else { # handle EOF }
but I don't think there is anything like this that will work in reality.

perldoc readline offers something like:
while (1) { undef $!; # doesn't really undefine $! $data = readline $fh; # or $fh->readline if (!defined $data) { last unless $!; # EOF # handle readline error } # do something with $data }
This sort of works. In fact, if use warnings is in effect, then the readline will report an error on stderr, at least for some kind of errors, in addition to setting $!. But, more importantly, this code is kind of ugly. And if I have to trap warnings in some way, it will get even uglier. Is this really the best or only way to write robust line-at-a-time input handling?
I've found something like the following in Stein's "Network Programming with Perl":
undef $!; while ($data = <$fh>) { # do something with $data } if (defined $!) { # handle readline error }
This is almost identical to my preferred way, but seems buggy because:
  1. undef $! doesn't really undefine $! because defined($!) returns true after undef($!) !!
  2. if the "do something" part sets $! then this will fail
  3. warnings from <readline> sent to stderr will have to be handled in some way
The Camel 3rd edition doesn't seem to mention readline errors. Surprisingly, Perl Best Practices doesn't mention readline error handling either.

What about using $fh->eof or $fh->error? They don't seem to work quite like I want to handle this case. But they don't seem to be documented well enough for me to tell for sure.

In reply to Best way to handle readline errors? by jrw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.