You are reading and modifying the same variable in the same expression where order of operand evaluation is not provided. As such, the result is undefined.

That said, for all existing versions of Perl, you'll get the same results, so we can explain what happens even if you can't count on it.

The key to understanding what happens is that $i puts $i on the stack. Not a copy of it, but $i itself. So $i..($i+=3)-1 is equivalent to $i+3 .. ($i+=3)-1, which will always be an empty range.

The following recreates the issue. If you add debug statements, you'll see range receiving arguments 7 and 6.

use feature qw( say refaliasing ); no warnings qw( experimental::refaliasing ); my @stack; sub padsv { \$stack[@stack] = \$_[0]; } sub const { \$stack[@stack] = \$_[0]; } sub add_equal { \my $rhs = \pop(@stack); \my $lhs = \pop(@stack); \my $rv = \( $lhs += $rhs ); \$stack[@stack] = \$rv } sub add { \my $rhs = \pop(@stack); \my $lhs = \pop(@stack); \my $rv = \( $lhs + $rhs ); \$stack[@stack] = \$rv } sub minus { \my $rhs = \pop(@stack); \my $lhs = \pop(@stack); \my $rv = \( $lhs - $rhs ); \$stack[@stack] = \$rv } sub range { \my $rhs = \pop(@stack); \my $lhs = \pop(@stack); #say "$lhs..$rhs"; \$stack[@stack] = \$_ for $lhs .. $rhs; } my $i = 4; # $i @stack # -- ------- padsv($i); # 4 $i padsv($i); # 4 $i,$i const(3); # 4 $i,$i,3 add_equal(); # 7 $i,7 const(1); # 7 $i,7,1 minus(); # 7 $i,6 range(); # 7 - say join ', ', @stack; # ""

In comparison, $i+0 .. ($i+=3)-1:

my $i = 4; # $i @stack # -- ------- padsv($i); # 4 $i const(0); # 4 $i,0 add(); # 4 4 padsv($i); # 4 4,$i const(3); # 4 4,$i,3 plus_equal(); # 7 4,7 const(1); # 7 4,7,1 minus(); # 7 4,6 range(); # 7 4,5,6 say join ', ', @stack; # "4, 5, 6"

In reply to Re: Strange aliasing(?) when modifying variable in range operator by ikegami
in thread Strange aliasing(?) when modifying variable in range operator by vr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.