in reply to $i=$i++

After being baffled for minutes on end about the seeming madness of ++$X + $X++ being 3, I divined the reason:
  1. $X is 0
  2. ++$X increments $X's value, and returns the variable $X (not its value)
  3. $X++ return's $X's value and then increments it, meaning $X is now 2
  4. 2 + 1 = 3
This can be verified by the Perl debugger:
perl -de0 main::(-e:1): 0 DB<1> x $N = 0; ++$N, $N++ 0 2 1 1

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: $i=$i++
by Elian (Parson) on Apr 30, 2002 at 16:18 UTC
    Right. The ultimate reason is that perl does everything by reference internally, not by value. We don't deal with real numbers or strings, we deal with pointers to perl variables. Since you're reusing those pointers multiple times, and affecting them multiple times as well, it's up in the air what the ultimate value will be. (Depends on how perl builds and evaluates the expression)