Terms

Perl 6 Terms

Most syntactic constructs in Perl 6 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 Perl 6. 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 infix:«=>» (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.

term *

Creates an object of type Whatever. See Whatever documentation for more details.

Identifier terms

There are built-in identifier terms in Perl 6, 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.

term time

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

term rand

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

term π

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

term pi

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

term τ

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

term tau

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

term e

Returns Euler's number.

term i

Returns the imaginary unit (for Complex numbers).

Variables

Variables are discussed in the variable language docs.

Constants

Constants are declared with constant, followed by an identifier and do not require a sigil. The RHS is evaluated at compile time, what may be too early to make sense.

constant SpeedOfLight = 299792458;     # m/s 
constant PHI          = 1.61803398875# The golden ratio is everywhere! 
constant POW2 = do { my int @table@table = 124 ... 2**32@table };
say POW2[16];
# OUTPUT: «65536␤» 

Constants are our-scoped by default, but adding my would make them lexical.

# Use "my" to make it only visible within the current block 
my constant SpeedOfLight = 186200# mi/s (watch out, not SI units)