tneff@bfmny0.BFM.COM (Tom Neff) (07/13/90)
In article <1990Jul13.175014.1750@uvaarpa.Virginia.EDU> worley@compass.com writes: >People might be interested to note that the conditional expression in >an 'if' is a string context, which I didn't expect. The >string/numeric context difference seems to usually be unimportant, but >$! is an exception: > > die "Can't do ..." if $!; Try using the standard workaround for this sort of thing: dir "Can't do ..." if 0+$!; -- "Nature loves a vacuum. Digital \O@/ Tom Neff doesn't." -- DEC sales letter /@O\ tneff@bfmny0.BFM.COM
worley@compass.com (Dale Worley) (07/14/90)
People might be interested to note that the conditional expression in an 'if' is a string context, which I didn't expect. The string/numeric context difference seems to usually be unimportant, but $! is an exception: die "Can't do ..." if $!; does not work, since if there is no error, $! in a string context will give 'Error 0', which tests TRUE. For another example, run the program: $a = $! + 0; print "numeric is $a\n"; print "string is $!\n"; if ($!) { print "tests TRUE\n"; } else { print "tests FALSE\n"; } Dale Worley Compass, Inc. worley@compass.com -- Don't go around saying the world owes you a living. The world owes you nothing. It was here first. -- Mark Twain
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/15/90)
In article <15668@bfmny0.BFM.COM> tneff@bfmny0.BFM.COM (Tom Neff) writes: : In article <1990Jul13.175014.1750@uvaarpa.Virginia.EDU> worley@compass.com writes: : >People might be interested to note that the conditional expression in : >an 'if' is a string context, which I didn't expect. The : >string/numeric context difference seems to usually be unimportant, but : >$! is an exception: : > : > die "Can't do ..." if $!; : : Try using the standard workaround for this sort of thing: : : dir "Can't do ..." if 0+$!; That's fine as a workaround. However, I'll make it so that if errno is 0, it returns the null string in a string context. Then the boolean value will be unsurprising. Larry
worley@compass.com (Dale Worley) (07/17/90)
X-Name: Larry Wall That's fine as a workaround. However, I'll make it so that if errno is 0, it returns the null string in a string context. Then the boolean value will be unsurprising. That sounds rather winning. But what I found strange at first was that the conditional was a string context, given that I think of truth values as 0/1. Dale Worley Compass, Inc. worley@compass.com -- You don't have many suspects who are innocent of a crime. That's contradictory. If a person is innocent of a crime, then he is not a suspect. -- Edwin Meese III
lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/18/90)
In article <1990Jul16.173905.12516@uvaarpa.Virginia.EDU> worley@compass.com writes:
:
: X-Name: Larry Wall
:
: That's fine as a workaround. However, I'll make it so that if errno
: is 0, it returns the null string in a string context. Then the boolean
: value will be unsurprising.
:
: That sounds rather winning. But what I found strange at first was
: that the conditional was a string context, given that I think of truth
: values as 0/1.
It's a consequence of how str_true is defined in perl.h. It says
#define str_true(str) (Str = (str), \
(Str->str_pok ? \
((*Str->str_ptr > '0' || \
Str->str_cur > 1 || \
(Str->str_cur && *Str->str_ptr != '0')) ? 1 : 0) \
: \
(Str->str_nok ? (Str->str_u.str_nval != 0.0) : 0 ) ))
If the scalar currently has a valid string value, it chooses that in
preference to the numeric value because the floating comparison will
be slower on many machines than the integerish comparisons in the string
branch. But since $! is the only thing in Perl that returns different
canonical values depending on the string or numeric context, it doesn't
much matter that boolean contexts are actually string contexts by preference.
And I just made $! not matter. I'm sure Gilbert and Sullivan could have
turned this into a song...
Larry