> Perl is not one of those languages.

Granting the freedom to use lethal weapons, doesn't mean that you should scratch your ears with a loaded gun.

> Because the string is huge and you want to avoid copying it twice.

The string is huge and manipulated twice in the same statement???

And are you sure that the temporary string created while dereferencing is cheaper then returning a copy?

> NB: there are 3 interesting values above, not four. Is that debatable?

ok in the meantime I updated the post you replied to, as you can see there this behavior is logical.

first call to X() returns 1 interesting value second call to X() returns 2 interesting values.

The problem is that string interpolation in Perl has a backdoor to allow evaluation of code in dereferencing context ... but the exact timing is not obvious!

 qq[@{X()}@{X()}]

This is effectively a poor man's templating system in many senses, especially because it's too poor to be predictable.

Without that backdoor, you'd be forced to use printf or explicit concats and this whole "problem" doesn't exist anymore.

So the only thing proven is that misusing interpolation to eval code with side-effects is a very bad idea!

use 5.010; { my $x; sub X { say "X"; $x = shift; \$x; } sub Y { say "Y"; $x = shift; \$x; } } printf "%s%s\n",${X(1)},${Y(2)}; printf "%s %s\n",${X(1)},${Y(2)}; print ${X(1)}." ".${Y(2)}; # updated

X Y 22 X Y 2 2 X Y 1 2

Cheers Rolf


In reply to Re^7: Order of evaluation/interpolation of references by LanX
in thread Order of evaluation/interpolation of references by Anonymous Monk

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.