Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Arguments are evaluated from left to right. (For me. I don't know if this is defined or undefined behaviour.) However, everything is passed by reference (or would that be "passed by alias" in Perl terminology). That explains why ++$i affected the $i. It must be this way to allow $_[0] = 4 to work.

The following is the C++ equivalent (ignoring the memory leaks). Try it and you will see similar results.

int func(int &at0, int &at1, int &at2); func(i, *(new int(++i)), *(new int(i+2)));

The following is a good example of the execution order in Perl:

my $i = 5; func($i+0, $i, ++$i, $i+0); # 5 6 6 6 for me. # ^^^^ ^^ # rvalue lvalue

From the above, you can also see the solution to your problem. You can work around the problem by converting lvalues to rvalues:

my $i = 5; func($i, ++i, $i+2); # 6 6 8 for me. ^^ lvalue my $i = 5; func($i+0, ++i, $i+2); # 5 6 8 for me. ^^^^ rvalue

Of course, you can no longer do $_[0] = 4; from within func if you apply this fix.


In reply to Re: Why is the execution order of subexpressions undefined? by ikegami
in thread Why is the execution order of subexpressions undefined? by BrowserUk

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 having an uproarious good time at the Monastery: (3)
As of 2024-04-19 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found