but I don't think there is anything like this that will work in reality.while ($data = <$fh>) { # do something with $data } if ($fh->READLINE_ERROR) { # but this method doesn't exist # handle readline error } else { # handle EOF }
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?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 is almost identical to my preferred way, but seems buggy because:undef $!; while ($data = <$fh>) { # do something with $data } if (defined $!) { # handle readline error }
In reply to Best way to handle readline errors? by jrw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |