471,893 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

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.


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

Read and be wise:

perldoc perlop Auto-Decrement and Auto-Increment

- Miller
May 10 '07 #2
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
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 Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.