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

Well, if you ran my test program (see above) on unix and got different results than I did, then I will have to defer to an observable fact that different versions of perl handle this differently. But on all the unix machines I've tested this on, it is 100% consistent (I will concede that Windows perl handles this differently).

Plus, the docs for $! state "after a failure". There is no definition for "failure" given, but I assume it means after readline returns undef, which it does on both EOF and I/O error. Therefore, it is valid to check the value of $! in the case under consideration, since readline has returned undef.

Could you please post the output of "uname -a" on the version of unix where you ran my test program and the version of perl you used to run it? Here are my results for a variety of perl versions and unices, plus Windows XP. The only version that displays the strange behavior of modifying $! on EOF is Windows XP.

AIX 5.2 This is perl, version 5.005_03 built for aix n=5 s=I/O error n=6 s=No such device or address n=0 s= This is perl, v5.8.0 built for aix-thread-multi n=5 s=I/O error n=6 s=No such device or address n=0 s= AIX 5.3 This is perl, v5.8.2 built for aix-thread-multi n=5 s=I/O error n=6 s=No such device or address n=0 s= SunOS 5.8 This is perl, version 5.005_03 built for sun4-solaris n=5 s=I/O error n=6 s=No such device or address n=0 s= This is perl, v5.6.0 built for sun4-solaris n=5 s=I/O error n=6 s=No such device or address n=0 s= SunOS 5.9 This is perl, version 5.005_03 built for sun4-solaris n=5 s=I/O error n=6 s=No such device or address n=0 s= This is perl, v5.6.0 built for sun4-solaris n=5 s=I/O error n=6 s=No such device or address n=0 s= This is perl, v5.6.1 built for sun4-solaris-64int n=5 s=I/O error n=6 s=No such device or address n=0 s= Linux 2.4.2-2 This is perl, v5.6.0 built for i386-linux n=5 s=Input/output error n=6 s=No such device or address n=0 s= Linux 2.4.21-47.0.1.ELsmp This is perl, v5.8.0 built for i386-linux-thread-multi n=5 s=Input/output error n=6 s=No such device or address n=0 s= CYGWIN_NT-5.1 1.5.19(0.150/4/2) This is perl, v5.8.7 built for cygwin-thread-multi-64int n=5 s=Input/Output error n=6 s=No such device or address n=0 s= Windows XP This is perl, v5.8.7 built for MSWin32-x86-multi-thread n=0 s= n=0 s= n=0 s=

Replies are listed 'Best First'.
Re^9: Best way to handle readline errors?
by ikegami (Patriarch) on Nov 29, 2006 at 17:49 UTC

    Plus, the docs for $! state "after a failure". There is no definition for "failure" given

    The docs are clear. $! is either set or it's value is meanlingless. Both the docs for the read system call and your own observations show that $! doesn't get set on EOF.

    You could say the docs for readline override the docs for $!, but that's obviously not true.

    If you don't care about portability, and you think it's safe enough for you, it's your call. Be sure to pick a value for $! that doesn't correspond to a valid errno on your system. Zero is probably a bad pick.

    Could you please post the output of "uname -a" on the version of unix where you ran my test program

    I'm using various versions of Perl on various versions of Windows.

      My belief is that, on unix, $! mirrors errno exactly. And on unix, errno behaves the way I have described. It is modified (to a non-zero value) by failing system calls and not modified by successful ones. Therefore, on unix, setting $! to zero before making a call to a perl function which (may) make a system call is a valid way of checking for an error.

      Can anyone explain why Windows perl behaves as it does, and not like unix, in this respect?

      In any case, using readline on Windows seems like a very iffy proposition if you're doing any serious programming which requires tight error checking. I wish that weren't so, because readline certainly is convenient.