Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Autoincrement operator precedence difference between C and Perl

by blokhead (Monsignor)
on Oct 09, 2007 at 13:12 UTC ( #643679=note: print w/replies, xml ) Need Help??

in reply to Autoincrement operator precedence difference between C and Perl

I'm surprised everyone is attributing the weird results to the order in which Perl operators evaluate their arguments. Perl doesn't have a weird evaluation order. It does things left-to-right as you would expect. Even if it did, it wouldn't sufficiently describe the outputs above. The order in which arguments are evaluated is not specified as a feature, sure, but it's not like Perl is trying to be smart and do things in different orders each time. It reserves the right to do so, but as far as I know it does not. Also, I do not think it splits up the atomic operations $i++ or ++$i into smaller bits, as the C optimizer might be doing, but I don't know how to check for sure. My hunch is because the increment operator is "magic" while +=1 is not.

I can't speak to what happens in C. But the real weirdness in Perl comes from the fact that the pre-increment operator returns an alias to $i (post-increment returns a plain old value). So in some of these examples, Perl evaluates the first ++$i, which returns an alias. The second increment operator changes $i. Because the first value is an alias to $i, its value also gets changed in this step before the addition operator acts on it. That is why ++$i + ++$i returns a value that is 4 greater than $i+$i: both increment operations increase both operands.

Under this interpretation and left-to-right evaluation, you can fully describe the behavior of all of these expressions.


Replies are listed 'Best First'.
Re^2: Autoincrement operator precedence difference between C and Perl
by shmem (Chancellor) on Oct 09, 2007 at 21:49 UTC
    While your observations are most probably correct, it stands as a fact that evaluation order of multiple pre/post-increments in one statement isn't explicitly defined in Perl 5, I guess on purpose. It is an artifact of the implementation of Perl - it is not in the language specs afaik - so you shouldn't rely on it, even if all perl 5 binaries we know of stack execution in the way you describe.

    And that is more than saying "while you can use multiple pre/post-increments in a single statement, it is bad practice and should be avoided" - it is saying "it's not defined. Don't".


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2021-02-26 16:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found