469,267 Members | 887 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

Increment Operator (++) behaves different in Perl

I was testing the increment operator in Perl and found a different behavior from other languanges.

Expand|Select|Wrap|Line Numbers
  1. my $i = 1;
  2. print ++$i + ++$i . "\n";
The above code prints out the answer 6, instead of 5 as other languages would do. Is there something I am missing about perl? If I add on
Expand|Select|Wrap|Line Numbers
  1. + ++$i
any number of times, it seems to always be off by one.

Thanks,

Mark T.
May 10 '07 #1
4 19712
miller
1,089 Expert 1GB
Hello Mark,

Read and be wise:

perldoc perlop Auto-Decrement and Auto-Increment

- Miller
May 10 '07 #2
KevinADC
4,059 Expert 2GB
I don't know the underlying explanation, but this type of staments is a no-no in perl:

++$i + ++$i

which is what the link Miller posted for you will say.
May 10 '07 #3
Hey guys, thanks so much for the guidance. I really appreciate it.

Mark T.
May 11 '07 #4
miller
1,089 Expert 1GB
No problem bro,

- M
May 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.