1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-10 16:54:33 +02:00

Fixed behavior of postfix ++

The postfix ++ operator increments its argument but returns its old value.
This commit is contained in:
b-xor-a
2019-07-04 14:28:52 +03:00
committed by GitHub
parent 84cb0e8899
commit 06216f24c9

View File

@@ -247,11 +247,12 @@ concat3(|@array); #=> a, b, c
## arguments. If you really need to, you can ask for a mutable container by
## using the `is rw` trait:
sub mutate( $n is rw ) {
$n++;
$n++; # postfix ++ operator increments its argument but returns its old value
}
my $m = 42;
mutate $m; #=> 43
mutate $m; # the value is incremented but the old value is returned
#=> 42
say $m; #=> 43
## This works because we are passing the container $m to the `mutate` sub.