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

Re: [MacPerl] FNG



On Wed, 27 Jan 1999 16:25:35 -0500 (EST), Ronald J. Kimball wrote:

>Perl stores both the numeric and string representations of a scalar
>variable.  The active representation is whichever was used last.  Most
>operators and functions in Perl don't care which representation is active.
>As documented, however, the bitwise operators (& | ^ ~) behave differently
>depending on whether the operand(s) are numbers or strings.

Which raises the final (?) question: does a boolean failure return a
string ("") or a number (0)? 'Cos if your print it, it'll print nothing
(empty string), and if you add a number to it, it won't complain under
-w (0). So, in a way, it is both. Boy, this is confusing.

But the big numeric result that started this thread, seems to suggest
that the number has the precedence. Let's see if this is indeed the
case:

	#! perl -w
	select STDERR;
	$\ = "\n"; $, = " ";
	$cond = (1 == 2);	# false
	print "Defined:", defined($cond)?"y":"n";	# defined
	print "As string:",$cond;			# empty string
	print "As number (warning?):",$cond + 0;	# no => numeric
	print "Bitwise inverted:", ~ $cond;		# 0xFFFFFFFF
	print "Bitwise OR-red with empty string:", "" | $cond;	#warning
	$zero = 0; $empty = '';	
	print " \"\" | 0 =", "" | $zero;		# warning
	print " \"\" | \"\" =", "" | $empty;	# no warning => empty

Results:
	Defined: y
	As string: 
	As number (warning?): 0
	Bitwise inverted: 4294967295
	Argument "" isn't numeric in bit_or at test.pl line 9.
	Bitwise OR-red with empty string: 0
	Argument "" isn't numeric in bit_or at test.pl line 11.
	 "" | 0 = 0
	 "" | "" = 

Note that the warning comes just *before* the resulting printout. So 

	"" | $cond

complains about a non-numeric argument, which must be the first one: as
there is no warning when both arguments are strings, therefore, $cond is
considered as numeric. QED.

The only inconsequential thing is that when printed directly, it acts as
an empty string. Yuck. Try to make sense out of all that. ;-)

I'll take a stab: a false is stored a both a 0 and as the empty string.
Both are valid, no coercion necessary. If behaviour depends on a scalar
being a string or a number (another inconsequential thingy in Perl:
remember that the *string* "0" is FALSE), then, it's a number.

	Bart.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch