Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

our

Perl 5 version 8.9 documentation
Recently read

our

  • our EXPR

  • our EXPR TYPE
  • our EXPR : ATTRS
  • our TYPE EXPR : ATTRS

    our associates a simple name with a package variable in the current package for use within the current scope. When use strict 'vars' is in effect, our lets you use declared global variables without qualifying them with package names, within the lexical scope of the our declaration. In this way our differs from use 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, our associates a simple name with a package variable in the current package, for use within the current scope. In other words, our has the same scoping rules as my, but does not necessarily create a variable.

    If more than one value is listed, the list must be placed in parentheses.

    1. our $foo;
    2. our($bar, $baz);

    An our declaration 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:

    1. package Foo;
    2. our $bar; # declares $Foo::bar for rest of lexical scope
    3. $bar = 20;
    4. package Bar;
    5. print $bar; # prints 20, as it refers to $Foo::bar

    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.

    1. use warnings;
    2. package Foo;
    3. our $bar; # declares $Foo::bar for rest of lexical scope
    4. $bar = 20;
    5. package Bar;
    6. our $bar = 30; # declares $Bar::bar for rest of lexical scope
    7. print $bar; # prints 30
    8. our $bar; # emits warning but has no other effect
    9. print $bar; # still prints 30

    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 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.

    The only currently recognized our() attribute is unique which indicates that a single copy of the global is to be used by all interpreters should the program happen to be running in a multi-interpreter environment. (The default behaviour would be for each interpreter to have its own copy of the global.) Examples:

    1. our @EXPORT : unique = qw(foo);
    2. our %EXPORT_TAGS : unique = (bar => [qw(aa bb cc)]);
    3. our $VERSION : unique = "1.00";

    Note that this attribute also has the effect of making the global readonly when the first new interpreter is cloned (for example, when the first new thread is created).

    Multi-interpreter environments can come to being either through the fork() emulation on Windows platforms, or by embedding perl in a multi-threaded application. The unique attribute does nothing in all other environments.

    Warning: the current implementation of this attribute operates on the typeglob associated with the variable; this means that our $x : unique also has the effect of our @x : unique; our %x : unique . This may be subject to change.