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
}
I believe this works properly, but it's kind of ugly. Is this really
the best or only way? 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 $! -- defined($!) returns true
after undef($!) !!
2) if the "do something" part sets $! then this will fail
The Camel 3rd edition doesn't seem to mention readline errors. 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.
-- John Wiersba