ZetCode

Perl variables

last modified October 8, 2026

Perl variables hold scalar values, ordered lists, or key-value mappings. A variable's sigil indicates how it is used in an expression. This tutorial introduces $, @, and %, lexical and package variables, interpolation, and several commonly used special variables.

Perl sigils

A scalar variable has a $ sigil, an array variable uses @, and a hash variable uses %. An individual array or hash element is a scalar, so its sigil is $.

sigils.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my $city = 'Oslo';
my @temps = (12, 15, 13);
my %capital = (Norway => 'Oslo', Japan => 'Tokyo');

say $city;
say $temps[1];
say $capital{Japan};

The variable @temps names the whole array, while $temps[1] names one scalar element. Similarly, %capital is a hash, but $capital{Japan} is one scalar value.

$ perl sigils.pl
Oslo
15
Tokyo

For a fuller introduction to sigils and expression context, see Perl basics: scalars, sigils, and context.

Lexical variables with my

my declares a lexical variable. Its visibility is limited to the enclosing block, file, or other lexical scope in which it is declared. Use lexical variables for most application data.

scope.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

my $message = 'outside';

{
    my $message = 'inside';
    say $message;
}

say $message;
$ perl scope.pl
inside
outside

The inner declaration is a separate lexical variable that temporarily shadows the outer variable. It does not change the outer value.

Package variables with our

our declares a lexical alias to a package variable. Its name is available in the current lexical scope, while the variable itself belongs to a package, such as $main::site_name. This is useful when deliberately sharing package-level state, but most variables should be declared with my.

use strict;
use warnings;
use v5.30.0;

our $site_name = 'ZetCode';
say $main::site_name;

our does not create a private lexical value: assigning through either $site_name or its fully qualified name refers to the same package variable.

Persistent lexical variables with state

A state variable is lexically scoped like my, but it keeps its value between executions of its declaration. It is initialized once. The feature is available in modern Perl; this example uses Perl 5.30 or later.

counter.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.30.0;

sub next_id {
    state $id = 0;
    return ++$id;
}

say next_id();
say next_id();
$ perl counter.pl
1
2

The $id name is private to next_id, and its value persists across calls. A state declaration inside a subroutine is initialized the first time execution reaches it.

Variable interpolation

Double-quoted strings interpolate scalar variables and interpret escape sequences such as \n. Single-quoted strings do not interpolate variables. Use braces to delimit a variable name when adjacent text could be read as part of the name.

use strict;
use warnings;
use v5.30.0;

my $name = 'Ada';
say "Hello, $name!";
say "${name}son";
say 'Hello, $name!';
Hello, Ada!
Adason
Hello, $name!

Arrays interpolate as a joined string using $" (a space by default). Hashes are better accessed by individual keys or explicitly formatted with a loop.

use strict;
use warnings;
use v5.30.0;

my @colors = ('red', 'blue');
say "@colors";
say "\@colors has " . scalar(@colors) . " items";
red blue
@colors has 2 items

Use a backslash to prevent interpolation of a sigil, as in \@colors.

Common special variables

Perl provides special variables for common tasks. These names have defined roles, so check the documentation before reusing them.

For the complete, version-specific list, see the Perl documentation for perlvar. Many special variables have modern named alternatives; prefer those where they make code clearer.

Source

Perl special variables ยท Perl data types

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.