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

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?

Replies are listed 'Best First'.
Re^7: Order of evaluation/interpolation of references
by LanX (Saint) on Mar 08, 2012 at 01:48 UTC
    > 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

      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

        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

Re^7: Order of evaluation/interpolation of references
by Jenda (Abbot) on Mar 08, 2012 at 15:24 UTC

    What was it they said about premature optimization?

    The thing that's debatable about the array example is whether anything like that should ever appear anywhere other than an obfu competition. Though ... debatable ... debatable it ain't. It should not!

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      I'm not saying that it is elegant code -- though if we put our minds to it, we could probably think of a use. But if it is legal, and it is, then it ought to work consistently.


      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?