On Mon, 25 Nov 2002 21:16:10 -0000,
[EMAIL PROTECTED] wrote:
>
>Can anyone explain why these print different output?
>
>my ($x,$y);
>print $x++ , ++$x , $x++ ,"\n";  # prints 032  
>print $y++ . ++$y . $y++ ,"\n";  # prints 022 

$x++ and ++$x are fundamentally different beasts.
$x++ increments $x after creating a temp var to return $x's orig value.
++$x increments and returns the variable $x (*not* its value).

The first line is equivalent to:
$x = 3;
print(0, $x, 2);

The second is:
print(($y++ . ++$y) . $y++)

where ($y++ . ++$y) is like:
$y = 2; "0"."$y"

>I was going to suggest that someone that the ++prefix was bugged only over
>the "," operator until I saw this:
>
>my ($x,$y);
>print ++$x , ++$x , ++$x , ++$x , ++$x , ++$x ,"\n"; # prints 666666
>print ++$y . ++$y . ++$y . ++$y . ++$y . ++$y ,"\n"; # prints 223456 (and
>not 123456!)
>
>Shouldn't the super-high precedence of ++ prevent there from being any
>distinction between the "." and the "," operator?

The first places 6 instances of $x on the stack, incrementing it 6 times as
a side effect.  When it is printed, it prints 666666.

For the second, perhaps parentheses make it clearer:
   print((((((++$y . ++$y) . ++$y) . ++$y) . ++$y) . ++$y))

The first . places two instances of $y on the stack, also incrementing
it twice.  These are then concatenated as "22".  Then $y is
alternately incremented and concatenated four more times, giving
223456.

Reply via email to