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

Here's a variation that doesn't use ++, and shows the same behaviour:
use 5.010; my $x; sub X { $x = shift; \$x; } say "${X(1)}${X(2)}"; say "${X(1)} ${X(2)}"; __END__ 22 1 2

Replies are listed 'Best First'.
Re^4: Order of evaluation/interpolation of references
by chromatic (Archbishop) on Mar 08, 2012 at 00:18 UTC

    If you use B::Concise and compare the optrees, the second form has two concat ops where the former only has one. Given the work the core goes through to manage the lifespans of SVs put on the Perl stack, I find this bug-like behavior unsurprising.

Re^4: Order of evaluation/interpolation of references
by BrowserUk (Patriarch) on Mar 08, 2012 at 00:21 UTC

    Which actually supports my premise rather than denies it.

    It says that this is "a single statement":say "${X(1)}${X(2)}";

    And this isn't:say "${X(1)} ${X(2)}";

    Which, by digging deep into the guts of how Perl parses the statements may be demonstrably true, but to expect users to know that is a joke.

    And even if the 'optimisation' did shave a few cycles, that it produces the wrong result, surely makes it a archetypically premature.


    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?

      I don't understand why you are so upset about this behavior.

      There are good reasons why some languages work hard on being side-effect free.

      X() has side-effects causing problems and I have problems imagining any practical application of this pattern.

      Or could you show me a reason to return the reference of the same static closure variable instead of the value itself?

      >that it produces the wrong result,

      sorry but which behavior is wrong and which is right? That's highly debatable.

      UPDATE:

      As you can see from the following code, Y() is always executed after X(), the problem is only in the timing of variable interpolation.

      and this only in conjunction of the highly disputable trick, to inject code execution by dereferencing. (IMHO allowing this is a design weakness)

      use 5.010; { my $x; sub X { say "X"; $x = shift; \$x; } sub Y { say "Y"; $x = shift; \$x; } } say "${X(1)}${Y(2)}"; say "${X(1)} ${Y(2)}";

      OUTPUT:

      X Y 22 X Y 1 2

      Cheers Rolf

        I don't understand why you are so upset about this behavior.

        I'm not upset. I just don't think it can be explained away as coder error.

        There are good reasons why some languages work hard on being side-effect free.

        Perl is not one of those languages.

        X() has side-effects causing problems and I have problems imagining any practical application of this pattern. Or could you show me a reason to return the reference of the same static closure variable instead of the value itself?

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

        Nobody thinks twice about returning a reference to an array or hash to avoid unnecessary copying:

        C:\test>perl -E"{my@x; sub X{ push@x,'some interesting value';;\@x}}; +say qq[@{X()}@{X()}]" some interesting valuesome interesting value some interesting value

        which behavior is wrong and which is right?

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


        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?