Movatterモバイル変換


[0]ホーム

URL:


functions /our
(source,CPAN)
You are viewing the version of this documentation from Perl 5.30.2.View the latest version
#our VARLIST
#our TYPE VARLIST
#our VARLIST : ATTRS
#our TYPE VARLIST : ATTRS

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

our has the same scoping rules asmy orstate, meaning that it is only valid within a lexical scope. Unlikemy andstate, which both declare new (lexical) variables,our only creates an alias to an existing variable: a package variable of the same name.

This means that whenuse 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 theour declaration. This applies immediately--even within the same statement.

package Foo;use strict;$Foo::foo = 23;{    our $foo;   # alias to $Foo::foo    print $foo; # prints 23}print $Foo::foo; # prints 23print $foo; # ERROR: requires explicit package name

This works even if the package variable has not been used before, as package variables spring into existence when first used.

package Foo;use strict;our $foo = 23;   # just like $Foo::foo = 23print $Foo::foo; # prints 23

Because the variable becomes legal immediately underuse 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.

package Foo;use strict;my  $foo = $foo; # error, undeclared $foo on right-hand sideour $foo = $foo; # no errors

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

our($bar, $baz);

Anour 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:

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

Multipleour 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 multiplemy declarations. Unlike a secondmy declaration, which will bind the name to a fresh variable, a secondour declaration 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 scopeprint $bar;    # prints 30our $bar;      # emits warning but has no other effectprint $bar;    # still prints 30

Anour 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 thefields pragma, and attributes are handled using theattributes pragma, or, starting from Perl 5.8.0, also via theAttribute::Handlers module. See"Private Variables via my()" in perlsub for details.

Note that with a parenthesised list,undef can be used as a dummy placeholder, for example to skip assignment of initial values:

our ( undef, $min, $hour ) = localtime;

our differs fromuse vars, which allows use of an unqualified nameonly within the affected package, but across scopes.

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2025 Movatter.jp