[comp.lang.perl] More ** weirdness

alanm@cognos.UUCP (Alan Myrvold) (11/23/90)

In article <109161@convex.convex.com> tchrist@convex.com (Tom Christiansen) writes:
>On a Sun, these expressions return these values:
>
>    (-1) ** 1 	 1
>    (-1) ** 2 	-1
>    (-1) ** 3 	 1
>    (-1) ** 4 	-1

well for me (on a Sun), it is exactly opposite (as I would expect).
What surprises me is the need for parentheses when using '**' with 'print'.  
On both PL 36 and 41,

      print ((-1) ** 2);       produces        1
      print (-1) ** 2;         produces       -1
      print (2 ** 3);          produces        8
      print 2 ** 3;            produces        8


---
Alan Myrvold          3755 Riverside Dr.     uunet!mitel!cunews!cognos!alanm
Cognos Incorporated   P.O. Box 9707          alanm@cognos.uucp
(613) 738-1440 x5530  Ottawa, Ontario       
                      CANADA  K1G 3Z4       

tchrist@convex.COM (Tom Christiansen) (11/24/90)

In article <9077@cognos.UUCP> alanm@cognos.UUCP (Alan Myrvold) writes:
> (quoting a posting of mine)
>well for me (on a Sun), it is exactly opposite (as I would expect).

You're right -- I got the signs switched.  Sorry.  What I wanted to know
was why the Sun version seemed to be able to take the log of a negative
number, and moreover do the right thing.  I always expected perl to be
checking first to make sure it never called the log of a negative number,
and remembering to restore the sign on odd integral exponents.

>What surprises me is the need for parentheses when using '**' with 'print'.  
>On both PL 36 and 41,

That's because it looked like a function to perl.  The list operators
(like print, unlink, sort, chown, ...) are peculiar creatures at best.
They normally gobble up all their operands, as in
	func x, b, c, d;
but if they see a paren as the first token, they consider it the start 
of the argument list.  Because they're lowest in precedence (kind of),
	func x op y;	#	 like func(x op y);
	func(x) op y;	#	 like func(x) op y;
	func (x) op y;	#	 like func(x) op y;
It's that last case that bites you.  It may make some sense if you let
func==rand, but when func==print and op==+ or the like, it's a surprise
that 
	print  -2  + 2;
	print (-2) + 2;
should do such different things.  It's also when what you thought was a 
list separating comma becomes a scalar arithmetic comma and throws 
always returns the value of its RHS operand.  So beware that
    print i+2,3,4,5;	#	like print(i+2,3,4,5);
and
    print (i+2),3,4,5;	#	like print(i+2),3,4,5;
are different.


--tom