YAP6 Operator: Mutating Operators

by Adriano Ferreira

Another article of the series "Yet Another Perl 6 Operator"

We already have seen two Perl 6 meta-operators in articles of this series: namely, the
negate and the reduction operators. These are two of the five standard meta-operators of the language. What makes meta-operators interesting is how Perl automatically generates new operators from others (user-defined or builtins) with some straightforward semantics derived from the transformation of the base operators.

This time, we approach mutating operators, which are a shortcut for typical assignments where the assignment target and the first operand are the same variable.

my $s = 'foo';
$s x= 3; # $a = 'foofoofoo'

my $x;
$x //= 'default'; # $x = 'default'


Ed Avis
2008-01-27 07:46:23
Can you do this with user-defined functions too? Or must you do special magic to register your function as an operator first?