Variables & sigils
Declarators — my / our / constant
FullHow a name is introduced and scoped — lexical my, package our, immutable constant.
A declarator introduces a variable and fixes its scope. my is the everyday lexical declarator; our binds a name into the enclosing package; constant defines a compile-time immutable value.
my — lexical scope #
A my variable is visible from its declaration to the end of the enclosing block. An inner block can shadow an outer name.
my $x = 10;
{ my $x = 20; say $x; }
say $x;
Output
20
10constant #
constant binds a name to a value once, at compile time; the value cannot be reassigned. The sigil is optional for scalars.
constant GREETING = "hi"; say GREETING;
Output
hiour — package scope #
our declares a variable in the current package, reachable from outside by its fully-qualified Package::name.
our $g = 42;
say $g;
package Foo { our $bar = 99; }
say $Foo::bar;
Output
42
99Notes #
- Prefer
myby default; reach forouronly when a name must be visible across package boundaries. - A
constantlist is immutable. Raku++ gists aconstant @arrayas[2 3 5 7], whereas Rakudo shows the immutable-Listform(2 3 5 7)— a display difference only; the values are identical. stateis a fourth declarator — per-closure persistent storage — covered under State variables.