Terms
ErrorsCollection

Terms

Raku terms

Most syntactic constructs in Raku can be categorized in terms and operators.

Here you can find an overview of different kinds of terms.

Literals

Int

42
12_300_00
:16<DEAD_BEEF>

Int literals consist of digits and can contain underscores between any two digits.

To specify a base other than ten, use the colonpair form :radix<number> .

Rat

12.34
1_200.345_678

Rat literals (rational numbers) contain two integer parts joined by a dot.

Note that trailing dots are not allowed, so you have to write 1.0 instead of 1. (this rule is important because there are infix operators starting with a dot, for example the .. Range operator).

Num

12.3e-32
3e8

Num literals (floating point numbers) consist of Rat or Int literals followed by an e and a (possibly negative) exponent. 3e8 constructs a Num with value 3 * 10**8.

Str

'a string'
'I\'m escaped!'
"I don't need to be"
"\"But I still can be,\" he said."
q|Other delimiters can be used too!|

String literals are most often created with ' or ", however strings are actually a powerful sub-language of Raku. See Quoting Constructs.

Regex

/ match some text /
rx/slurp \s rest (.*$/

These forms produce regex literals. See Quoting Constructs.

Pair

=> 1
'a' => 'b'
:identifier
:!identifier
:identifier<value>
:identifier<value1 value2>
:identifier($value)
:identifier['val1''val2']
:identifier{key1 => 'val1'key2 => 'value2'}
:valueidentifier
:$item
:@array
:%hash
:&callable

Pair objects can be created either with the => infix operator (which auto-quotes the left-hand side if it is an identifier), or with the various colon-pair forms. Those almost always start with a colon and then are followed either by an identifier or the name of an already existing variable (whose name without the sigil is used as the key and value of the variable is used as the value of the pair). There is a special form where an integer value is immediately after the colon and the key is immediately after the value.

In the identifier form of a colon-pair, the optional value can be any circumfix. If it is left blank, the value is Bool::True. The value of the :!identifier form is Bool::False.

If used in an argument list, all of these forms count as named arguments, with the exception of 'quoted string' => $value .

List

()
123
<a b c>
«a b c»
qw/a b c/

List literals are: the empty pair of parentheses (), a comma-separated list, or several quoting constructs.

*

The * literal creates an object of type Whatever. See Whatever documentation for more details; when used as a term, the expression it's included will become a WhateverCode

say .^name with *;    # OUTPUT: «Whatever␤» 
say .^name with *+3;  # OUTPUT: «WhateverCode␤» 

Identifier terms

There are built-in identifier terms in Raku, which are listed below. In addition one can add new identifier terms with the syntax:

sub term:<forty-two> { 42 };
say forty-two

or as constants:

constant forty-two = 42;
say forty-two;

term self

Inside a method, self refers to the invocant (i.e. the object the method was called on). If used in a context where it doesn't make sense, a compile-time exception of type X::Syntax::NoSelf is thrown.

term now

Returns an Instant object representing the current time. It includes leap seconds and as such a few dozen seconds larger than time:

say (now - time).Int# OUTPUT: «37␤»

term time

Returns the current POSIX time as an Int. See now for high-resolution timestamp that includes leap seconds.

term rand

Returns a pseudo-random Num in the range 0..^1.

term π

Returns the number π at codepoint U+03C0 (GREEK SMALL LETTER PI), i.e. the ratio between circumference and diameter of a circle. The ASCII equivalent of π is pi.

term pi

Returns the number π, i.e., the ratio between circumference and diameter of a circle. pi is the ASCII equivalent of π.

term τ

Returns the number τ at codepoint U+03C4 (GREEK SMALL LETTER TAU), i.e. the ratio between circumference and radius of a circle. The ASCII equivalent of τ is tau.

term tau

Returns the number τ, i.e. the ratio between circumference and radius of a circle. tau is the ASCII equivalent of τ.

term 𝑒

Returns Euler's number at codepoint U+1D452 (MATHEMATICAL ITALIC SMALL E). The ASCII equivalent of 𝑒 is e.

term e

Returns Euler's number. e is the ASCII equivalent of 𝑒.

term i

Returns the imaginary unit (for Complex numbers).

term ∅

Returns set(), aka the empty set, at codepoint U+2205 (EMPTY SET).

Variables

Variables are discussed in the variable language docs.

Constants

Constants are similar to variable language docs without a container, and thus cannot be rebound. However, their initializers are evaluated at BEGIN time:

constant speed-of-light = 299792458# m/s 
constant @foo  = 123;
constant &talk = &say;
talk speed-of-light²; # OUTPUT: «89875517873681764␤» 
talk @foo;            # OUTPUT: «(1 2 3)␤»

Compile-time evaluation means you should be careful with using constants inside modules, which get automatically precompiled, and so the value of the constant would not change even between multiple executions of the program:

# Foo.rakumod 
unit module Foo;
constant comp-time = DateTime.now;
# The value of the constant remains the same even though our script 
# is executed multiple times: 
$ raku -I. -MFoo -e 'say Foo::comp-time'
2018-06-17T18:18:50.021484-04:00
$ raku -I. -MFoo -e 'say Foo::comp-time'
2018-06-17T18:18:50.021484-04:00

Constants are declared with keyword constant followed by an identifier with an optional sigil. Constants are our scoped by default.

    constant foo  = 42;
my  constant $baz = rand;
our constant @foo = 123;
    constant %bar = %(:42foo, :100bar);

NOTE: if you're using the Rakudo compiler, you need version 2018.08 or newer for type constraints and auto-coercion on constants to be available. Auto-coercion on %-sigiled constants requires 6.d.

An optional type constraint can be used, in which case the use of scope declarator is required:

# !!WRONG!! missing scope declarator before type: 
Int constant bar = 42;
 
# RIGHT: 
our Int constant bar = 42;

Unlike variable language docs, you cannot parameterize @-, %-, and &-sigiled constants by specifying the parameterization type in the declarator itself:

# !!WRONG!! cannot parameterize @-sigiled constant with Int 
# This will throw X::ParametricConstant 
our Int constant @foo = 42;
 
# OK: parameterized types as values are fine 
constant @foo = Array[Int].new: 42;

The reason for the restriction is that constants with @ and % sigils default to List and Map types, which cannot be parameterized. To keep things simple and consistent, parameterization was simply disallowed in these constructs.

The @-, %-, and &-sigiled constants specify implied typecheck of the given value for Positional, Associative, and Callable roles respectively. The @-sigiled constants—and as of 6.d language version, the %-sigiled constants as well—perform auto-coercion of the value if it does not pass the implied typecheck. The @-sigiled constants will coerce using method cache and %-sigiled constants coerce using method Map.

constant @foo = 42;
@foo.raku.say# OUTPUT: «(42,)» 
 
constant @bar = [<a b c>];
@bar.raku.say# OUTPUT: «["a", "b", "c"]» 
 
constant %foo = <foo bar>;
%foo.raku.say# OUTPUT: «Map.new((:foo("bar")))» 
 
constant %bar = {:10foo, :72bar};
%bar.raku.say# OUTPUT: «{:bar(72), :foo(10)}» 
 
# Pair is already Associative, so it remains a Pair 
constant %baz = :72baz;
%baz.raku.say# OUTPUT: «:baz(72)» 

For convenience and consistency reasons, you can use the binding operator (:=) instead of the assignment operator, use backslash before sigilless name of the constant variable (same as with sigilless variables), and even omit the name of the constant entirely to have an anonymous constant. Since you can't refer to anonymous entities, you may be better off using a BEGIN phaser instead, for clarity.

constant %foo := :{:42foo};
constant \foo = 42;
constant = 'anon';