in reply to Best way to handle readline errors?

undef $! doesn't really undefine $! because defined($!) returns true after undef($!) !!

Huh? How do you get that? Doesn't work that way for me...

perl -e 'print "Output:\n"; for $i (1..3) { print "$i: ".(defined($_)? "defined\n":"not defined\n"); $_ = ($i==1) ? "":undef }' Output: 1: not defined 2: defined 3: not defined
(perl 5.8.6 on macosx/darwin)

$_ starts out as undefined until something other than undef is assigned to it, and after that, assigning undef to it appears to have the expected result.

I would think most 5.* perl versions to behave the same in this regard, so maybe you were doing things differently?

(update: oops! Sorry, I got confused about which variable was being talked about there. Thanks to jrw for setting me straight.)

warnings from <readline> sent to stderr will have to be handled in some way

Right. Open STDERR to a scalar:

open( STDERR, ">", \$stderr_log );
It might suffice just to do that, and check the length of that scalar after each "readline" operation. (If it's not empty, do something about it; and if it's appropriate to continue, reset it to an empty string before continuing.)

Also, if readline fails and returns false, you have eof($fh) that will also return false if the readline failure was due to an error (rather than end-of-file). Are you saying that you are having some problem getting that to work the way you want? (You haven't mentioned what OS you're using, or what sorts of errors you're worried about...)

Replies are listed 'Best First'.
Re^2: Best way to handle readline errors?
by jrw (Monk) on Nov 11, 2006 at 22:04 UTC
    Graff,

    To answer your first question, try:

    perl -e 'undef $!; print "just kidding\n" if defined $!'

    This prints "just kidding" on AIX 5.2 (perl 5.8.0 and 5.005_03), cygwin (perl 5.8.7), linux 2.4.2 (perl 5.6.0), Solaris 5.9 (perl 5.6.1, 5.6.0, 5.005_03).

    What I'm looking for is general advice which allows me to add error checking to all my calls to readline or <$fh>. Here are my "requirements": a minimal amount of ugly code, relatively efficient, independent of the rest of my program (that rules out using the "send STDERR to scalar" trick). It would also be nice if the technique worked at least as far back as perl 5.005_03.

    $fh->eof is best avoided if $fh might be attached to a terminal, which in the general case it might be.