FAQ
Perlop:

...
Auto-increment and Auto-decrement
"++" and "--" work as in C. That is, if placed before a variable, they increment or decrement the variable by one before returning the value, and if placed after, increment or decrement after returning the value.
     $i = 0; $j = 0;
     print $i++; # prints 0
     print ++$j; # prints 1
Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behavior. Avoid statements like:
     $i = $i ++;
     print ++ $i + $i ++;
Perl will not guarantee what the result of the above statements is.
...
On 14 Mar 2014, at 17:03, Ralph Grove wrote:

sub doit {
return ++$g ;
}

$g = 0;
my $sum = $g + doit() + doit();
print "$sum \n";

$g = 0;
$sum = doit() + $g + doit();
print "$sum \n";

$g = 0;
$sum = doit() + doit() + $g;
print "$sum \n";

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 3 of 6 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedMar 14, '14 at 5:04p
activeMar 17, '14 at 5:42p
posts6
users6
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase