in reply to Re^6: Order of evaluation/interpolation of references
in thread Order of evaluation/interpolation of references
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Order of evaluation/interpolation of references
by BrowserUk (Patriarch) on Mar 08, 2012 at 02:19 UTC | |
by LanX (Saint) on Mar 08, 2012 at 02:53 UTC | |
by BrowserUk (Patriarch) on Mar 08, 2012 at 03:30 UTC | |
by LanX (Saint) on Mar 08, 2012 at 12:28 UTC | |
by BrowserUk (Patriarch) on Mar 08, 2012 at 14:19 UTC | |
| |
by LanX (Saint) on Mar 08, 2012 at 02:27 UTC | |
by BrowserUk (Patriarch) on Mar 08, 2012 at 02:43 UTC | |
by LanX (Saint) on Mar 08, 2012 at 03:03 UTC |