Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Auto Increment "magic" Inquiry

by Solo (Deacon)
on Jan 04, 2007 at 21:11 UTC ( #593019=note: print w/replies, xml ) Need Help??


in reply to Auto Increment "magic" Inquiry

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.

As I read it, the doc is telling you not to rely on the behavior of your example to be the correct and consistent behavoir, though it may be consistent on your platform.

Another way to interpret it is that Perl uses the same mechanism C uses to perform this op, so however C behaves on your system, Perl should behave the same, but we still won't guarantee that. Thanks to chromatic for correcting this below.

$i = $i++ + ++$i;

Or are you looking for some explanation like: "$i begins as 0, the pre-increment comes first, making $i=1, then 0+1 is assigned to $i, then $i is post-incremented yielding 2; or is it 1+1 assigned to $i"? But I think that's exactly what the pod is telling you not to try to explain and not to use.

--Solo

--
You said you wanted to be around when I made a mistake; well, this could be it, sweetheart.

Replies are listed 'Best First'.
Re^2: Auto Increment "magic" Inquiry
by chromatic (Archbishop) on Jan 04, 2007 at 22:21 UTC
    Another way to interpret it is that Perl uses the same mechanism C uses to perform this op, so however C behaves on your system, Perl should behave the same...

    Perl doesn't use the same mechanism as C, however. Perl uses an optree, which (compiled) C doesn't do. (A C compiler may use a similar tree structure for optimizations, but it doesn't guarantee the order of operations for similar reasons, not because it uses the same mechanism.)

Re^2: Auto Increment "magic" Inquiry
by brusimm (Pilgrim) on Jan 04, 2007 at 21:53 UTC
    Solo, The second part of your post is what I am attempting to discover here. The explanation.

    Since my co-worker can seem to accurately predict the printed results, it seems to defy everything (Or is it pure luck?) I've read in the reference materials that I quoted in my posting, that are available online. That's why I'm thinking someone may have an insight as to how this can happen on my system in such a consistent fashion. If not, so be it, but I was hoping to at least have a better understanding of it on my system, as it's acting.

    Thank you Solo

      brusimm:

      It's consistent because Perl will generate the code the same way each time you compile it. However, another version of Perl may generate slightly different code for the same sequence.

      For example, suppose you have the statement $j = $i++ + $i++;. It's perfectly acceptable for perl to implement it as:

      $temp_1 = $i; $temp_2 = $i; $j = $temp_1 + $temp_2; $i = $i + 1; $i = $i + 1;
      and it's also acceptable for it to implement it like:

      $temp_1 = $i; $i = $i + 1; $temp_2 = $i; $i = $i + 1; $j = $temp_1 + $temp_2;
      (Other implementations are also possible...) So while any particular perl compiler will be consistent from run to run, and program to program, different perl compilers (version, platform, vendor, etc.) are free to do it another way.

      Your co-worker appears to know which way it's implemented on your perl compiler.

      roboticus

      Things can be entirely consistent and predictable and yet not reliable.

      What?

      When the docs say you can't rely on something, it means that the behaviour isn't part of the public interface. It's liable to change without notice.

      In fact, docs are more likely to say that about predictable things, since those are the very behaviours which users are likely to assume are reliable.

      This sort of thing is more of a problem when you do too much learning through experimenting. You can't tell whether things you learn via experimentation are defined behaviour or an accidental side-effect of implementation, so you need to check them against the docs before relying on them.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2021-08-05 02:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (44 votes). Check out past polls.

    Notices?