in reply to question 1st - undefined behaviour

see perop
  • Comment on Re: question 1st - undefined behaviour (perlop)

Replies are listed 'Best First'.
Re^2: question 1st - undefined behaviour (perlop)
by rumos2 (Acolyte) on Sep 23, 2013 at 23:02 UTC
    if you talking about operators precedence, so we can calculate that expression by the next way:
    $x + ++$x + $x++:
    first:
    ++$x - ok, at now here returns 6.
    second:
    $x++ - ok, at now here return 6, but $x will right now is 7 so we must get 21, if anywhere we get $x object as is, -but it isn't so.
    we can get 19, if we get values, not objects - i've mean 7+6+6
    we will get
    5(value of copy of the object before any object changes)+7(at this position is value of original object $x)+6(at this position copy of $x after we make ++$x) == 18 - by logic which I describe.

    Let's make first other operation - $x++ not ++$x - we get 5 here anyway.
    then we make ++$x ($x == 6 ) - ok - we get 7 here. 7+5==12.
    for getting at now 18 as a result we must take $x==6 - look's like it is absolutely wrong.

    so..
    by operators precedence from perlop(at perldoc) we can say that we calculate it from left to right.
    why in position of $x - we get value of copy $x not for the moment of calculation, but for the moment of expression calculation begin.
Re^2: question 1st - undefined behaviour (perlop)
by rumos2 (Acolyte) on Sep 23, 2013 at 23:30 UTC
    I've mean exactly this -
    my $x=5;
    my $y=$x + ++$x;
    $y returns 12.

    here $x in first position is used as original $x and it's returns it's changed value after calculation of pre-increment.

    my $x=5;
    my $y=$x + ++$x + $x++
    $y returns 18. precedence crash(5($x) + 7(++$x is 6, but returns $x and become 7 after next step) + 6($x++, at now $x is 7, but here we get copy before increment and get 6) in this order) or $x is used as a copy at here (like after $x++) and this copy is maked BEFORE any changes.
    my $x=5;
    my $y=++$x + $x++ + $x
    $y returns 20 -
    ++$x return $x and make it's value 6; $x++ return 6 forever, but $x is 7 at now
    7 + 6 + 7 == 20 - ok.