On 2011-11-05 14:49, Ken Peng wrote:
于 2011-11-5 21:03, Shawn H Corey 写道:

You're assuming that perl clears $! at the start of your script. Never
assume a variable has a valid value unless you set it yourself. In other
words, always initialize your variables.

It did clear it, see this:

$ perl -le 'print $!;open HD,"/etc/passwd" or die;<HD>;print $!'

Bad file descriptor

I don't know where the $! get setted.

Who cares? You should only use $! *immediately* after an error.

Like with eval, don't gamble on values of global variables,
but use the return value of the 'open'.


How *not* to do it:

perl -Mstrict -wle'
{  local $!;
   print "1:", $!;
   open my $fh, "/etc/passwd" or die "ERROR: $!";
   print "2:", $!;
   <$fh>;
   print "3:", $!;
}
print "4:", $!;
'
1:
2:Inappropriate ioctl for device
3:Inappropriate ioctl for device
4:

These 2 and 3 are irrelevant intermediate issues at the system level, so just don't go there.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to