Course of Raku / Advanced / Operators

Types of Raku operators

You have already used many operators — +, ~, ++, and so on. In Raku, operators are classified by where they sit relative to their operands. Knowing the categories helps later, when you define operators of your own.

prefix

A prefix operator comes before a single operand:

my $x = 5;
say -$x; # -5
say ?$x; # True

Here, - negates the number and ? turns a value into its Boolean.

infix

An infix operator sits between two operands. Most of the familiar arithmetic and string operators are infix:

say 3 + 4;       # 7
say 'a' ~ 'b';   # ab

An infix operator is not always a punctuation symbol — it can be a word. The gcd operator you met with integers, for example, is an infix operator written as a name between its two operands:

say 12 gcd 18;   # 6

postfix

A postfix operator comes after a single operand:

my $x = 5;
$x++;
say $x; # 6

circumfix and postcircumfix

A circumfix operator surrounds its operand. The square brackets that build an array are a circumfix operator:

my @a = [1, 2, 3];

A postcircumfix operator surrounds something but follows a term. Subscripting is a postcircumfix operator — the [1] after @a:

my @a = 10, 20, 30;
say @a[1]; # 20

These names — prefix, infix, postfix, circumfix, and postcircumfix — are the same words Raku uses when you declare a new operator, as you will see later.

Practice

Complete the quizzes that cover the contents of this topic.

Exercises

This section contains 3 exercises. Examine all the topics of this section before doing the coding practice.

  1. Count up
  2. Boolean of a value
  3. Subscript a hash

Course navigation

Operators   |   Count up