in reply to Re^8: Best way to handle readline errors?
in thread Best way to handle readline errors?

Perl does not provide a mechanism to check if readline returned an error. Wasn't this thread about finding one? My earlier posts show that I didn't find one.

Replies are listed 'Best First'.
Re^10: Best way to handle readline errors? (perl 6?)
by tye (Sage) on Nov 29, 2006 at 17:55 UTC

    It'd be nice if <> returned '' for EOF and undef for error. Even implementing that in a backward-compatible way would be tricky (a pragma that turns on that behavior within a lexical scope and teaching while(<>) to deal with it -- or making this change in Perl 6).

    - tye        

      Perl 6 pretty much solves the "errno" problem by using "interesting values of undef" to convey the reason the undef is undefined, if you happen to call something like readline in a scalar context. However, most such generators will be called using for rather than while, which will naturally distinguish end of list from exceptions that should be thrown, since the default action in Perl 6 is to throw an exception when it is determined that no one is checking for it directly, and generators used in list context don't generally want to poke exceptions into the list. Other interesting changes in Perl 6 include the fact that $! is lexically scoped, not a global alias to errno. Error values are propagated to an outer dynamic scope's $! when the decision is made whether to throw an exception or return an unthrown exception. It is hoped that this will prevent all the problems of trying to shadow C's idea of errno directly, since that's arguably one of the areas where the design of Unix was rather botched in the name of simplicity. Also, by and large, $! is treated as merely informative and useful for interpolation, but the actual underlying mechanisms do not rely on it directly, since the actual information is in whatever exception is propagating, either by explicitly being thrown, or by being returned in an interesting undef. $! is viewed as merely a handy reference to whatever the "current" error is.
      The while (defined(my $line = <FH>)) idom complicates things further.

        I don't believe so (re "further", it is, of course, part of the motivation for requiring backward compatability but is not the only such motivation). I don't see how using a lexical pragma that breaks such code is anybody's problem other than the person who broke the code by adding the pragma and not fixing the affected code to conform to the new behavior. Similarly, with Perl 6, you can use Perl 5 idioms if you tell Perl 6 that the code is "Perl 5 code" or if you know that the pragma also works in Perl 6. Though, perhaps there is already Perl 6 code in the wild that does this (no, I don't know if Perl 6 still works this way or not).

        - tye        

Re^10: Best way to handle readline errors?
by jrw (Monk) on Nov 30, 2006 at 00:51 UTC
    Actually, this thread is about the best way to handle errors, not whether or not there is a way. I assumed the readline doc is correct. In my experience on unix, it is. I can't explain why Windows perl behaves as it does, but there is a lot I can't explain about Windows.