Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

blokhead


In reply to Re: Autoincrement operator precedence difference between C and Perl by blokhead
in thread Autoincrement operator precedence difference between C and Perl by Mbk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found