our
- our TYPE VARLIST
- our VARLIST : ATTRS
- our TYPE VARLIST : ATTRS
ourmakes a lexical alias to a package (i.e. global) variable of the same name in the current package for use within the current lexical scope.ourhas the same scoping rules asmyorstate, meaning that it is only valid within a lexical scope. Unlikemyandstate, which both declare new (lexical) variables,ouronly creates an alias to an existing variable: a package variable of the same name.This means that when
use strict 'vars'is in effect,ourlets you use a package variable without qualifying it with the package name, but only within the lexical scope of theourdeclaration. This applies immediately--even within the same statement.This works even if the package variable has not been used before, as package variables spring into existence when first used.
Because the variable becomes legal immediately under
use strict 'vars', so long as there is no variable with that name is already in scope, you can then reference the package variable again even within the same statement.If more than one variable is listed, the list must be placed in parentheses.
- our($bar, $baz);
An
ourdeclaration 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
ourdeclarations 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 multiplemydeclarations. Unlike a secondmydeclaration, which will bind the name to a fresh variable, a secondourdeclaration in the same package, in the same scope, is merely redundant.An
ourdeclaration 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
fieldspragma, and attributes are handled using theattributespragma, or, starting from Perl 5.8.0, also via theAttribute::Handlersmodule. See Private Variables via my() in perlsub for details, and fields, attributes, and Attribute::Handlers.Note that with a parenthesised list,
undefcan be used as a dummy placeholder, for example to skip assignment of initial values:ourdiffers fromuse vars, which allows use of an unqualified name only within the affected package, but across scopes.