Maintainer: Adriano Ferreira <email@example.com> Date: 22 Dec 2007 Last Modified: 22 Dec 2007 Number: 15 Version: 2
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'
This is probably the most unexciting of Perl 6 meta-operators as this is well known to many programming languages since C. However as we told above, given appropriate conditions, Perl autogenerates such operators from existing ones (including user-defined), without the need for extra coding.
Infix operators may be turned into their corresponding
assignment operators by suffixing with
The newly created operators work according to the usual
A op= B; equiv to A = A op B;
which limits the application of the meta-operator
based on whether the left side (
A) can function
both as an rvalue and an lvalue simultaneously.
That discards, for instance, mutating versions
of relational operators.
The precedence of any assignment operator is forced to be the same as that of ordinary assignment, regardless of the precedence of the base operator.
Existing forms ending in
'=' may not be modified
with this meta-operator.
Note: Spaces are never allowed betwen any meta-operator and the operator being modified. Thus
$x x ='foo' # $x x (='foo') $a! == $b # probably a syntax error [ * ] # not the reduce operator for *
won't probably do what you want. The relevant lexical rule here is that operators (including modified ones) are tokens which have to be recognized by the Longest-Token rule.
$Revision: 115 $