Your guess is incorrect. It's undocumented and potentially undefined, but the concatenation operator always evaluates its LHS before its RHS.

The key is that the variable is placed on the stack, not its value.


First snippet (simplified):

$ perl -MO=Concise,-exec -E'my $foo = "weird123"; say $foo . do { $foo + =~ s/123//; $foo };' ... 6 <;> nextstate(main 49 -e:1) v:%,{,469764096 7 <0> pushmark s 8 <0> padsv[$foo:47,49] s 9 <0> enter s a <;> nextstate(main 48 -e:1) v:%,{,469764096 b <0> padsv[$foo:47,49] sRM c <$> const[PV ""] s d </> subst(/"123"/) vKS e <;> nextstate(main 48 -e:1) v:%,{,469764096 f <0> padsv[$foo:47,49] s g <@> leave sKP h <2> concat[t2] sK/2 i <@> say vK ...
  1. (8) $foo is placed on the stack. (The scalar, not its value.)
  2. (d) $foo is changed.
  3. (f) $foo is placed on the stack. (The scalar, not its value.)
  4. (h) The values on the on the stack ($foo and $foo) are replaced with the result of their concatenation.

Replicating the behaviour with an inspectable stack:

use feature qw( say ); use Data::Alias qw( alias ); sub concat { my $lhs = shift; my $rhs = shift; $lhs . $rhs } { my $foo = "weird123"; alias push @_, $foo; $foo =~ s/123//; alias push @_, $foo; alias push @_, &concat; say shift(@_); }

Second snippet (simplified):

$ perl -MO=Concise,-exec -E'my $foo = "weird123"; say $foo . "" . do { + $foo =~ s/123//; $foo };' ... 6 <;> nextstate(main 49 -e:1) v:%,{,469764096 7 <0> pushmark s 8 <0> padsv[$foo:47,49] s 9 <$> const[PV ""] s a <2> concat[t2] sK/2 b <0> enter s c <;> nextstate(main 48 -e:1) v:%,{,469764096 d <0> padsv[$foo:47,49] sRM e <$> const[PV ""] s f </> subst(/"123"/) vKS g <;> nextstate(main 48 -e:1) v:%,{,469764096 h <0> padsv[$foo:47,49] s i <@> leave sKP j <2> concat[t3] sKS/2 k <@> say vK ...
  1. (8) $foo is placed on the stack. (The scalar, not its value.)
  2. (9) An empty string is placed on the stack.
  3. (a) The values on the on the stack ($foo and the new value) are replaced with the result of their concatenation.
  4. (f) $foo is changed.
  5. (h) $foo is placed on the stack. (The scalar, not its value.)
  6. (j) The values on the on the stack (the result of the earlier concatenation and $foo) are replaced with the result of their concatenation.

Replicating the behaviour with an inspectable stack:

use feature qw( say ); use Data::Alias qw( alias ); sub concat { my $lhs = shift; my $rhs = shift; $lhs . $rhs } { my $foo = "weird123"; alias push @_, $foo; alias push @_, ""; alias push @_, &concat; $foo =~ s/123//; alias push @_, $foo; alias push @_, &concat; say shift(@_); }

In reply to Re: Strange behavior by ikegami
in thread Strange behavior by redbull2012

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.