saurabh.hirani has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys,
A friend of mine had just asked an interesting question about increment, decrement operators which led to my query.
What should be the output ofperl -le '$a = 10; $a++ + $a--'
It should be 21 instead of 20 because if I were to think how the calculation would be done - convert to postfix, push operands on stack till operator is encountered, when operator encountered, pop out 2 operands and perform operation and push result onto stack:
A similar explanation can be done for:
perl -le '$a = 10; $a++ + $a--'
perl -le '$a = 10; $a++ + $a-- + $a-- + $a--'
Can you guys please tell me how would the postfix calculation progress in case of
andperl -le '$a = 10; print $a++ + ++$a;'
perl -le '$a = 10; print ++$a + $a++;'
I don't know why the result of ++$a + $a++ turned out to be 23.
Thanks for going through,
|
---|