in reply to Re^4: Will "$_[0]=shift" always resolve shift first?
in thread Will "$_[0]=shift" always resolve shift first?

You will find the following code equivalent to $array[++$x] = ++$x;:

do { local @_; alias $_[0] = ++$x; # Aliases to $x alias $_[1] = $array[++$x]; $_[1] = $_[0] };

As you can see, the LHS is fully evaluated before the RHS, yes you still get the effect you say can't happen under those conditions.

If you don't have Data::Alias (like me), you can use the following code:

do { our $arg0; local *arg0 = \( ++$x ); our $arg1; local *arg1 = \( $array[++$x] ); $arg1 = $arg0; }

Replies are listed 'Best First'.
Re^6: Will "$_[0]=shift" always resolve shift first?
by GrandFather (Saint) on Sep 09, 2008 at 03:16 UTC

    Actually that doesn't seem to me to contradict anything I wrote! You show a code fragment that is completely consistent (in fact illustrates) my assertion that "part of the LHS calculation is performed before the RHS is fully evaluated".

    Besides, the under the hood machinations are unimportant. The effect and important point to note is that where the language does not contract to perform operations in any particular order, it doesn't have to and the results may be surprising. One might expect that $array[++$x] = ++$x; would be equivalent to:

    ++$x; $array[$x + 1] = $x; ++$x;

    but that is not how Perl does it (at least for version 5.8.7). What Perl appears to do is:

    ++$x; ++$x; $array[$x] = $x;

    Perl reduces RSI - it saves typing