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
4212_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.341_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-323e8
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
a => 1'a' => 'b':identifier:!identifier:identifier<value>:identifier<value1 value2>:identifier():identifier['val1', 'val2']:identifier:valueidentifier::::
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
()1, 2, 3<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> ;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 variables without a container, and thus cannot be rebound. However, their initializers are evaluated at BEGIN time:
constant speed-of-light = 299792458; # m/sconstant = 1, 2, 3;constant = ;talk speed-of-light²; # OUTPUT: «89875517873681764»talk ; # 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.pm6unit ;constant comp-time = DateTime.now;
# The value of the constant remains the same even though our script# is executed multiple times:$ perl6 -I. -MFoo -e 'say Foo::comp-time'2018-06-17T18:18:50.021484-04:00$ perl6 -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 = rand;our constant = 1, 2, 3;constant = %(: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 variables, you cannot parameterize @
-, %
-, and &
-sigiled constants by specifying the parameterization type in the declarator itself:
# !!WRONG!! cannot parameterize @-sigiled constant with Intour Int constant = 42;# OK: parameterized types as values are fineconstant = 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 = 42;.perl.say; # OUTPUT: «(42,)»constant = [<a b c>];.perl.say; # OUTPUT: «["a", "b", "c"]»
use v6.d.PREVIEW;constant = <foo bar>;.perl.say; # OUTPUT: «Map.new((:foo("bar")))»constant = ;.perl.say; # OUTPUT: «{:bar(72), :foo(10)}»# Pair is already Associative, so it remains a Pairconstant = :72baz;.perl.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 := :;constant \foo = 42;constant = 'anon';