Documentation for syntax
identifiers assembled from the following types:
Identifiers are grammatical building blocks that may be used to give a name to entities/objects such as constants, variables (e.g.
Scalars) and routines (e.g.
Subs and Methods). In a variable name, any sigil (and twigil) precedes the identifier and does not form a part thereof.
constant c = 299792458; # identifier "c" names an Intmy = 123; # identifier "a" in the name "$a" of a Scalarsub hello ; # identifier "hello" names a Sub
Identifiers come in different forms: ordinary, extended, and compound identifiers.
An ordinary identifier is composed of a leading alphabetic character which may be followed by one or more alphanumeric characters. It may also contain isolated, embedded apostrophes
' and/or hyphens
-, provided that the next character is each time alphabetic.
The definitions of "alphabetic" and "alphanumeric" include appropriate Unicode characters. Which characters are "appropriate" depends on the implementation. In the Rakudo/MoarVM Perl 6 implementation alphabetic characters include characters with the Unicode General Category value Letter (L), and the underscore
_. Alphanumeric characters additionally include characters with the Unicode General Category value Number, Decimal Digit (Nd).
# valid ordinary identifiers:x_snake_oilsomething-longerwith-numbers1234don't-do-thatpiece_of_π駱駝道 # "Rakuda-dō", Japanese for "Way of the camel"
# invalid ordinary identifiers:42 # identifier does not start with alphabetic characterwith-numbers1234-5 # embedded hyphen not followed by alphabetic characteris-prime? # question mark is not alphanumericx² # superscript 2 is not alphanumeric (explained above)
It is often convenient to have names that contain characters that are not allowed in ordinary identifiers. Use cases include situations where a set of entities shares a common "short" name, but still needs for each of its elements to be identifiable individually. For example, you might use a module whose short name is
Dog, while its long name includes its naming authority and version:
Dog:auth<Somebody>:ver<1.0> # long module names including author and versionDog:auth<Somebody>:ver<2.0>use Dog:auth<Somebody>:ver<2.0>;# Selection of second module causes its full name to be aliased to the# short name for the rest of # the lexical scope, allowing a declaration# like this.my Dog .= new("woof");
Similarly, sets of operators work together in various syntactic categories with names like
postfix. The official names of these operators often contain characters that are excluded from ordinary identifiers. The long name is what constitutes the extended identifier, and includes this syntactic category; the short name will be included in quotes in the definition:
infix:<+> # the official name of the operator in $a + $binfix:<*> # the official name of the operator in $a * $binfix:«<=» # the official name of the operator in $a <= $b
For all such uses, you can append one or more colon-separated strings to an ordinary identifier to create a so-called extended identifier. When appended to an identifier (that is, in postfix position), this colon-separated string generates unique variants of that identifier.
These strings have the form
value are optional; that is, after the colon that separates it from a regular identifier, there will be a
key and/or a quoting bracketing construct such as
« » or
[' '] which quotes one or more arbitrary characters
# exemplary valid extended identifiers:postfix:<²> # the official long name of the operator in $x²WOW:That'sAwesomeWOW:That's<<🆒>>party:sweet<16># exemplary invalid extended identifiers:party:16<sweet> # 16 is not an ordinary identifierparty:16sweetparty:!a # ...and neither is !aparty: # ...nor $a
In an extended identifier, the postfix string is considered an integral part of the name, so
infix:<-> are two different operators. The bracketing characters used, however, do not count as part of it; only the quoted data matters. So these are all the same name:
Similarly, all of this works:
my :bar<baz> = 'quux';say :bar«baz»; # OUTPUT: «quux␤»my :<home> = 'Where the glory has no end';say :['home']; # OUTPUT: «Where [...]␤»my :bar<2> = 5;say :bar(1+1); # OUTPUT: «5␤»
Where an extended identifier comprises two or more colon pairs, their order is generally significant:
my :b<c>:d<e> = 100;my :d<e>:b<c> = 200;say :b<c>:d<e>; # OUTPUT: «100␤», NOT: «200␤»
An exception to this rule is module versioning; so these identifiers effectively name the same module:
use ThatModule:auth<Somebody>:ver<220.127.116.11.18>use ThatModule:ver<18.104.22.168.18>:auth<Somebody>
Furthermore, extended identifiers support compile-time interpolation; this requires the use of constants for the interpolation values:
constant = 42; # Constant binds to Int; $-sigil enables interpolationmy :foo<42> = "answer";say :foo«»; # OUTPUT: «answer␤»
Although quoting bracketing constructs are generally interchangeable in the context of identifiers, they are not identical. In particular, angle brackets
< > (which mimic single quote interpolation characteristics) cannot be used for the interpolation of constant names.
constant = 'are';my :<are>= <the champions>;say :«»; # OUTPUT: «[the champions]␤»say :<$what>;# Compilation error: Variable '@we:<$what>' is not declared
A compound identifier is an identifier that is composed of two or more ordinary and/or extended identifiers that are separated from one another by a double colon
The double colon
:: is known as the namespace separator or the package delimiter, which clarifies its semantic function in a name: to force the preceding portion of the name to be considered a package/namespace through which the subsequent portion of the name is to be located:
say ::var # OUTPUT: «Hello␤»
In the example above,
MyModule::var is a compound identifier, composed of the package name identifier
MyModule and the identifier part of the variable name
$MyModule::var is often referred to as a package-qualified name.
Separating identifiers with double colons causes the rightmost name to be inserted into existing (see above example) or automatically created packages:
my ::bar = 1;say OUR::.keys; # OUTPUT: «(foo)␤»say OUR::foo.HOW # OUTPUT: «Perl6::Metamodel::PackageHOW.new␤»
The last lines shows how the
foo package was created automatically, as a deposit for variables in that namespace.
The double colon syntax enables runtime interpolation of a string into a package or variable name using
::($expr) where you'd ordinarily put a package or variable name:
my = "quux";my ::quux = 7;say ::(); # OUTPUT: «7␤»