Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
> there's some logical explanation?

Well ... implementation details and an explicit warning not to mess with side-effects of multiple increments in the same statement.*

When calling a sub simple arguments are passed by reference which can be modified (you could call this alias) but the result of increments are passed as values .

The increments happen from left to right on the same instance, but will influence the aliases inside the sub.

DB<17> sub tst { say "@_" } DB<18> $i=0;tst $i,$i,++$i # last increment operates on fir +st two aliases 1 1 1 DB<19> $i=0;tst $i,$i,$i++ # again, increment returns value + at evaluation time 1 1 0 DB<20> $i=0;tst $i++,$i,$i++ # increments return value, but i +nfluence alias in the middle 0 2 1

Does it explain your results?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

*) from perlop#Auto-increment-and-Auto-decrement

Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behavior. Avoid statements like:
$i = $i ++; print ++ $i + $i ++;
Perl will not guarantee what the result of the above statements is.

update

sorry I just notice that you didn't have multiple increments in the same statement,² but the explanation with call by reference and value still holds:

>perl -wE "$i=1; say for $i..$i++" # 2 .. 1 (first is alias +, postinc returns 1 ) >perl -wE "$i=1; say for $i..++$i" # 2 .. 2 (first is alias +, preinc returns 2 ) 2 ...

update

²) but already mixing $i and $i++ in the same statement is discouraged!


In reply to Re: Strange aliasing(?) when modifying variable in range operator by LanX
in thread Strange aliasing(?) when modifying variable in range operator by vr

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 chilling in the Monastery: (2)
As of 2024-04-26 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found