moderated >> Best way to handle readline errors?

by jrw32982 » Fri, 10 Nov 2006 04:10:50 GMT

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

Similar Threads

1. Best practices for SQL error handling?

Hi all,

are there any best practices for generic SQL error handling like how
to test if a SELECT returned any values at all? I have noticed that
different DBD drivers seem to produce different return values on 
selects - DBD-Oracle returns 0E0 if successful, DBD-CSV returns 0E0
if no row selected and the number of rows selected otherwise.

Is there a good recommendation on how to handle this if I need to 
use the same code with different DBD drivers?

Best regards,
Reiner.

2. What is the best way to handle errors? - Perl

3. Best error handling mechanism?

I've read numerous articles on the internet, and I haven't come to a
clear conclusion yet:  What form of error handling is best in Perl?
I've seen try/catch with Error.pm, eval/die with Exception.pm, and just
plain old eval/die.  What's the best way to go about creating a robust
error handler?

Thanks,
Rob

4. How to test readline errors (or read errors for that matter)

5. Best ways of using a username & password?

Hi,
I'm placing a username and password form on my site but can't decide on 
the best way to do it. I've looked at
Apache Session ID
Encrypted cookies
and a couple of other things that are crap. What's the best method of 
doing it. I need them to enter a username and password and I need the 
site to remember there using it and timeout after 20 minutes of inactivity.
Is there an industry standard for doing this?

Help

Thanks

Gary Mayor

6. Signal Handling/Term::ReadLine problem in Perl

7. better readline?


I'm writing a little script where the user enters some data via keyboard.

The script in some cases can guess what the user will want to enter, but I'd
like the user to be able to override what the computer has guessed.

For example, the computer thinks the user will enter "8/2 Updated database",
but the user may want to enter "8/2 Removed links table".

Is there a way to have perl prompt the user thusly:

  Enter a date and note:  8/2 Updated database<cursor goes here>

... but then the user could backspace all the way back to 8/2 and change it
if they want?  Almost like I'm pushing something into <STDIN>, which will
then get read back out?

I'm not even sure what words to use to ask, but I hope that's clear enough??

Thanks!

- Bryan


8. Getting application ReadLine and Perl debugger ReadLine to cooperate - Perl