Perl DISCUSSION

What is the difference between my, our and local in Perl, and when should each be used?

Started by gauravflawless Perl my our locallexical scopepackage variablesdynamic scopeuse strict
5 replies 248 views 6 participants
Latest activity · 30 Sep 2026

What is the difference between my, our and local in Perl, and when should each be used?

gauravflawless Perl Forum
#1

After adding use strict; to an old Perl script I had to declare every variable, and I fixed most errors by putting my in front. Some variables are shared between subroutines, though, and the existing code uses our in some places and local in others, for example local $/; before reading a file.

What does each of the three actually do? In particular, is local just an older spelling of my, and when is our the right choice?

Community replies 5

Re: What is the difference between my, our and local in Perl, and when should each be used?

#2

my creates a new lexical variable. It exists from the point of declaration to the end of the enclosing block (or file), and code outside that block cannot reach it by any name. It is not stored in the package symbol table at all. This is the right default for nearly everything: loop counters, temporaries, subroutine arguments via my ($x, $y) = @_;.

our does not create storage. It declares that, inside the current lexical scope, the short name refers to the package variable of that name, so in package Counter the line our $total; lets you write $total for $Counter::total without strict complaining.

Re: What is the difference between my, our and local in Perl, and when should each be used?

#3

local is not an older my. It works only on package (global) variables: it saves the current value, lets you assign a temporary one, and restores the old value automatically when the enclosing block exits. The change is visible to every subroutine called during that time, which is called dynamic scope.

Compare: with our $level = 1; and sub show { print $level }, a caller that does local $level = 2; show(); prints 2, and after that block ends show() prints 1 again. If the caller wrote my $level = 2; instead, show() would still print 1, because the lexical is a different variable that show cannot see.

Re: What is the difference between my, our and local in Perl, and when should each be used?

#4

The practical use of local today is temporarily changing Perl's built-in globals. local $/; sets the input record separator to undef so that <$fh> returns the whole file in one read, and the normal line-by-line behaviour comes back at the closing brace. The usual idiom is my $text = do { local $/; <$fh> };. The same applies to $|, $, and single hash entries such as local $ENV{PATH} = "/usr/bin";.

Without local, changing $/ would silently break every later read in the program, including reads inside modules.

Re: What is the difference between my, our and local in Perl, and when should each be used?

#5

Use our when something outside the file needs to find the variable by its package name. The classic cases are our $VERSION = '1.02';, our @ISA and our @EXPORT_OK in a module, because the module loader and Exporter look those up in the symbol table, where a my variable is invisible.

For state shared by a few subroutines in one file, a file-level my variable is usually better than our: the subroutines declared after it can use it, and no other file can modify it by accident.

Re: What is the difference between my, our and local in Perl, and when should each be used?

#6

Two pitfalls. Trying local on a lexical is a compile error (Can't localize lexical variable), so you cannot mix the two on the same variable. And if you want a private counter that keeps its value between calls, neither local nor our is needed: on Perl 5.10 and later write use feature 'state'; and then state $n = 0; return ++$n; inside the sub. The first three calls return 1, 2 and 3, and nothing outside the sub can reset the counter.

TEP COMMUNITY