Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Auto Increment "magic" Inquiry

by blokhead (Monsignor)
on Jan 04, 2007 at 22:17 UTC ( [id://593035]=note: print w/replies, xml ) Need Help??


in reply to Auto Increment "magic" Inquiry

<Update> After writing this, I realize that it makes no difference that ++$i returns an alias. But I'm too lazy to rewrite the whole thing. The main point is the order in which you evaluate the arguments to the addition operation in those big expressions.</Update> Update2: changed my example so that aliases *did* make a difference. Thanks ysth++.

Statements like yours (with multiple assignments to $i in a single statement) are confusing/unpredictable because there are aliases involved. Preincrement ++$i returns an alias to $i, while postincrement $i++ returns just a plain old value. You can see that this is the case:

sub foo { print \$_[0], $/ } foo($i); # SCALAR(0x81526f0) foo(++$i); # SCALAR(0x81526f0) foo($i++) # SCALAR(0x8151c28)
Now that you know aliases are involved, it's a simple matter of just following the execution.. No magic involved.

So let's look an example:

$i = 0; $i = ++$i + ++$i + $i++ + ++$i;
First, recall that addition is left-associative. Also, check that when evaluating an addition, perl evaluates the left argument before the right argument. At least, that's how it happens in my perl. If there is anything "undefined" about your test cases, it is this part, not the fact that you are using increment operators. So here's how perl evaluates this statement:

value of $iexpressionnext thing to evaluate
0((++$i + ++$i) + $i++) + ++$i First ++$i sets $i to 1, returns alias to $i
1((alias + ++$i) + $i++) + ++$i Second ++$i sets $i to 2, returns alias to $i
2((alias + alias) + $i++) + ++$i contents of $i + contents of $i = 4
2(     4         + $i++) + ++$i Third $i++ returns 2, sets $i to 3
3(     4       + 2) + ++$i 4 + 2 = 6
3                6        + ++$i Fourth ++$i sets $i to 4, returns alias
4                6        + alias 6 + contents of $i = 10
4                         10

Now hopefully you agree with what everyone has been saying, that assigning to a variable multiple times while using its value in the same statement is really not worth it. But I wouldn't say that it's black magic, once you understand about aliasing.

It's up to you to decide it perl will ever do anything other than left-to-right evaluation of arguments to the addition operator. I guess if perl wants to reserve the right to do something else, you'll have to respect that.

blokhead

Replies are listed 'Best First'.
Re^2: Auto Increment "magic" Inquiry
by ysth (Canon) on Jan 05, 2007 at 10:18 UTC
    After writing this, I realize that it makes no difference that ++$i returns an alias. But I'm too lazy to rewrite the whole thing. The main point is the order in which you evaluate the arguments to the addition operation in those big expressions.
    It does make a difference, but not in the example you show; to see a difference, you need to have something modify $i before the alias is used, like ++$i + $i++.

Log In?
Username:
Password:

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

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

    No recent polls found