perl-5.38.0
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 は単純名を、現在のレキシカルスコープ内で使うために、 現在のパッケージの同じ名前のパッケージ(つまりグローバルな)変数への レキシカルな別名を作ります。

our has the same scoping rules as my or state, meaning that it is only valid within a lexical scope. Unlike my and state, which both declare new (lexical) variables, our only creates an alias to an existing variable: a package variable of the same name.

ourmystate と同じスコープルールを持ちます; つまり レキシカルスコープの中でだけ有効です。 新しい(レキシカル)変数を宣言する mystate と異なり、our は既に 存在する変数への別名を作るだけです: 同じ名前のパッケージ変数です。

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

つまり、use strict 'vars' が有効の場合は、our を 使うことで、 パッケージ変数をパッケージ名で修飾することなく使うことができますが、 our 宣言のレキシカルスコープ内だけということです。 これは(たとえ同じ文の中でも)直ちに適用されます。

    package Foo;
    use v5.36;  # which implies "use strict;"

    $Foo::foo = 23;

    {
        our $foo;   # alias to $Foo::foo
        print $foo; # prints 23
    }

    print $Foo::foo; # prints 23

    print $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 v5.36;

    our $foo = 23;   # just like $Foo::foo = 23

    print $Foo::foo; # prints 23

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.

変数は use strict 'vars' の基で直ちに正当になるので、 スコープ内に同じ名前の変数がない限り、 たとえ同じ文の中でもパッケージ変数を再び参照できます。

    package Foo;
    use v5.36;

    my  $foo = $foo; # error, undeclared $foo on right-hand side
    our $foo = $foo; # no errors

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

複数の変数を指定する場合は、リストはかっこで囲まなければなりません。

    our($bar, $baz);

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

our 宣言はレキシカルスコープ全体に対して (たとえ パッケージ境界を越えていても)見える、パッケージ変数への別名を宣言します。 この変数が入るパッケージは宣言した時点で定義され、 使用した時点ではありません。 これにより、以下のような振る舞いになります:

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

同じレキシカルスコープでも、パッケージが異なっていれば、同じ名前で複数の our 宣言ができます。 同じパッケージになっていると、警告が出力されるようになっていれば 複数の my 宣言がある場合と同じように警告が出力されます。 新しい変数を名前に割り当てることになる 2 回目の my 宣言と 違って、同じパッケージの同じスコープで 2 回 our 宣言するのは単に冗長です。

    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 our declaration may also have a list of attributes associated with it.

our 宣言には、それと結び付けられる属性のリストを 持つこともあります。

The exact semantics and interface of TYPE and ATTRS are still evolving. TYPE is currently bound to the use of the 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.

TYPE と ATTRS の正確な文法とインターフェースは今でも進化しています。 現在のところ、TYPE は fields プラグマの使用と結び付けられていて、 属性は attributes プラグマか、Perl 5.8.0 からは Attribute::Handlers モジュールと結び付けられています。 詳しくは "Private Variables via my()" in perlsub を参照してください。

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

かっこで囲まれたリストでは、undef は、例えば初期値の代入を 飛ばすために、ダミーのプレースホルダとして使えることに注意してください:

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

our differs from use vars, which allows use of an unqualified name only within the affected package, but across scopes.

ouruse vars と異なります; スコープを またぐのではなく、影響するパッケージの内側 のみ で完全修飾されていない 名前を使えるようにします。