Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Pre vs Post Incrementing variables

by DrHyde (Prior)
on Sep 15, 2010 at 10:16 UTC ( [id://860182]=note: print w/replies, xml ) Need Help??


in reply to Pre vs Post Incrementing variables

In this:

my $i = 0; my ($j, $k) = (++$i, ++$i); print "\$j: $j\t\$k: $k\n"; $j: 2 $k: 2

It first evaluates the ++es (because it's a pre-increment, so it happens before anything else), so $i = 2. Then, it builds a list of (2, 2), and finally assigns that list to ($j, $k).

In this:

my $i = 0; my ($j, $k) = ($i++, $i++); print "\$j: $j\t\$k: $k\n"; $j: 0 $k: 1

Because it's a post-increment, the ++es get evaluated *after* $i is "evaluated" to build the list. So the first $i is evaluated, it's 0, so 0 goes into the list. Then the ++ happens, setting $i = 1. Then the next $i is evaluated, putting 1 into the list, and finally $i is incremented again. End result, a list of (0, 1) is assigned to ($j, $k).

But as others have pointed out, having multiple ++es and --es in a statement is Bad Juju. What I've explained is what happens in current perls, but I wouldn't trust it to work the same in the next point release of perl 5, let alone in perl 6. See perlop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2026-02-11 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.