Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: question 1st - undefined behaviour

by boftx (Deacon)
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]# ./ x: 18 y: 20
On time, cheap, compliant with final specs. Pick two.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055402]
Discipulus there are none so deaf as those who will not hear..
Discipulus who do not want to ear
[Corion]: marto: No, I haven't seen anything either and I'm not sure if learning-by- osmosis will work here
[marto]: I've experienced the behavior LanX described, outsourcing really opens your eyes to the situation :)
[Discipulus]: ;=)

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2017-03-30 07:08 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.