Variables & sigils

Declarators — my / our / constant

Full

How 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
10

constant #

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
hi

our — 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
99

Notes #