our
makes a lexical alias to a package variable of the same name in the current
package for use within the current lexical scope.
our
has the same scoping rules as my
or state
, but our
only
declares an alias, whereas my
or state
both declare a variable name and
allocate storage for that name within the current scope.
This means that when use strict 'vars'
is in effect, our
lets you use
a package variable without qualifying it with the package name, but only within
the lexical scope of the our
declaration. In this way, our
differs from
use vars
, which allows use of an unqualified name only within the
affected package, but across scopes.
If more than one value is listed, the list must be placed in parentheses.
An our
declaration declares an alias for a package variable that will be visible
across its entire lexical scope, even across package boundaries. The
package in which the variable is entered is determined at the point
of the declaration, not at the point of use. This means the following
behavior holds:
Multiple our
declarations with the same name in the same lexical
scope are allowed if they are in different packages. If they happen
to be in the same package, Perl will emit warnings if you have asked
for them, just like multiple my
declarations. Unlike a second
my
declaration, which will bind the name to a fresh variable, a
second our
declaration in the same package, in the same scope, is
merely redundant.
An our
declaration may also have a list of attributes associated
with it.
The exact semantics and interface of TYPE and ATTRS are still
evolving. TYPE is currently bound to the use of the fields
pragma,
and attributes are handled using the attributes
pragma, or, starting
from Perl 5.8.0, also via the Attribute::Handlers
module. See
Private Variables via my() in perlsub for details, and fields,
attributes, and Attribute::Handlers.