Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Declare and assign a local var ...Declare a separate global namespace (Berikutnya)
Keywords altering or affecting scoping of identifiers

Declare and assign a package variable (lexical scoping)

Daftar Isi

  • our EXPR

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

    our associates a simple name with a package variable in the currentpackage for use within the current scope. When use strict 'vars' is ineffect, our lets you use declared global variables without qualifyingthem 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 or state, which allocates storage for a variable andassociates a simple name with that storage for use within the currentscope, our associates a simple name with a package (read: global)variable in the current package, for use within the current lexical scope.In other words, our has the same scoping rules as my or state, butdoes not necessarily create a variable.

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

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

    An our declaration declares a global variable that will be visibleacross its entire lexical scope, even across package boundaries. Thepackage in which the variable is entered is determined at the pointof the declaration, not at the point of use. This means the followingbehavior 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 lexicalscope are allowed if they are in different packages. If they happento be in the same package, Perl will emit warnings if you have askedfor them, just like multiple my declarations. Unlike a secondmy declaration, which will bind the name to a fresh variable, asecond our declaration in the same package, in the same scope, ismerely 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 associatedwith it.

    The exact semantics and interface of TYPE and ATTRS are stillevolving. TYPE is currently bound to the use of the fields pragma,and attributes are handled using the attributes pragma, or, startingfrom Perl 5.8.0, also via the Attribute::Handlers module. SeePrivate Variables via my() in perlsub for details, and fields,attributes, and Attribute::Handlers.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Declare and assign a local var ...Declare a separate global namespace (Berikutnya)