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 $.
#!/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.
#!/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.
#!/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.
$_is the default topic used by many operators and functions, includingprint,chomp, and pattern matching when no explicit argument is supplied.@_contains the arguments passed to the currently executing subroutine.$!describes the most recent system error. In a message such asopen ... or die "...: $!", inspect it immediately after the failed operation.$?holds the status from the most recently completed child process. It encodes more than the child's plain exit code; consultperlvarwhen interpreting it.$/is the input record separator, normally a newline.chompuses this separator by default.$,is the output field separator used byprint; its default is empty.$"is the separator used when an array is interpolated in a double-quoted string; its default is a space.$0contains the name of the running program, and$$is its process ID.
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
List all Perl tutorials.