- our TYPE EXPR
- our EXPR : ATTRS
- our TYPE EXPR : ATTRS
ourassociates a simple name with a package variable in the current package for use within the current scope. Whenuse strict 'vars'is in effect,ourlets you use declared global variables without qualifying them with package names, within the lexical scope of theourdeclaration. In this wayourdiffers fromuse vars, which is package scoped.Unlike
my, which both allocates storage for a variable and associates a simple name with that storage for use within the current scope,ourassociates a simple name with a package variable in the current package, for use within the current scope. In other words,ourhas the same scoping rules asmy, but does not necessarily create a variable.If more than one value is listed, the list must be placed in parentheses.
our $foo; our($bar, $baz);
An
ourdeclaration declares a global 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:package Foo; our $bar; # declares $Foo::bar for rest of lexical scope $bar = 20;
package Bar; print $bar; # prints 20, as it refers to $Foo::bar
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.use warnings; package Foo; our $bar; # declares $Foo::bar for rest of lexical scope $bar = 20;
package Bar; our $bar = 30; # declares $Bar::bar for rest of lexical scope print $bar; # prints 30
our $bar; # emits warning but has no other effect print $bar; # still prints 30
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
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.

