Raku Operators
raku(1) defines an extensive set of operators. They cannot all be listed here. Instead see the canonical list for further information.
Contents
Assignment and Binding
Variables are assigned like:
my $a = 1
Variables can also be bound. If a variable is bound to a value literal, it is effectively a constant.
my $a := 1
On the other hand, if a variable is bound to another variable, it becomes a second reference to the same underlying data.
my $a := $b
Arithmetic Operators
Infix Operator |
Meaning |
Example |
+ |
Addition |
1 + 2 = 3 |
- |
Subtraction |
1 - 2 = -1 |
* |
Multiplication |
1 * 2 = 2 |
/ |
Division |
1 / 2 = .5 |
div |
Integer Division |
1 div 2 = 0 |
** |
Exponentiation |
1 ** 2 = 1 |
% |
Modulo |
1 % 2 = 1 |
%% |
Divisibility |
1 %% 2 = False |
Prefix Operator |
Meaning |
Example |
+ |
Coerce to a number |
+'3' = 3 |
- |
Coerce to a number and negate |
-'3' = -3 |
There are also some compound assignment operators.
Assignment Operator |
Meaning |
+= |
Add to a value |
-= |
Subtract from a value |
*= |
Multiply a value |
/= |
Divide a value |
min= |
Update a value to the lesser of it and some other value |
String Operators
Infix Operator |
Meaning |
Example |
~ |
Concatenation |
'a' ~ 'b' = 'ab' |
There is also a compound assignment operator.
Assignment Operator |
Meaning |
~= |
Concatenate to a value |
Boolean Operators
Prefix Operator |
Meaning |
Example |
? |
Coerce to a boolean |
?'' = False |
! |
Coerce to a boolean and negate |
!0 = True |
Comparison Operators
There are a set of numeric comparison operators.
Infix Operator |
Meaning |
Example |
== |
Equality |
1 == 2 = False |
!= |
Inequality |
1 != 2 = True |
< |
Less than |
1 < 2 = True |
<= |
Less than or equal to |
1 <= 2 = True |
> |
Greater than |
1 > 2 = False |
>= |
Greater than or equal to |
1 >= 2 = False |
There are also a set of string comparison operators.
Infix Operator |
Meaning |
Example |
eq |
Equality |
'a' eq 'b' = False |
ne |
Inequality |
'a' ne 'b' = True |
lt |
Less than |
'a' lt 'b' = True |
le |
Less than or equal to |
'a' le 'b' = True |
gt |
Greater than |
'a' gt 'b' = False |
ge |
Greater than or equal to |
'a' ge 'b' = False |
There are also three-way comparisons operators. These return one of Same, Less, or More.
<=> for numeric values
leg for string values
cmp is a smart comparator, dispatching between the above two
Match Operators
The smart match operator ~~ has different behavior depending on the operands.
Expression |
Result |
2 ~~ 2 |
True |
2 ~~ Int |
True |
"Raku" ~~ "Raku" |
True |
"Raku" ~~ Str |
True |
"enlightenment" ~~ /light/ |
「light」 |
When one or both of the operands are enum names, the following behaviors occur:
Enum types match all constituent names (e.g. True ~~ Bool)
Enum names match both their name and their value (e.g. True ~~ 1)
Reduction Operators
Any infix operator can be surrounded with square brackets ([,]) to form a reduction operator. This is used as a prefix list operator.
say [+] 1, 2, 3; # 1 + 2 + 3 = 6
Reversed Operators
Any infix operator can be prefixed with R to form a reversed operator. This simply reverses which operand is the first input or the second input.
This is only useful for non-commutative operators. Furthermore, one of the only useful ways to use reversed operators is as a reduction operator.
say [R/] 2, 4, 16; # 16 / 4 = 4; 4 / 2 = 2
Hyper Operators
The hyper operator tokens are « and », but the ASCII << and >> are also accepted. These tokens are applied to another operator (sometimes surrounding, sometimes prefixing, etc, etc) to form a hyper operator. These are used to reduce two lists in some manner.
The way to call a hyper operator, and the manner in which the two lists reduce, depends on the operands.
The general rules of hyper operators are:
- any non-list operands are implicitly cast into lists
- the 'arrows' should point at the shorter list
- the shorter list is cycled until the longer list is completely processed
Some examples:
say (1, 2, 3) »*» 2; # (2 4 6) say (1, 2, 3, 4) »~» <a b>; # (1a 2b 3a 4b) say (1, 2, 3) »+« (4, 5, 6); # (5 7 9) say (&sin, &cos, &sqrt)».(0.5); # (0.479425538604203 0.877582561890373 0.707106781186548)