--- Abigail <[EMAIL PROTECTED]> wrote:
> On Mon, Nov 25, 2002 ... [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
>
> Because the behaviour is documented as being UNDEFINED. From the manual
> page:
>
> "++" and "--" work as in C.
>
> And in C, the behaviour is undefined. For all you know, pink monkeys
> may fly out of your nose if you do things like this.
Using that as evidence is cheating, and wrong. The paragraph in question
says:
"++" and "--" work as in C. That is, if placed before a
variable, they increment or decrement the variable before
returning the value, and if placed after, increment or
decrement the variable after returning the value.
The problem we are discussing isn't really to do with -- and ++, but
anything that causes a side effect on a variable that is reused. I
guess any object method calls could have similar behaviour.
Now, considering the fair amount of information and guessing as to
what is going on... I'm going to attempt to guess the perl
implimentation of this section - without looking at the source code.
If we call a function as follows:
func($i++, $i, $i++);
then perl has a syntax tree looking roughly like this:
func
/ | \
++ $i ++
/ \
$i $i
Preparing the function call looks like:
1. Start at the left node, can we pass that node type to a function?
If yes, then go to next node, otherwise reduce that branch.
2. Call the function with the reduced syntax tree (however you care
to do that).
So, using my simple example (and let $i = 0) the steps are:
1. Left node is ++, so we reduce this. The reduction yields 0, but
$i is now 1.
2. Middle node can be passed on.
3. Right node is ++, so we reduce this. This node = 1, and $i = 2.
4. Now, we call the function using the following tree:
func
/ | \
0 $i 1 [$i = 2]
where $i is passed as a reference. This follows Yitzchak's
line of thinking. I guess someone will have to check the source
to either prove or disprove this :)
Jonathan Paton
=====
s''! v+v+v+v+ J r e P h+h+h+h+ !s`\x21`~`g,s`^ . | ~.*``mg,$v=q.
P ! v-v-v-v- u l r e r h-h-h- !12.,@.=m`.`g;do{$.=$2.$1,$.=~s`h
E ! v+v+v+ s k e h+h+ !`2`x,$.=~s`v`31`,print$.[$v+=$.]
R ! v-v- t H a c h h- !}while/([hv])([+-])/g;print"\xA"
L ! A n o t !';$..=$1while/([^!]*)$/mg;eval$.
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com