Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Auto-increment frenzy

by BrowserUk (Patriarch)
on Aug 27, 2003 at 00:24 UTC ( [id://286909]=note: print w/replies, xml ) Need Help??


in reply to Auto-increment frenzy

Whilst the evaluation order is undefined, explaining the behaviour is fairly easy

P:\test>perl -MO=Deparse,-p $a=$b=$c=1; $a=$a++ + $a++; $b=$b++ + $b; $c=$c + $c++; print "$a $b $c\n"; ^Z ($a = ($b = ($c = 1))); ($a = (($a++) + ($a++))); ($b = (($b++) + $b)); ($c = ($c + ($c++))); print("$a $b $c\n"); - syntax OK

If you work through unwinding the parens, then you will end up with the correct result. The behaviour is even definable and consistant.

Explaining why is this way is somewhat harder:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.

Replies are listed 'Best First'.
Re: Auto-increment frenzy
by Abigail-II (Bishop) on Aug 27, 2003 at 08:08 UTC
    Whilst the evaluation order is undefined

    The behaviour is even definable and consistant

    Those two lines contradict each other.

    perl -MO=Deparse,-p only shows you precedenceorder, not evaluation order. Take for instance

    $a = $a ++ + $a ++;
    The evaluation order might be:
    • Fetch the value of $a, save a copy. (A)
    • Increment the value and store it back in $a.
    • Fetch the value of $a, save a copy. (B)
    • Increment the value and store it back in $a.
    • Add (A) and (B), store the value in $a.
    But it also might be:
    • Fetch the value of $a (A)
    • Fetch the value of $a (B)
    • Add (A) and (B), store the value in $a
    • Increment (A), store it back in $a
    • Increment (B), store it back in $b

    The order of evaluation is UNDEFINED, and therefore, the entire statement is has undefined behaviour.

    And yes, I know the argument, "If I run the program, it always returns XXX". That doesn't say anything. The order in which keys are returned from a hash is undefined as well. If you write a program that inserts the numbers 1 to 100 in a hash, in that order, and then fetches the keys, you will get the same order on each run of the program. Does that mean the order is defined? No, because that fixed order will no longer happen with 5.8.1. Then the order will differ from run to run. And it's safe to introduce that in a maintainance release, because the order in which keys are returned is undefined, even if you think you can predict it.

    Abigail

      Abigail, I think you misspoke. You are indicating that the addition may precede the increments in your second choice.

      Your earlier statement that behaviour is "undefined" is also incorrect. Only the order of evaluation is undefined.

      What is known about:

      $a + $b
      is that both sides will be evaluated before the addition. What is not defined is whether $a or $b will be evaluated first.

      In my example it doesn't matter, but if both terms use and alter the same variables then there can be side effects between the terms.

        Abigail, I think you misspoke. You are indicating that the addition may precede the increments in your second choice.

        Indeed. That may happen. Post increment means that the variable will be incremented some time between fetching the old value and the end of the statement. It doesn't say it will be incremented before the evaluation of some other operation in the same statement.

        Your earlier statement that behaviour is "undefined" is also incorrect.
        If my statement is incorrect, it must mean that the behaviour is defined. Please define that behaviour for me, quoting the relevant parts of the documentation.

        In my example it doesn't matter, but if both terms use and alter the same variables then there can be side effects between the terms.

        Exactly. Hence, undefined behaviour.

        Abigail

      Your right Abigail.

      In the scheme of how future perl releases will behave, it is undefined. and is therefore unpredictable. I wasn't disagreeing with this.

      In terms of how any given installation of perl behaves now, then Deparse can illuminate the order in which things are evaluated. And if you inspect a few similar sets of Deparse outputs it becomes predictable how other similar expressions wil be evaluated for that given installation.

      This doesn't remove the risks associated with relying upon the current behaviour, and was not meant to encourage such reliance. Only to explain that behavour and show that it isn't random. Just unwritten and subject to change.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://286909]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found