Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: question 1st - undefined behaviour

by boftx (Chaplain)
on Sep 23, 2013 at 23:23 UTC ( #1055402=note: print w/ replies, xml ) Need Help??


in reply to question 1st - undefined behaviour

As someone else already mentioned, you really should take a look at just exactly what the pre- and post-increment operators are doing and when.

Also, and even more importantly, you have made a major error in thinking that your code snippet from your comment above ($x = ++$x + $x++ + $x) is the same as in your OP, which was $x = $x + ++$x + $x++. They are entirely different.

Here is a simple program to demonstrate the point:

#!/usr/bin/perl use strict; use warnings; my $x = 5; $x = $x + ++$x + $x++; print "x: $x\n"; my $y = 5; # avoid any contamination from first group. $y = ++$y + $y++ + $y; print "y: $y\n"; exit; __END__ Output: [~/perl/test]# ./prepost.pl x: 18 y: 20
On time, cheap, compliant with final specs. Pick two.


Comment on Re: question 1st - undefined behaviour
Select or Download Code
Re^2: question 1st - undefined behaviour
by rumos2 (Novice) on Sep 23, 2013 at 23:41 UTC
    -we post it in close time))
    I understand that,
    $x=$x + ++$x + $x++ and
    $x=++$x + $x++ $x
    entirely different, I've just want to understand:
    1. does $x++ return an object copy before incrementing it's value?
    2. does ++$x return original object, but not value of $x after $x incrementation ?
    3. why in first example ($x + ++$x + $x++ ) first $x is used as a copy of $x for the moment when it's not been changed, and in second example (++$x + $x++ + $x) we get not copy of $x but original $x.

    And I don't seeking for
    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. I want to understand, and search for any document or c++-source, which describe what exactly happens here and why.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-07-22 08:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (106 votes), past polls