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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |