Operators in Raku
Explore arithmetic, comparison, logical, assignment, and string operators in Raku, plus unique features like chained comparisons, junctions, and smart matching
Operators are the verbs of a programming language—they take values and combine them into new ones. Raku has one of the richest operator vocabularies of any language, including familiar arithmetic and comparison operators alongside unusual ones like the three-way comparison <=>, the defined-or //, and junction operators that let a single value be “any of these” at once.
Because Raku is a multi-paradigm language with gradual typing, its operators are carefully separated by purpose. Numeric comparisons (==, <, >) are distinct from string comparisons (eq, lt, gt), so the language never has to guess what you meant. This is a deliberate departure from Perl 5’s context-driven behavior, and it makes Raku code easier to reason about.
In this tutorial you’ll work through arithmetic, comparison, logical, assignment, and string operators, then explore a few operators that are distinctly Raku: chained comparisons, the smart-match operator ~~, and junctions. Every example is runnable with Docker.
Arithmetic Operators
Raku’s arithmetic operators include the usual suspects plus a couple of distinctions worth knowing. The / operator produces an exact rational number (a Rat), not a truncated integer, while div performs integer division and mod gives the integer remainder.
Create a file named arithmetic.raku:
| |
Notice that 17 / 5 yields 3.4 exactly—Raku stores it as a rational rather than a lossy floating-point value. Use div when you specifically want integer division.
Comparison and Logical Operators
Raku splits comparison into numeric and string families. Numeric operators are the symbolic ones (==, !=, <, >, <=, >=), and string operators are spelled out (eq, ne, lt, gt, le, ge). The three-way operators <=> (numeric) and cmp (generic) return an Order value of Less, Same, or More.
Create a file named comparison.raku:
| |
The chained comparison 1 < 2 < 3 is genuine mathematical notation—Raku evaluates it as 1 < 2 && 2 < 3 without you having to spell it out.
Assignment Operators
Every binary operator in Raku has a corresponding assignment form built by appending =. The string concatenation operator ~ becomes ~=, and the defined-or operator // becomes //=, which assigns only when the variable is currently undefined.
Create a file named assignment.raku:
| |
The //= operator is especially handy for supplying defaults: it leaves a defined value untouched but fills in undefined ones.
String and List Operators
Raku uses ~ for string concatenation (not +, which is strictly numeric), x for string repetition, and xx for list repetition. Ranges are built with .., and the smart-match operator ~~ tests whether a value matches a type, range, or regex.
Create a file named string_ops.raku:
| |
The smart-match operator adapts to whatever is on its right side: a type check against Int, a membership test against a range, or a pattern match against a regex. The regex match returns a Match object, which prints with corner brackets around the matched text.
Precedence and Junctions
Raku follows conventional precedence: exponentiation binds tightest, then multiplication and division, then addition and subtraction. Parentheses override the defaults. Raku also offers junctions—values built with | (any), & (all), or ^ (one)—that let a single comparison test against multiple possibilities at once.
Create a file named precedence.raku:
| |
The junction 1 | 2 | 3 behaves like all three values simultaneously. Comparing it with == 2 succeeds because one branch matches. The so operator collapses the resulting junction down to a single Bool.
Running with Docker
| |
Expected Output
# arithmetic.raku
22
12
85
3.4
2
1419857
3
2
# comparison.raku
True
True
True
True
Less
More
Same
True
True
More
False
True
False
True
False
# assignment.raku
15
12
24
6
36
Hello, World!
default
# string_ops.raku
CodeArchaeology
==========
(0 0 0 0 0)
1..5
(1 2 3 4 5)
15
True
True
「ell」
# precedence.raku
14
50
20
True
False
Key Concepts
- Numeric vs. string operators are separate: use
==/</>for numbers andeq/lt/gtfor strings—Raku never guesses which you meant. /returns an exact rational (Rat), so17 / 5is3.4precisely; usedivandmodfor integer division and remainder.~is concatenation,xrepeats strings,xxrepeats lists—string joining never overloads the numeric+.- Three-way operators (
<=>for numbers,cmpfor anything) return anOrderofLess,Same, orMore, ideal for sorting. - Chained comparisons like
1 < 2 < 3work as written, matching mathematical notation. - The smart-match operator
~~flexibly tests values against types, ranges, and regexes. //and//=test definedness rather than truth, making them perfect for supplying defaults without clobbering a valid0or empty string.- Junctions (
|,&,^) let one value stand for many, so a single comparison can test against several possibilities at once.
Running Today
All examples can be run using Docker:
docker pull rakudo-star:alpine
Comments
Loading comments...
Leave a Comment