in reply to Re: question 1st - undefined behaviour (perlop)
in thread question 1st - undefined behaviour

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.
  • Comment on Re^2: question 1st - undefined behaviour (perlop)

Replies are listed 'Best First'.
Re^3: question 1st - undefined behaviour (perlop)
by Anonymous Monk on Sep 23, 2013 at 23:51 UTC