Variables, Sigils and Twigils
3.1 Sigils
Default type constraint Mu (holds Any by default) Positional (array/list) my @a = 1, 2, 3; Associative (hash) my %h = a => 1; my &f = sub { 42 };
Sigils are invariant: @array[0], %hash<key>, unlike Perl 5's $array[0].
Sigilless variables: my \x = 42; (binds directly, immutable binding).
my $answer = 42;
my Str $name = 'Ana'; # typed variable
my Int $count; # undefined: holds the type object (Int)
say $count; # (Int)
say $count.defined; # False
my ($a, $b, $c) = 1, 2, 3; # list assignment
($a, $b) = ($b, $a); # swap
constant PI-ISH = 3.14; # compile-time constant
my $x = my $y = 0; # chained declaration3.2 Scope Declarators
Lexical scope (the default choice) Package scope (+ lexical alias)
Lexical, initialized once, persists between calls Attribute (in classes) Anonymous (no name installed) temp / let
Dynamically restore value at scope exit (always / on failure)
sub counter { state $n = 0; ++$n }
say counter() for ^3; # 1 2 33.3 Twigils
has $!secret; Public attribute (auto accessor) $*OUT, $*ERR, $*CWD, %*ENV, @*ARGS, $*PROGRAM
$?FILE, $?LINE, $?PACKAGE, &?ROUTINE Positional placeholder parameter { $^a + $^b } Named placeholder parameter { say $:name } $=pod, $=finish Slang (sublanguage) variable $~MAIN, $~Regex Named match capture ($/<name>)
3.4 Special Variables
Last match object; $0, $1... and $<name> $*IN, $*OUT, $*ERR $*CWD, $*HOME, $*TMPDIR Directories (IO::Path)
$*PROGRAM-NAME, $*PID, $*KERNEL, $*DISTRO, $*VM, $*RAKU