in reply to Re: Re: Re: For loop problem
in thread For loop problem
print ++$x, ++$x, ++$x; #333 print ++$x, $x++, ++$x; #313
Given a left to right evaluation, to me this should produce 123 and 113 respectively.
"You see, the arguments to a function aren't copies of the variables, but rather aliases -- by modifying ONE of the $x's, you've modified the others as well"
I can accept that but what about this:my $x=3; print $x, $x=5, ++$x, $x--, $x, $x++, $x=0; #0006050 print $x++, $x=5, ++$x, $x--, $x, $x++, $x=0; #3006050
Again, 3566550 seems the reasonable output (to me) for both accounts. Instead the post-(in|de)crement operators seem to be squireling away their value of $x when it came by them in a left to right evaluation of the terms.
I can see the pattern and work with it, my question is why is it that way? What are the advantages to this type of evaluation? I don't know enough Perl to extrapolate the reasoning behind them.
As one final example consider:$x = 3; print ++$x/$x--;
Coming into the function x is 3, it gets pre-incremented (now 4), it's divided by x (still 4), x is post-decremented (to 3). Print outputs 1 (4/4) and x leaving the function is 3. This is how it _should_ happen to my mind, instead it's 3/4 as stated earlier.
Maybe it's just my logic that's skewed or I'm showing a glaring flaw in my understanding of how operations need to work internally, but it seems rather convoluted for no good reason.
-- I seek enlightenment
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Re: Re: For loop problem
by chipmunk (Parson) on Jun 04, 2001 at 06:05 UTC | |
by Arguile (Hermit) on Jun 04, 2001 at 06:41 UTC |