Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Order of evaluation/interpolation of references

by Anonymous Monk
on Mar 07, 2012 at 19:45 UTC ( #958361=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've got this code:
use strict; use warnings; { my $x = 0; sub X() { ++$x; \$x; } } print ${X()}, "\n"; print ${X()}, "\n"; print ${X()}, "\n"; print ${X()}, "\n"; print "${X()}${X()}\n"; # why does this behave print "${X()} ${X()}\n"; # differently from this?
X() provides a reference to the its $x, and $x increases from 1,2,3, etc. But funny things happen when I have two X()'s in one statement. Sometimes it evaluates the two independently, so I get two different values, but sometimes it evaluates both of them to the same value. The output is:
1 2 3 4 66 <--- why is this not 56? 7 8 <--- or why is this not 8 8?
What I can't explain is why the last two outputs have different values, since the only difference between them is the addition of a space in the string.

Replies are listed 'Best First'.
Re: Order of evaluation/interpolation of references (alias vs copy)
by tye (Sage) on Mar 07, 2012 at 20:17 UTC

    Perhaps because "${X()}${X()}" does ${X()} . ${X()} before any stringification makes a copy, so both aliases to the hidden $x are incremented again before their values are copied to the new string. While the other case turns into ( ${X()} . ' ' ) . ${X()} so the first alias to the hidden $x gets copied into a string with a trailing space before $x gets incremented again.

    Lots of surprises to discover when you do such things.

    - tye        

      I think you're right!
      lanx:/tmp$ perl -MO=Deparse,-p,-q -e 'print "${X()} ${X()}\n"' print((((${X();} . ' ') . ${X();}) . "\n")); lanx:/tmp$ perl -MO=Deparse,-p,-q -e 'print "${X()}${X()}\n"' print(((${X();} . ${X();}) . "\n"));

      Cheers Rolf

Re: Order of evaluation/interpolation of references
by JavaFan (Canon) on Mar 07, 2012 at 21:26 UTC
    So, you're doing ++ multiple times on the same variable in the same statement, and you're getting results that you don't expect.

    Time to whip out the manual!

    Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behaviour. Avoid statements like:
    $i = $i ++; print ++ $i + $i ++;
    Perl will not guarantee what the result of the above statements is.
    You're doing just that. You do something that is documented to not guarantee what the result is.

    It's fine if you go that way. Just don't ask why you're getting the results you're getting.

      you're doing ++ multiple times on the same variable in the same statement

      If that's true, and I'm not totally convinced it is, but neither can I prove otherwise, then it is a poster child for the stupidity of whole "undefined behaviour" meme.

      It effectively means that you can never call two functions in the same statement. At least without not having looked deep into the guts of them.

      And that just makes a mockery of encapsulation, modularisation, libraries ...

      Even worse as someone (was it you?) recently showed that deciding exactly what constitutes "as statement" in Perl is far from trivial.


      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?

        Not really. X() does not look the same as ${X()}. Having a function that returns a reference to a scalar is something unusual and that can't even be made to look "vanilla" in the calling code.

        Returning the same reference to the same scalar but (hopefully) having different values each time is fundamentally a broken design.

        Perl mostly makes copies of things but there are no shortage of places where Perl keeps aliases to things. So, Perl making a copy soon enough can save one from such a broken design in a lot of cases. Such may even convince one that the design is not so horrible. But it isn't hard to come up with ways to use such a broken thing where the copying doesn't happen fast enough.

        One could certainly prefer a language where copying is always done. That certainly has merits.

        And this is certainly not just an "undefined order of operations" problem. But, yes, exactly when the copying happens is a subtle interplay of a bunch of things, many of which are subject to optimization and/or the result of previous optimizations.

        Trying to exactly specify the precise order of such subtle "operations" looks like a fool's errand to me (I don't think anybody would ever succeed). Doing so would also certainly prevent the possibility of most optimizations.

        Note that the important "operation" here is the copying of a scalar which is not even an operation explicitly called out in the code. It is a implementation detail of string concatenation (which is also not explicitly coded).

        Not that I expect you to agree with any of that. I didn't reply for your benefit.

        - tye        

        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
Re: Order of evaluation/interpolation of references
by BrowserUk (Pope) on Mar 07, 2012 at 20:03 UTC

    Good question++ I don't have an answer, but an equally intriguing variation:

    C:\test>perl -E"{my$x=0; sub X{++$x;\$x}} say qq[${X()}${X()}${X()}${X +()}${X()}${X()}]" 223456 C:\test>perl -E"{my$x=0; sub X{++$x;\$x}} say qq[${X()} ${X()}${X()}${ +X()}${X()}${X()}]" 1 23456

    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?

Re: Order of evaluation/interpolation of references
by Anonymous Monk on Mar 08, 2012 at 15:01 UTC
    Be that as it may ... if you want to get work done and go home, find a simpler way of saying the same thing that DOESN'T involve "golf." You can spend all your time looking for golf-balls in the briers; I'm done for the day and I'm going to the pub.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://958361]
Approved by BrowserUk
Front-paged by BrowserUk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-12-12 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (333 votes). Check out past polls.

    Notices?