in reply to Re^6: Order of evaluation/interpolation of references
in thread Order of evaluation/interpolation of references

> 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

Replies are listed 'Best First'.
Re^8: Order of evaluation/interpolation of references
by BrowserUk (Patriarch) on Mar 08, 2012 at 02:19 UTC
    So the only thing proven is that misusing interpolation to eval code with side-effects is a very bad idea!

    Maybe, but not illegal. The result is therefore a bug.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      And for me it's still not clear which order of interpolation should be named correct in your opinion.

      For me it's intuitively natural to think that "$x$y$z" should act like join("",$x,$y,$z) .

      But print join ("",${X(1)},${Y(2)}); prints

      X Y 22

      because it's well defined that all arguments are evaluated before they are passed to a function.

      So the whole point of this ill idea of returning the same reference with different side effects could only work because interpolation was inconsistent from the beginning.

      Cheers Rolf

        it's well defined that all arguments are evaluated before they are passed to a function.

        If this is right:

        C:\test>perl -E"$\"=''; {my@x; sub X{ push@x,'x';;\@x}}; say qq[@{X()} +@{X()}]" xxx

        Then this is wrong:

        C:\test>perl -E"{my$x; sub X{ $x.='x';\$x}}; say qq[${X()}${X()}]" xxxx

        And vice versa.

        From your other post:

        it's a clear violation ...

        Of what? Your sensibilities? Do you think the same about print "The price is: $obj->tostring()\n";?

        Saying you don't like it, is not the same as it being illegal. And it has always been legal, so it should be consistent.

        From my understanding, interpolating code references into strings is not just legal in Perl6, but further enhanced.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

      The bug is in the design, and I doubt that such an ugly templating system was ever intended, it has look and feel of an accident.

      A real templating system in the core language and PHP wouldn't even exist today.

      I would appreciate if future strictures would kill this possibility in favor of such real templating.

      Cheers Rolf

        such an ugly templating system

        I see nothing in the OP that mentions "templating". I think you assume far, far to much.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?