[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] find value in array



At 8:33 PM +0200 10/18/2000, Bart Lateur wrote:
>On Wed, 18 Oct 2000 08:38:15 -0700, John W Baxter wrote:
>
>>>>  Cool. The expression $list['0 but true'] works like $list[0] !!
>>>
>  >>Perl even goes so far as to special case the string '0 but true'; when
>  >>it is converted to a number, Perl does not produce an "argument is not
>>>numeric" warning.
>>
>>That's not a special case...try
>>
>>print 0 + "77 seventy-seven" + "\n";
>>
>  >When a number is needed and Perl has a string, it converts as much of
>  >the string as possible to a number and uses that.
>
>Oh, blimey: far too many people seem to be running Perl code without
>warnings enabled. It does catch a lot of bugs, you know!
>
>	$^W = 1;
>	print 0 + "77 and the rest";
>-->
>      Argument "77 and the rest" isn't numeric in addition (+) at test.pl
>      line 3.
>      77
>
>>Hence, "twenty" == "thirty" is true, since both are 0.
>
>That's true. That's one of the typical bugs that -w will catch.
>
>The special thing is that there is NO warning for the conversion of the
>string "0 but true" to a number. The value will be zero, obviously.
>
>OTOH, it's not the only string for which this is the case: other zero
>valued strings are True as well: "0E0", "0.000", ".0", "0.", "-0". All
>zero, all true. So this exception was actualy unnecessary.

Actually, the exception WAS necessary.

Try this:

#! perl -w

print 42 + "0 but true";
print 42 + "0 and yet still true";  # equivalent in English, not in Perl

or even

print 42 + "0 But True";
$null = "\0";
print 42 + "0 but true$null";  # add trailing \0 and get a warning
print 42 + "0  but true";  # an extra space causes a warning

The reason that "0E0" doesn't generate an "isn't numeric" warning is 
that it *IS* numeric, and likewise the other zero-valued strings you 
listed. Being zero-valued is not the same thing as being non-numeric.

For instance,

print 42 + 1.0E2, "\n";
print 42 + "1.0E2", "\n";	# identical result

However, try this:

print 42 + "0x0", "\n";

On page 39 of "Programming Perl (2 ed)," you will find that 021, 
0x11, and 1_7 are valid ways of saying "seventeen" in a numeric 
literal context, but not as strings. "0x11" != 0x11

BOTTOM LINE: the exact string "0 but true" (all lower case except the 
zero, single spaces around the word "but," no leading or trailing 
characters, only an EXACT match of those ten characters) is a special 
case, and for good reason.

-- 
Linc Madison  *  San Francisco, CA  *  LincPerl@LincMad.com
NO SPAM: California Bus & Prof Code Section 17538.45 applies!

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org