Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Pre vs Post Incrementing variables

by girarde (Friar)
on Sep 14, 2010 at 12:44 UTC ( #860102=note: print w/ replies, xml ) Need Help??


in reply to Pre vs Post Incrementing variables

My guess: the entire right hand expression is evaluated before assignments are made. Reasonable except when you want something else, but in that case, what should the rule be instead?


Comment on Re: Pre vs Post Incrementing variables
Replies are listed 'Best First'.
Re^2: Pre vs Post Incrementing variables
by ikegami (Pope) on Sep 14, 2010 at 19:26 UTC

    My guess: the entire right hand expression is evaluated before assignments are made.

    Of course it does, but it doesn't explain anything. The question can be phrased as "Why is the output of the first snippet different from the output of the following snippets in the following code?"

    $ perl -E'$i=0; ($j, $k) = (++$i, ++$i); say "j:$j k:$k";' j:2 k:2 $ perl -E'$i=1; ($j, $k) = ($i++, $i++); say "j:$j k:$k";' j:1 k:2 $ perl -E'$i=0; ($j, $k) = (++$i+0, ++$i+0); say "j:$j k:$k";' j:1 k:2

    The answer is here.

Re^2: Pre vs Post Incrementing variables
by JavaFan (Canon) on Sep 14, 2010 at 15:58 UTC
    My guess: the entire right hand expression is evaluated before assignments are made.
    Good guess. Doesn't really answer the question though, as any assignment will evaluate the RHS before doing the assignment. (If it didn't, what would it assign? Purple monkeys?)

      Pink. Purple is on Thursdays.

      --MidLifeXis

        Except at night.

      Actually, it does. Evaluating that right hand increments $i twice. This occurs before either of the assignments.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2015-07-29 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls