One of the points that came up during my talk last night was this bit of
code from the golfed version of my Pachinko JAPH:

$#s-(@f=($")x(@s='Just another Perl hacker'=~/./g))%2

@s is being set on the right-hand side of the minus operator, but it's also
being used on the left-hand side.  Yet the code works.  Wha?


Here's a simple example that shows the same behavior:

  DB<1> x $#s - (@s=qw/ 1 2 3 /)
0  '-1'

$#s is one less than scalar(@s), so -1 is the correct result.


So perl is evaluating the right-hand side before the left-hand side, right?

  DB<2> x (@t=qw/ 1 2 3 /) - $#t
0  1

Switch the terms around, and it still works!

We couldn't figure out the explanation during the meeting...


Here's another example, that hints at what is going on:

  DB<3> x ($#u+0) - (@u=qw/ 1 2 3 /)
0  '-4'

Now $#u is being evaluated as 0 rather than 2.


So here's what's happening.  When perl evaluates $#s - (@s=qw/ 1 2 3 /), it
does the following:
1. evaluate $#s
2. evaluate @s=qw/ 1 2 3 /
3. subtract
But! when it evaluates $#s, instead of saving the value of $#s, it saves a
pointer to the value of $#s.  It doesn't follow the pointer until step 3,
by which time $#s has been set to 2.

Similarly, C<print ++$x, ++$x, ++$x> prints 333 rather than 123, because
perl is saving a pointer to the value of $x rather than the actual value of
$x.


Ronald
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to