Creating operators
ErrorsCollection

Creating operators

A short tutorial on how to declare operators and create new ones.

Operators are declared by using the sub keyword followed by prefix, infix, postfix, circumfix, or postcircumfix; then a colon and the operator name in a quote construct. For (post-)circumfix operators separate the two parts by white space.

sub hello {
    say "Hello, world!";
}
 
say &hello.^name;   # OUTPUT: «Sub␤» 
hello;              # OUTPUT: «Hello, world!␤» 
 
my $s = sub ($a$b{ $a + $b };
say $s.^name;       # OUTPUT: «Sub␤» 
say $s(25);       # OUTPUT: «7␤» 
 
# Alternatively we could create a more 
# general operator to sum n numbers 
sub prefix:<Σ>*@number-list ) {
    [+@number-list
}
 
say Σ (13161); # OUTPUT: «30␤» 
 
sub infix:<:=:>$a is rw$b is rw ) {
    ($a$b= ($b$a)
}
 
my ($num$letter= ('A'3);
say $num;          # OUTPUT: «A␤» 
say $letter;       # OUTPUT: «3␤» 
 
# Swap two variables' values 
$num :=: $letter;
 
say $num;          # OUTPUT: «3␤» 
say $letter;       # OUTPUT: «A␤» 
 
sub postfix:<!>Int $num where * >= 0 ) { [*1..$num }
say 0!;            # OUTPUT: «1␤» 
say 5!;            # OUTPUT: «120␤» 
 
sub postfix:<>$a ) { say I love $a! }
42♥;               # OUTPUT: «I love 42!␤» 
 
sub postcircumfix:< ⸩>Positional $aWhatever ) {
    say $a[0], ''$a[*-1]
}
 
[1,2,3,4]⸨*⸩;      # OUTPUT: «1…4␤» 
 
constant term:<> = ""# We don't want to quote "love", do we? 
sub circumfix:<α ω>$a ) {
    say $a is the beginning and the end.
};
 
α♥ω;               # OUTPUT: «♥ is the beginning and the end.␤»

These operators use the extended identifier syntax; that is what enables the use of any kind of codepoint to refer to them.