in reply to pre v.s. post (increment/decrement)
The clearest way of describing the difference I can think of is to fully expand the syntax
print ++$i;
is equivalent to
$i = $i +1; print $i;
whereas
print $i++;
is equivalent to
print $i; $i = $i + 1;
which I think emphasises the order in which things occur. The difference only becomes material when you use the pre- or post-increment notation as part of another statement. Ie. ++$i; and $i++; as stand-alone statements have identical effect.
The difference only becomes material when the syntax is used as a part of another statement.
Which can be exemplified by
my @a = (1..10); my $i = 0; print $a[$i++] while $i < @a; 12345678910 $i = 0; print $a[++$i] while $i < @a; 2345678910 Use of uninitialized value in print at (eval 8) line 1, <> line 7.
|
|---|