What is the difference between 'my' and 'our' in Perl?
How does our
differ from my
and what does our
do?
In Summary:
Available since Perl 5, my
is a way to declare non-package variables, that are:
- private
- new
- non-global
- separate from any package, so that the variable cannot be accessed in the form of
$package_name::variable
.
On the other hand, our
variables are package variables, and thus automatically:
- global variables
- definitely not private
- not necessarily new
-
can be accessed outside the package (or lexical scope) with the
qualified namespace, as
$package_name::variable
.
Declaring a variable with our
allows you to predeclare variables in order to use them under use strict
without getting typo warnings or compile-time errors. Since Perl 5.6, it has replaced the obsolete use vars
, which was only file-scoped, and not lexically scoped as is our
.
For example, the formal, qualified name for variable $x
inside package main
is $main::x
. Declaring our $x
allows you to use the bare $x
variable without penalty (i.e., without a resulting error), in the scope of the declaration, when the script uses use strict
or use strict "vars"
. The scope might be one, or two, or more packages, or one small block.
The PerlMonks and PerlDoc links from cartman and Olafur are a great reference - below is my crack at a summary:
my
variables are lexically scoped within a single block defined by {}
or within the same file if not in {}
s. They are not accessible from packages/subroutines defined outside of the same lexical scope / block.
our
variables are scoped within a package/file and accessible from any code that use
or require
that package/file - name conflicts are resolved between packages by prepending the appropriate namespace.
Just to round it out, local
variables are "dynamically" scoped, differing from my
variables in that they are also accessible from subroutines called within the same block.
An example:
use strict;
for (1 .. 2){
# Both variables are lexically scoped to the block.
our ($o); # Belongs to 'main' package.
my ($m); # Does not belong to a package.
# The variables differ with respect to newness.
$o ++;
$m ++;
print __PACKAGE__, " >> o=$o m=$m\n"; # $m is always 1.
# The package has changed, but we still have direct,
# unqualified access to both variables, because the
# lexical scope has not changed.
package Fubb;
print __PACKAGE__, " >> o=$o m=$m\n";
}
# The our() and my() variables differ with respect to privacy.
# We can still access the variable declared with our(), provided
# that we fully qualify its name, but the variable declared
# with my() is unavailable.
print __PACKAGE__, " >> main::o=$main::o\n"; # 2
print __PACKAGE__, " >> main::m=$main::m\n"; # Undefined.
# Attempts to access the variables directly won't compile.
# print __PACKAGE__, " >> o=$o\n";
# print __PACKAGE__, " >> m=$m\n";
# Variables declared with use vars() are like those declared
# with our(): belong to a package; not private; and not new.
# However, their scoping is package-based rather than lexical.
for (1 .. 9){
use vars qw($uv);
$uv ++;
}
# Even though we are outside the lexical scope where the
# use vars() variable was declared, we have direct access
# because the package has not changed.
print __PACKAGE__, " >> uv=$uv\n";
# And we can access it from another package.
package Bubb;
print __PACKAGE__, " >> main::uv=$main::uv\n";