Derek B. Smith wrote:
--- "Dr.Ruud" <[EMAIL PROTECTED]> wrote:

Norbert Preining schreef:

The Perl Book says: Auto increment and decrement
work as in
C. So if I take this C program:
#include <stdio.h>
#include <stddef.h>
int main() {
         int a;
         int b;
         a=3;
         b=(++a)+(a++);
         printf("b=%d\n",b);
}
it prints b=8.

When I do the same in perl:
$a = 3;
$b = (++$a) + ($a++);
print "b=$b\n";

It prints b=9.

I checked with a friend and Java behaves like C
and prints 8.

$ perl -wle '
  $a = 3;
  $b = 0 + (++$a) + ($a++);
  print "b=$b\n";
'
b=8
:)


Lessons learned....

always initialize your variables to zero. : )


That's not initialising the variable, its, sort of, initialising the expression.

$b = 0;
$b += ++$a + $a++;

(the parentheses are irrelevant) also gives 9. This looks like a bug to me, and
v5.6 does it as well. Unless anyone can persuade me that this is correct
behaviour?

Rob

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to