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

So what would you expect the following to print?

use strict; my @array = (0, 0, 0); my $x = 0; $array[++$x] = $x++; print "@array\n"; $x = 0; $array[++$x] = $x; print "@array\n"; $x = 0; $array[++$x] = ++$x; print "@array\n";

Perl reduces RSI - it saves typing

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

    What is your point?

    If your point was to contradict betterworld, it doesn't. While it may not *appear* to be the case, the RHS of the assignment is evaluated before the LHS in every instance.

    If your point is that it can get very complex very fast, I agree.

      In the last case ($array[++$x] = ++$x;) both $x pre-increments are evaluated before the content of $x is returned as the value for the RHS (2 is assigned) so at least part of the LHS calculation is performed before the RHS is fully evaluated.


      Perl reduces RSI - it saves typing

        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; }