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

You're wrong about the use of $!, at least on unix.

I don't think I am wrong about $! at all, I just follow it's convention according to the documentation. If as you say $! holds the current errno , then as per perlvar, you're clearly wrong about wanting to use it to use it to determine failure of a system call. If i may quote from a section about $! in perlvar.
If used numerically, yields the current value of the C errno variable, + or in other words, if a system or library call fails, it sets this v +ariable. This means that the value of $! is meaningful only immediat +ely after a failure:
The errno variable is not cleared by successful system calls, but it is set by failing ones.

$! is definitely set after a failed system/library call, no doubt about that. The docs say it is not set to zero after success, they do not say it remains unchanged (and that's what you have to watch out for). $! is sometimes cleared after successful calls while also being set in certain other instances. Here's your own code demonstrating both possibilities.

#!/usr/bin/perl -Wl sub doit { $! = undef; open FH, shift or die "open failure: $!\n"; print "After successful open : $!"; 1 while readline FH; print "After successful read : $!"; } doit $0, 5; doit $0, 6; doit $0, undef; __END__ ___output___ ___linux___ After successful open : Inappropriate ioctl for device After successful read : After successful open : Inappropriate ioctl for device After successful read : After successful open : Inappropriate ioctl for device After successful read : $ perl -e 'print $]' 5.008008 $ uname -a Linux cond0 2.6.17-10-386 #2 Fri Oct 13 18:41:40 UTC 2006 i686 GNU/Lin +ux $ cat /etc/issue Ubuntu 6.10 \n \l ___windows___ Name "main::FH" used only once: possible typo at read.pl line 4. After successful open : After successful read : Bad file descriptor After successful open : After successful read : Bad file descriptor After successful open : After successful read : Bad file descriptor $ perl -e "print $]" 5.008006 $ ver Microsoft Windows [Version 5.2.3790] $ type c:\boot.ini | perl -ne "print $1 if /\"(.*)\"/" Windows NT 5.2(3790.srv03.SP1.rtm.050324-1447)
In order to distinguish successful and failing system calls by the use of errno alone

Please don't do that for the sakes of portability and convention, do what everyone else does, use $! only to determine the cause to a failure of a system/library call. If, in doubt, consult the documentation about this variable in perlvar, it is straight-forward and I should imagine it takes precedence over any other documentation that offers any ambiguity.



perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er