5.14.1

NAME

perlsub - Perl subroutines

perlsub - Perl のサブルーチン

概要

To declare subroutines:

サブルーチンを宣言するには:

    sub NAME;                     # A "forward" declaration.
    sub NAME(PROTO);              #  ditto, but with prototypes
    sub NAME : ATTRS;             #  with attributes
    sub NAME(PROTO) : ATTRS;      #  with attributes and prototypes
    sub NAME;                     # "先行" 宣言
    sub NAME(PROTO);              #  同上。ただしプロトタイプ付き
    sub NAME : ATTRS;             #  属性付き
    sub NAME(PROTO) : ATTRS;      #  属性とプロトタイプ付き
    sub NAME BLOCK                # A declaration and a definition.
    sub NAME(PROTO) BLOCK         #  ditto, but with prototypes
    sub NAME : ATTRS BLOCK        #  with attributes
    sub NAME(PROTO) : ATTRS BLOCK #  with prototypes and attributes
    sub NAME BLOCK                # 宣言と定義
    sub NAME(PROTO) BLOCK         #  同上。ただしプロトタイプ付き
    sub NAME : ATTRS BLOCK        #  属性付き
    sub NAME(PROTO) : ATTRS BLOCK #  プロトタイプと属性付き

To define an anonymous subroutine at runtime:

実行時に無名サブルーチンを定義するには:

    $subref = sub BLOCK;                 # no proto
    $subref = sub (PROTO) BLOCK;         # with proto
    $subref = sub : ATTRS BLOCK;         # with attributes
    $subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributes
    $subref = sub BLOCK;                 # プロトタイプなし
    $subref = sub (PROTO) BLOCK;         # プロトタイプ付き
    $subref = sub : ATTRS BLOCK;         # 属性付き
    $subref = sub (PROTO) : ATTRS BLOCK; # プロトタイプと属性付き

To import subroutines:

サブルーチンをインポートするには:

    use MODULE qw(NAME1 NAME2 NAME3);

To call subroutines:

サブルーチンを呼び出すには:

    NAME(LIST);    # & is optional with parentheses.
    NAME LIST;     # Parentheses optional if predeclared/imported.
    &NAME(LIST);   # Circumvent prototypes.
    &NAME;         # Makes current @_ visible to called subroutine.
    NAME(LIST);    # かっこが付いているときは & は省略可能。
    NAME LIST;     # 予め宣言/インポートされているならかっこは省略可能。
    &NAME(LIST);   # プロトタイプを回避する。
    &NAME;         # 呼び出されたサブルーチンから現在の @_ を可視化する。

説明

Like many languages, Perl provides for user-defined subroutines. These may be located anywhere in the main program, loaded in from other files via the do, require, or use keywords, or generated on the fly using eval or anonymous subroutines. You can even call a function indirectly using a variable containing its name or a CODE reference.

多くの言語と同様、Perl はユーザー定義のサブルーチンを提供しています。 これらのサブルーチンは、メインプログラムのどこにでも置くことができ、 do, require, use といったキーワードを使って他のファイルから ロードすることができ、eval や無名サブルーチンを使って 生成することもできます。 サブルーチンの名前や、コードリファレンスを保持する変数を使って 間接的に関数を呼び出すことも可能です。

The Perl model for function call and return values is simple: all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars. Any arrays or hashes in these call and return lists will collapse, losing their identities--but you may always use pass-by-reference instead to avoid this. Both call and return lists may contain as many or as few scalar elements as you'd like. (Often a function without an explicit return statement is called a subroutine, but there's really no difference from Perl's perspective.)

Perl での関数呼び出しと戻り値のモデルは単純です。全ての関数は引数を、 平坦で一つのスカラのリストで受け取り、同様に全ての関数は呼び出し元に 対して平坦で一つのスカラのりストで返すというものです。 これらの呼び出しリストと戻り値リストにある全ての配列とハッシュは、 潰されてそのアイデンティティを失います。 しかし、これを避けるために常にリファレンスで渡すことができます。 呼び出しリストと戻り値リストの両方とも好きなだけの数のスカラを 保持することができます(明確な return 文のない関数はしばしばサブルーチンと 呼ばれますが、Perl の定義上はこれらの間に何の違いもありません)。

Any arguments passed in show up in the array @_. Therefore, if you called a function with two arguments, those would be stored in $_[0] and $_[1]. The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). If an argument is an array or hash element which did not exist when the function was called, that element is created only when (and if) it is modified or a reference to it is taken. (Some earlier versions of Perl created the element whether or not the element was assigned to.) Assigning to the whole array @_ removes that aliasing, and does not update any arguments.

ルーチンに渡されるすべての引数は配列 @_ に置かれます。 したがって、ある関数を二つの引数を付けて呼び出したならば、 その引数は $_[0]$_[1] に格納されます。 配列 @_ は local 配列ですが、その要素は実際の スカラパラメータの別名です。 たとえば $_[0] が更新された場合、対応する引数が更新されます (更新できない場合にはエラーとなります)。 引数が、配列やハッシュの(関数が呼び出された時点では存在してない) 要素であった場合、その要素は(対応する別名が)修正されたり リファレンスが取られたときにのみ作成されます(以前の一部のバージョンの Perl では、この要素は代入が行われようが行われまいが作成されていました)。 配列 @_ 全体に対する代入は別名を破棄し、何の引数も更新しません。

A return statement may be used to exit a subroutine, optionally specifying the returned value, which will be evaluated in the appropriate context (list, scalar, or void) depending on the context of the subroutine call. If you specify no return value, the subroutine returns an empty list in list context, the undefined value in scalar context, or nothing in void context. If you return one or more aggregates (arrays and hashes), these will be flattened together into one large indistinguishable list.

サブルーチンから脱出するために使われた return 文が 使われることもありますし、サブルーチン呼び出しのコンテキストによって 適切なコンテキスト(リスト、スカラ、無効)で評価される戻り値の指定を 省略する事が可能です。 もし何の戻り値も指定しなければ、サブルーチンはリストコンテキストにおいては 空リストを返し、スカラコンテキストにおいては未定義値を返し、 無効コンテキストではなにも返しません。

If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while, the returned value is unspecified. The empty sub returns the empty list.

return がなく、最後の文が式だった場合、その値が返されます。 最後の文が foreachwhile のようなループ制御構造だった場合、 返される値は不定です。 空のサブルーチンは空リストを返します。

Perl does not have named formal parameters. In practice all you do is assign to a my() list of these. Variables that aren't declared to be private are global variables. For gory details on creating private variables, see "Private Variables via my()" and "Temporary Values via local()". To create protected environments for a set of functions in a separate package (and probably a separate file), see "Packages" in perlmod.

Perlは名前付き仮引数を持っていません。 my() にこれらのリストを代入することで行えます。 プライベートであると宣言されずに使われている変数は全てグローバル変数です。 プライベート変数に関する詳細は"Private Variables via my()""Temporary Values via local()" を参照してください。 パッケージで(おそらくはファイルでも)分けられている関数のセットのための 保護された環境を作るには "Packages" in perlmod を参照してください。

Example:

例:

    sub max {
        my $max = shift(@_);
        foreach $foo (@_) {
            $max = $foo if $max < $foo;
        }
        return $max;
    }
    $bestday = max($mon,$tue,$wed,$thu,$fri);

Example:

例:

    # get a line, combining continuation lines
    #  that start with whitespace
    # 行を取り、空白で始まる継続行を連結します

    sub get_line {
        $thisline = $lookahead;  # global variables!
        LINE: while (defined($lookahead = <STDIN>)) {
            if ($lookahead =~ /^[ \t]/) {
                $thisline .= $lookahead;
            }
            else {
                last LINE;
            }
        }
        return $thisline;
    }

    $lookahead = <STDIN>;       # get first line
    while (defined($line = get_line())) {
        ...
    }

Assigning to a list of private variables to name your arguments:

引数に名前を付けるためにプライベート変数のリストに代入する:

    sub maybeset {
        my($key, $value) = @_;
        $Foo{$key} = $value unless $Foo{$key};
    }

Because the assignment copies the values, this also has the effect of turning call-by-reference into call-by-value. Otherwise a function is free to do in-place modifications of @_ and change its caller's values.

これは、参照渡しを値渡しにする効果もあります。 なぜなら、値のコピーを代入しているからです。 このようにしないのであれば、関数は自由に @_ の値をその場で 書き換えることができ、それはそのまま呼び出し側の値を変更します。

    upcase_in($v1, $v2);  # this changes $v1 and $v2
    sub upcase_in {
        for (@_) { tr/a-z/A-Z/ }
    }

You aren't allowed to modify constants in this way, of course. If an argument were actually literal and you tried to change it, you'd take a (presumably fatal) exception. For example, this won't work:

もちろん、このやり方で定数を変更することは許されません。 ある引数が実際にはリテラルであった場合にその引数を変更しようとすると、 (おそらくは致命的な)例外が引き起こされることになるでしょう。 例えば次の例はうまく働きません。

    upcase_in("frederick");

It would be much safer if the upcase_in() function were written to return a copy of its parameters instead of changing them in place:

upcase_in() 関数を安全なものにするには、パラメータそのものを 書き換えるのではなくそのコピーを返すように記述するようにします:

    ($v3, $v4) = upcase($v1, $v2);  # this doesn't change $v1 and $v2
    sub upcase {
        return unless defined wantarray;  # void context, do nothing
        my @parms = @_;
        for (@parms) { tr/a-z/A-Z/ }
        return wantarray ? @parms : $parms[0];
    }

Notice how this (unprototyped) function doesn't care whether it was passed real scalars or arrays. Perl sees all arguments as one big, long, flat parameter list in @_. This is one area where Perl's simple argument-passing style shines. The upcase() function would work perfectly well without changing the upcase() definition even if we fed it things like this:

この(プロトタイプがついていない)関数が自分に対して本当のスカラが 渡されたのか配列が渡されたのかを気にしていないということに注意してください。 Perl は一つの巨大な平坦な(リストの中にリストが含まれることはない、 ということ) @_ パラメータリストとして全ての引数を見るのです。 これは Perl の単純な引数渡しの形式のやり方の一つです。 この upcase() 関数は、以下のような呼び出しをした場合でも upcase() の 定義を変更することなく完璧に動作します:

    @newlist   = upcase(@list1, @list2);
    @newlist   = upcase( split /:/, $var );

Do not, however, be tempted to do this:

ただし、以下のような呼び出しをやろうとしてはいけません:

    (@a, @b)   = upcase(@list1, @list2);

Like the flattened incoming parameter list, the return list is also flattened on return. So all you have managed to do here is stored everything in @a and made @b empty. See "Pass by Reference" for alternatives.

関数に渡される引数リストは平坦なリストであるのと同様、戻り値のリストも また平坦なリストです。 ですから、関数が返した全ての要素は @a に格納され、@b は 空になります。 別のやり方については "Pass by Reference" を参照してください。

A subroutine may be called using an explicit & prefix. The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The & is not optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the &$subref() or &{$subref}() constructs, although the $subref->() notation solves that problem. See perlref for more about all that.

サブルーチンは、明示的な & というプリフィックスを付けて呼び出すことが できます。 最近の Perl では & は省略可能であり、サブルーチンがあらかじめ 宣言されている場合には括弧も省略できます。 defined() や undef() の引数として使ったような、単なる名前付き サブルーチンであるときの &省略可能ではありません&$subref()&{$subref}() のような、サブルーチンの名前や リファレンスを使った間接的なサブルーン呼び出しを行いたいたいときにも & は省略することはできません。 但し$subref->() の記法が問題を解決します。 詳しくは perlref を参照してください。

Subroutines may be called recursively. If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid.

サブルーチンは再帰的に呼び出すこともできます。 あるサブルーチンが & 付きで呼び出されたなら、引数リストは省略可能で、 もし省略されたなら、そのサブルーチンに対して @_ 配列は設定されません: 代わりに呼び出し時の @_ 配列がサブルーチンに対して可視となります。 これは新しいユーザーが避けたいと思う効果的なメカニズムです。

    &foo(1,2,3);        # pass three arguments
    foo(1,2,3);         # the same
    &foo(1,2,3);        # 三つの引数を渡す
    foo(1,2,3);         # 上と同じ
    foo();              # pass a null list
    &foo();             # the same
    foo();              # 空リストを渡す
    &foo();             # 上と同じ
    &foo;               # foo() get current args, like foo(@_) !!
    foo;                # like foo() IFF sub foo predeclared, else "foo"
    &foo;               # foo() は foo(@_) と同様現在の引数を取る!!
    foo;                # サブルーチン foo が予め定義されている場合に限り
                        # foo() と同様、それ以外では "foo"

Not only does the & form make the argument list optional, it also disables any prototype checking on arguments you do provide. This is partly for historical reasons, and partly for having a convenient way to cheat if you know what you're doing. See Prototypes below.

& 形式は引数リストを省略可能にするばかりでなく、与えられた引数に 対するすべてのプロトタイプチェックも無効にします。 これは一部には歴史的な理由であり、一部にはあなたが自分の行っている動作を わかっているときにうまくごまかしを行う便利な方法を残しておくためです。 後に出てくる Prototypes を参照してください。

Subroutines whose names are in all upper case are reserved to the Perl core, as are modules whose names are in all lower case. A subroutine in all capitals is a loosely-held convention meaning it will be called indirectly by the run-time system itself, usually due to a triggered event. Subroutines that do special, pre-defined things include AUTOLOAD, CLONE, DESTROY plus all functions mentioned in perltie and PerlIO::via.

名前が全て大文字であるサブルーチンは、 名前が全て小文字のモジュール名が (訳注: pragma 用として) 予約されているのと同じように Perl のコアで予約されています。実行時にシステム自身によって (通常はイベントをトリガーとして) 間接的に呼び出されるサブルーチンは、 名前を全て大文字で書くのがしきたりです。 特殊で、あらかじめ決められていることをするサブルーチンは、 AUTOLOAD, CLONE, DESTROY に、perltiePerlIO::via で 説明されている関数全てを加えたものです。

The BEGIN, UNITCHECK, CHECK, INIT and END subroutines are not so much subroutines as named special code blocks, of which you can have more than one in a package, and which you can not call explicitly. See "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod

The BEGIN, UNITCHECK, CHECK, INIT, END サブルーチンは サブルーチンというよりは特殊コードブロックで、その中でも一つのパッケージに 複数作ることができ、明示的には 呼び出せません"BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod を参照してください。

my() によるプライベート変数

Synopsis:

概要:

    my $foo;            # declare $foo lexically local
    my (@wid, %get);    # declare list of variables local
    my $foo = "flurp";  # declare $foo lexical, and init it
    my @oof = @bar;     # declare @oof lexical, and init it
    my $x : Foo = $y;   # similar, with an attribute applied

WARNING: The use of attribute lists on my declarations is still evolving. The current semantics and interface are subject to change. See attributes and Attribute::Handlers.

警告: my 定義での属性リストの使用はいまだ改良の途中です。 現在の文法とインターフェースは変更される可能性があります。 attributesAttribute::Handlers を参照してください。

The my operator declares the listed variables to be lexically confined to the enclosing block, conditional (if/unless/elsif/else), loop (for/foreach/while/until/continue), subroutine, eval, or do/require/use'd file. If more than one value is listed, the list must be placed in parentheses. All listed elements must be legal lvalues. Only alphanumeric identifiers may be lexically scoped--magical built-ins like $/ must currently be localized with local instead.

my 演算子は、それを囲んでいるブロック、条件文 (if/unless/elsif/else)、 ループ(for/foreach/while/until/continue)、サブルーチン、eval、 あるいは do/require/use されたファイルにレキシカルに閉じ込められる 変数を定義します。二つ以上リストアップされている場合には、そのリストは 括弧でくくられていなければなりません。 すべてのリスト要素は正しい左辺値でなければなりません。 $/ のような組み込み変数が現時点では local局所化 されなければならないのに対し、 アルファベットと数字による識別子はレキシカルでマジカルなスコープになります。

Unlike dynamic variables created by the local operator, lexical variables declared with my are totally hidden from the outside world, including any called subroutines. This is true if it's the same subroutine called from itself or elsewhere--every call gets its own copy.

local 文によって生成される動的変数とは異なり、my を使って 宣言されたレキシカル変数はそれを呼び出したサブルーチンも含め、外側の 世界からは秘匿されます。 自分自身が同じサブルーチンを呼んだ場合でさえそうです。 個々の呼び出しはそれぞれのコピーを所有します。

This doesn't mean that a my variable declared in a statically enclosing lexical scope would be invisible. Only dynamic scopes are cut off. For example, the bumpx() function below has access to the lexical $x variable because both the my and the sub occurred at the same scope, presumably file scope.

このことは静的に閉じているレキシカルスコープで宣言されている my 変数が見えなくなるということは意味しません。 動的変数だけが切り取られます。例を挙げると、以下の bumpx() は レキシカル変数 $x にアクセスします。 なぜなら、mysub の両方が同じスコープに現れているからです。

    my $x = 10;
    sub bumpx { $x++ } 

An eval(), however, can see lexical variables of the scope it is being evaluated in, so long as the names aren't hidden by declarations within the eval() itself. See perlref.

しかしながら eval() は、eval() 自身の内側にある宣言によって隠されない 名前の寿命と同じ長さを持つスコープのレキシカル変数を見ることができます。 perlref を参照してください。

The parameter list to my() may be assigned to if desired, which allows you to initialize your variables. (If no initializer is given for a particular variable, it is created with the undefined value.) Commonly this is used to name input parameters to a subroutine. Examples:

my() に対するパラーメータリストには、お望みとあらば変数を初期化するための 代入を行うことができます(変数に対して初期値が与えられなければ、 その変数は未定義値を使って生成されます)。 一般的にこの機能はサブルーチンに対する入力パラメータに名前を付けるために 使われています。例を挙げましょう:

    $arg = "fred";        # "global" variable
    $n = cube_root(27);
    print "$arg thinks the root is $n\n";
 fred thinks the root is 3

    sub cube_root {
        my $arg = shift;  # name doesn't matter
        $arg **= 1/3;
        return $arg;
    }

The my is simply a modifier on something you might assign to. So when you do assign to variables in its argument list, my doesn't change whether those variables are viewed as a scalar or an array. So

my は、あなたが代入を行いたいと考えている何かに対する修飾子です。 ですから、引数リストの中にある変数に対して代入を行うときには my は それらの変数がスカラとして見えているのか配列として見えているかという 状態を変えることはありません。ですから、

    my ($foo) = <STDIN>;                # WRONG?
    my @FOO = <STDIN>;

both supply a list context to the right-hand side, while

これらの両方ともが右辺をリストコンテキストにするのに対して、

    my $foo = <STDIN>;

supplies a scalar context. But the following declares only one variable:

これはスカラコンテキストを与えます。しかし、以下のような宣言を 行った場合、一つの変数だけが有効です:

    my $foo, $bar = 1;                  # WRONG

That has the same effect as

これは以下のように書いたのと同じことになります

    my $foo;
    $bar = 1;

The declared variable is not introduced (is not visible) until after the current statement. Thus,

宣言された変数は、その文が終了するまでは導入されません(不可視の 状態です)。 したがって、

    my $x = $x;

can be used to initialize a new $x with the value of the old $x, and the expression

これは古い $x の値を使って新しい $x を初期化するのに使うことができます。 そして

    my $x = 123 and $x == 123

is false unless the old $x happened to have the value 123.

これは古い $x の値がたまたま 123 でない限り、新しい $x には偽が 設定されます。

Lexical scopes of control structures are not bounded precisely by the braces that delimit their controlled blocks; control expressions are part of that scope, too. Thus in the loop

制御構文のレキシカルスコープは、その制御ブロックを区切る中かっこによって 厳格に束縛されることはありません; 制御式もまた、スコープの一部です。 ですから以下のループでは

    while (my $line = <>) {
        $line = lc $line;
    } continue {
        print $line;
    }

the scope of $line extends from its declaration throughout the rest of the loop construct (including the continue clause), but not beyond it. Similarly, in the conditional

$line のスコープはその宣言から(continueブロックを含む)ループ構造の残りの 部分まで拡張されますが、それを越えることはありません。 同様に、以下の条件文では

    if ((my $answer = <STDIN>) =~ /^yes$/i) {
        user_agrees();
    } elsif ($answer =~ /^no$/i) {
        user_disagrees();
    } else {
        chomp $answer;
        die "'$answer' is neither 'yes' nor 'no'";
    }

the scope of $answer extends from its declaration through the rest of that conditional, including any elsif and else clauses, but not beyond it. See "Simple Statements" in perlsyn for information on the scope of variables in statements with modifiers.

$answer のスコープはその宣言から条件文の elsifelse ブロックを含む残りの部分まで拡張されますが、 それを越えることはありません。 修飾子付きの文での変数のスコープに関する情報については "Simple Statements" in perlsyn を参照してください。

The foreach loop defaults to scoping its index variable dynamically in the manner of local. However, if the index variable is prefixed with the keyword my, or if there is already a lexical by that name in scope, then a new lexical is created instead. Thus in the loop

foreach はその添え字変数に対するスコープをデフォルトでは local のやり方で動的なものにしています。 しかしながら、添え字変数に my というキーワードが前置されていた場合、 または現在のスコープでその名前がすでにレキシカルである場合には、 新しいレキシカル変数が代わりに作られます。 ですから以下のループでは:

    for my $i (1, 2, 3) {
        some_function();
    }

the scope of $i extends to the end of the loop, but not beyond it, rendering the value of $i inaccessible within some_function().

$i のスコープはループの終端まで拡張されますが、それを越えることはないので、 $i の値は some_function() の中では参照することができなくなります。

Some users may wish to encourage the use of lexically scoped variables. As an aid to catching implicit uses to package variables, which are always global, if you say

ユーザーの一部には、レキシカルなスコープの変数の使用を奨励することを 望んでいる人もいるでしょう。 常にグローバルであるパッケージ変数に対する暗黙の使用を捕捉することを 助けるものとして、

    use strict 'vars';

then any variable mentioned from there to the end of the enclosing block must either refer to a lexical variable, be predeclared via our or use vars, or else must be fully qualified with the package name. A compilation error results otherwise. An inner block may countermand this with no strict 'vars'.

のようにすると、この文からそれを囲むブロックの終端までの間の変数の参照は、 レキシカル変数に対する参照か、our または use vars による 事前宣言か、さもなければ完全なパッケージ名で修飾した 名前でなければなりません。 それ以外のものがあるとコンパイルエラーが発生します。 これの対象となっているブロックの内側にあるブロックで no strict 'vars' とすると(内側のブロック中では)この制限を 取り消すことができます。

A my has both a compile-time and a run-time effect. At compile time, the compiler takes notice of it. The principal usefulness of this is to quiet use strict 'vars', but it is also essential for generation of closures as detailed in perlref. Actual initialization is delayed until run time, though, so it gets executed at the appropriate time, such as each time through a loop, for example.

my はコンパイル時の効果と実行時の効果の両方を持っています。 コンパイル時においては、コンパイラはその変数を認識します。 これの基本的な実用性は use strict 'vars' を黙らせるということですが、 perlref で詳細を記述しているクロージャの作成にも有用です。 実際の初期化は実行時まで遅らされ、そのためたとえばループを通る度に 適切に実行されるのです。

Variables declared with my are not part of any package and are therefore never fully qualified with the package name. In particular, you're not allowed to try to make a package variable (or other global) lexical:

my によって宣言された変数はどのパッケージの一部でもなく、 そのためパッケージ名を使って修飾されることは決してありません。 特に、パッケージ変数(もしくはその他のグローバルな変数)を レキシカルにしようとすることは許されていません。

    my $pack::var;      # ERROR!  Illegal syntax

In fact, a dynamic variable (also known as package or global variables) are still accessible using the fully qualified :: notation even while a lexical of the same name is also visible:

実際のところ、動的変数(パッケージ変数やグローバル変数として 知られているもの)は、同じ名前を持ったレキシカルが可視な状態であったとしても、 :: 記法を使った(名前の)完全な修飾を行うことによって まだアクセス可能なのです:

    package main;
    local $x = 10;
    my    $x = 20;
    print "$x and $::x\n";

That will print out 20 and 10.

この例は 2010 を出力します。

You may declare my variables at the outermost scope of a file to hide any such identifiers from the world outside that file. This is similar in spirit to C's static variables when they are used at the file level. To do this with a subroutine requires the use of a closure (an anonymous function that accesses enclosing lexicals). If you want to create a private subroutine that cannot be called from outside that block, it can declare a lexical variable containing an anonymous sub reference:

このファイルの外側の世界からこのような識別子を隠すために、my 変数を ファイルの最も外側のスコープで宣言することができます。 これは、概念としては C でのファイルレベルの static 変数と似ています。 これをサブルーチンの内側で行うには、 クロージャ(レキシカルアクセスを伴った無名関数)を使います。 ブロックで外側のブロックから呼び出すことのできないようなプライベートな サブルーチンを作りたいなら、無名サブルーチンのリファレンスを 保持するレキシカル変数を宣言することができます:

    my $secret_version = '1.001-beta';
    my $secret_sub = sub { print $secret_version };
    &$secret_sub();

As long as the reference is never returned by any function within the module, no outside module can see the subroutine, because its name is not in any package's symbol table. Remember that it's not REALLY called $some_pack::secret_version or anything; it's just $secret_version, unqualified and unqualifiable.

このリファレンスが決して、モジュールの内側にあるどんな関数からも 返されることがないのと同様に、外側のモジュールはサブルーチンを 見ることができません。 なぜなら、その名前はどのパッケージのシンボルテーブルにも 存在していないからです。 $some_pack::secret_version などの手段を使って呼び出すことは できない のだということを思い出してください。 これは単なる $secret_version であって、修飾されることはなく 修飾することはできません。

This does not work with object methods, however; all object methods have to be in the symbol table of some package to be found. See "Function Templates" in perlref for something of a work-around to this.

しかしこれはオブジェクトメソッドと共に動作させることはできません。 全てのオブジェクトメソッドは、発見可能な何らかのパッケージの シンボルテーブルに存在している必要があります。 これを回避する方法については "Function Templates" in perlref を 参照してください。

永続的なプライベート変数

There are two ways to build persistent private variables in Perl 5.10. First, you can simply use the state feature. Or, you can use closures, if you want to stay compatible with releases older than 5.10.

Perl 5.10 で永続的プライベート変数を構築するには二つの方法があります。 一つ目は単に state 機能を使うことです。 あるいは、5.10 よりも古いリリースとの互換性を維持したいなら、クロージャを 使うことです。

state() による永続変数

Beginning with perl 5.9.4, you can declare variables with the state keyword in place of my. For that to work, though, you must have enabled that feature beforehand, either by using the feature pragma, or by using -E on one-liners. (see feature)

perl 5.9.4 から、my の代わりに state キーワードを使って変数を 宣言できます。 しかし、これを働かせるには、feature プラグマを使うか、一行野郎では -E を使うことで予めこの機能を有効にしなければなりません。 (feature を参照してください)

For example, the following code maintains a private counter, incremented each time the gimme_another() function is called:

例えば、以下のコードはプライベートなカウンタを管理し、gimme_another() 関数が 呼び出されるたびにカウンタを増加させます:

    use feature 'state';
    sub gimme_another { state $x; return ++$x }

Also, since $x is lexical, it can't be reached or modified by any Perl code outside.

また、$x はレキシカルなので、その外側の Perl コードからアクセスすることは できません。

When combined with variable declaration, simple scalar assignment to state variables (as in state $x = 42) is executed only the first time. When such statements are evaluated subsequent times, the assignment is ignored. The behavior of this sort of assignment to non-scalar variables is undefined.

変数宣言と結び付けられたとき、(state $x = 42 のような)state 変数への 単純なスカラ代入は一度目だけ実行されます。 その後再びこの文が評価されても、代入は無視されます。 非スカラ変数へのこの種の代入の振る舞いは未定義です。

クロージャによる永続変数

Just because a lexical variable is lexically (also called statically) scoped to its enclosing block, eval, or do FILE, this doesn't mean that within a function it works like a C static. It normally works more like a C auto, but with implicit garbage collection.

レキシカル変数はそれを囲むブロック、evaldo FILE に属する レキシカル(もしくは静的)スコープに属します。 このことは C の static と同様に動作するということを意味しません。 通常は C の auto と同じように動作しますが、 暗黙のガベージコレクションを伴っています。

Unlike local variables in C or C++, Perl's lexical variables don't necessarily get recycled just because their scope has exited. If something more permanent is still aware of the lexical, it will stick around. So long as something else references a lexical, that lexical won't be freed--which is as it should be. You wouldn't want memory being free until you were done using it, or kept around once you were done. Automatic garbage collection takes care of this for you.

C や C++ におけるローカル変数とは異なり、Perl のレキシカル変数は 単にそのスコープから抜けたからという理由で、必ずしもリサイクルされません。 何かもっと永続的なものがそのレキシカル変数に関してまだ気付いていれば、 それは留まるでしょう。 何か他のものがレキシカル変数を参照していれば、そのレキシカル変数は 解放されません -- そしてそうあるべきです。 あなたはそれを使いおわるまではメモリを解放したいとは思わないでしょうし、 使い終わったものを保持し続けたいとも思わないでしょう。 あなたのために自動ガベージコレクションがこれを行います。

This means that you can pass back or save away references to lexical variables, whereas to return a pointer to a C auto is a grave error. It also gives us a way to simulate C's function statics. Here's a mechanism for giving a function private variables with both lexical scoping and a static lifetime. If you do want to create something like C's static variables, just enclose the whole function in an extra block, and put the static variable outside the function but in the block.

これはつまり、レキシカル変数に対するリファレンスを返したり 保存したりすることができるということです。 一方 C の auto 変数に対するポインタを返すことはエラーとなります。 レキシカル変数はまた、C の関数に static な変数のシミュレートも行います。 以下の例はレキシカルスコープを持つと同時に static な寿命を持つ関数に プライベートな変数です。 C の static 変数のようなものを作りたいというのであれば、 関数全体をさらにブロックで囲んでやり、static 変数をブロックの内側で、 かつ関数の外側の場所においてやります。

    {
        my $secret_val = 0;
        sub gimme_another {
            return ++$secret_val;
        }
    }
    # $secret_val now becomes unreachable by the outside
    # world, but retains its value between calls to gimme_another

If this function is being sourced in from a separate file via require or use, then this is probably just fine. If it's all in the main program, you'll need to arrange for the my to be executed early, either by putting the whole block above your main program, or more likely, placing merely a BEGIN code block around it to make sure it gets executed before your program starts to run:

この関数が requireuse を通じて別の独立したファイルから 取り込まれた場合、これはとても役に立つことでしょう。 もしこれがすべてメインプログラムの中にあるのであれば、 ブロック全体をメインプログラムの前に置くか、(こちらか好ましいやり方ですが) プログラムが実行されるよりも前に実行されることが保証されている BEGIN コードブロックの中に置くようにして、my を早めに実行するように 変更する必要があるでしょう:

    BEGIN {
        my $secret_val = 0;
        sub gimme_another {
            return ++$secret_val;
        }
    }

See "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod about the special triggered code blocks, BEGIN, UNITCHECK, CHECK, INIT and END.

特別なトリガーコードブロックである BEGIN, CHECK, INIT, END に ついては "BEGIN, UNITCHECK, CHECK, INIT and END" in perlmod を参照してください。

If declared at the outermost scope (the file scope), then lexicals work somewhat like C's file statics. They are available to all functions in that same file declared below them, but are inaccessible from outside that file. This strategy is sometimes used in modules to create private variables that the whole module can see.

最も外側のスコープ(ファイルスコープ)で宣言されたのであれば、 レキシカル変数は C のファイルに static なものと同じように振る舞います。 同じファイルの、宣言の後にある全ての関数から参照可能でありますが、 そのファイルの外側から参照することはできません。 この戦略はモジュールの中でモジュール全体から見える プライベートな変数を作るために使われます。

local() を使った一時的な値

WARNING: In general, you should be using my instead of local, because it's faster and safer. Exceptions to this include the global punctuation variables, global filehandles and formats, and direct manipulation of the Perl symbol table itself. local is mostly used when the current value of a variable must be visible to called subroutines.

警告: 一般的には、local ではなくmy を使うべきです。 なぜなら、そちらのほうが早く、安全だからです。 この例外には、グローバルな 句読点変数(punctuation variable)、グローバルファイルハンドル、フォーマット、 そして Perl のシンボルテーブル自身に対する直接の操作が含まれます。 local はほとんどの場合、変数の現在の値が呼び出されたサブルーチンに 対して可視にしなければならない場合に使われます。

Synopsis:

概要:

    # localization of values
    # 値のローカル化
    local $foo;                 # make $foo dynamically local
    local (@wid, %get);         # make list of variables local
    local $foo = "flurp";       # make $foo dynamic, and init it
    local @oof = @bar;          # make @oof dynamic, and init it
    local $foo;                 # $foo を動的にローカルにする
    local (@wid, %get);         # 変数のリストをローカルにする
    local $foo = "flurp";       # $foo を動的にして、初期化する
    local @oof = @bar;          # @oof を動的にして、初期化する
    local $hash{key} = "val";   # sets a local value for this hash entry
    delete local $hash{key};    # delete this entry for the current block
    local ($cond ? $v1 : $v2);  # several types of lvalues support
                                # localization
    local $hash{key} = "val";   # このハッシュエントリにローカルな値を入れる
    delete local $hash{key};    # 現在のブロックでこのエントリを削除
    local ($cond ? $v1 : $v2);  # いくつかの種類の左辺値はローカル化に
                                # 対応している
    # localization of symbols
    # シンボルのローカル化
    local *FH;                  # localize $FH, @FH, %FH, &FH  ...
    local *merlyn = *randal;    # now $merlyn is really $randal, plus
                                #     @merlyn is really @randal, etc
    local *merlyn = 'randal';   # SAME THING: promote 'randal' to *randal
    local *merlyn = \$randal;   # just alias $merlyn, not @merlyn etc
    local *FH;                  # $FH, @FH, %FH, &FH ... をローカル化
    local *merlyn = *randal;    # ここで $merlyn は実際には $randal、さらに
                                #        @merlyn は実際には @randal、など
    local *merlyn = 'randal';   # 「同様」: 'randal' を *randal にする
    local *merlyn = \$randal;   # 単に $merlyn の別名で @merlyn などではない

A local modifies its listed variables to be "local" to the enclosing block, eval, or do FILE--and to any subroutine called from within that block. A local just gives temporary values to global (meaning package) variables. It does not create a local variable. This is known as dynamic scoping. Lexical scoping is done with my, which works more like C's auto declarations.

local は、引数に取ったそのリスト中の変数を(local() を囲む)ブロック (あるいは、サブルーチン, evaldo FILE)と、そのブロック中で 呼び出された全てのもの にローカルなものにします。 local は単にグローバル変数(やパッケージ変数)に一時的な値を与えるだけです。 これは動的スコープとして知られています。 レキシカルスコープは my を使って行われ、これは C の 自動変数宣言のように 働きます。

Some types of lvalues can be localized as well : hash and array elements and slices, conditionals (provided that their result is always localizable), and symbolic references. As for simple variables, this creates new, dynamically scoped values.

いくつかの種類の左辺値もローカル化できます : ハッシュと配列の要素とスライス、 条件文(その結果が常にローカル化可能なもの)、シンボリックリファレンス。 単純変数に関しては、これは新しく、動的スコープを持つ値を作ります。

If more than one variable or expression is given to local, they must be placed in parentheses. This operator works by saving the current values of those variables in its argument list on a hidden stack and restoring them upon exiting the block, subroutine, or eval. This means that called subroutines can also reference the local variable, but not the global one. The argument list may be assigned to if desired, which allows you to initialize your local variables. (If no initializer is given for a particular variable, it is created with an undefined value.)

local に対して二つ以上の変数や表現を与えるのならば、それらの変数は括弧で くくっておかなければなりません。 この演算子は引数リストにある変数のカレントの値を隠れたスタックに保存し、 ブロックやサブルーチン、eval から抜けるときにその値を復帰することによって 動作しています。 これはつまり、呼び出されたサブルーチンからもローカル変数を 参照することができるけれども、グローバルなものを見ることは できないということです。 引数リストには、好みに応じてリスト中のローカル変数を初期化するための 代入を行うことができます(ある特定の変数に対して初期値が 与えられなかった場合には、その変数は未定義値を伴って生成されます)。

Because local is a run-time operator, it gets executed each time through a loop. Consequently, it's more efficient to localize your variables outside the loop.

local は実行時演算子なので、ループで通る度に実行されます。 現在の Perl はメモリの使用に関して改善されていますが、 結果として、ループの外側で変数を宣言したほうがより効率が良いのです。

local() の文法的注意

A local is simply a modifier on an lvalue expression. When you assign to a localized variable, the local doesn't change whether its list is viewed as a scalar or an array. So

local は左辺値に対する単なる修飾子です。 局所化された変数に代入を行ったとき、local はそのリストがスカラとして 見えているのか配列として見えているかということを変えることはありません。 ですから、

    local($foo) = <STDIN>;
    local @FOO = <STDIN>;

both supply a list context to the right-hand side, while

これらの両方ともが右辺をリストコンテキストにするのに対して、

    local $foo = <STDIN>;

supplies a scalar context.

これはスカラコンテキストを与えます.

特殊変数のローカル化

If you localize a special variable, you'll be giving a new value to it, but its magic won't go away. That means that all side-effects related to this magic still work with the localized value.

特殊変数をローカル化すると、それに新しい値を入れられますが、特殊機能は なくなりません。 これは、その特殊機能に関係する全ての副作用はローカル化された値に対しても 働くということを意味します。

This feature allows code like this to work :

この機能は以下のようなコードが動作するようにします:

    # Read the whole contents of FILE in $slurp
    { local $/ = undef; $slurp = <FILE>; }

Note, however, that this restricts localization of some values ; for example, the following statement dies, as of perl 5.9.0, with an error Modification of a read-only value attempted, because the $1 variable is magical and read-only :

しかし、これはいくつかの値のローカル化を制限することに注意してください; 例えば、以下の文は、perl 5.9.0 からは、 Modification of a read-only value attempted というエラーで die します; なぜなら $1 変数はマジカルで読み込み専用だからです:

    local $1 = 2;

One exception is the default scalar variable: starting with perl 5.14 local($_) will always strip all magic from $_, to make it possible to safely reuse $_ in a subroutine.

一つの例外は、デフォルトスカラ変数です: perl 5.14 から、サブルーチン内で 安全に $_ を再利用できるように、local($_) は常に 全てのマジックを取り除きます。

WARNING: Localization of tied arrays and hashes does not currently work as described. This will be fixed in a future release of Perl; in the meantime, avoid code that relies on any particular behaviour of localising tied arrays or hashes (localising individual elements is still okay). See "Localising Tied Arrays and Hashes Is Broken" in perl58delta for more details.

警告: tie された配列とハッシュのローカル化は現在のところ 記述されたとおりに動作しません。 これは Perl の将来のリリースで修正されます; 当面の間は、tie された配列やハッシュのローカル化によるどのような 振る舞いにも依存しないようなコードにしてください (個々の要素のローカル化は問題ありません)。 さらなる詳細については "Localising Tied Arrays and Hashes Is Broken" in perl58delta を 参照してください。

グロブのローカル化

The construct

以下のような構造は

    local *name;

creates a whole new symbol table entry for the glob name in the current package. That means that all variables in its glob slot ($name, @name, %name, &name, and the name filehandle) are dynamically reset.

現在のパッケージでグロブ name のための完全に新しいシンボルテーブル エントリを作成します。 これは、このグロブスロットに入る全ての変数 ($name, @name, %name, &name, name ファイルハンドル) は動的にリセットされることを意味します。

This implies, among other things, that any magic eventually carried by those variables is locally lost. In other words, saying local */ will not have any effect on the internal value of the input record separator.

これは特に、 最終的にこれらの変数によってもたらされるマジックは局所的には 失われることを意味します。 言い換えると、local */ としても、入力レコードセパレータの内部の 値には何の影響も与えないということです。

複合型の要素のローカル化

It's also worth taking a moment to explain what happens when you localize a member of a composite type (i.e. an array or hash element). In this case, the element is localized by name. This means that when the scope of the local() ends, the saved value will be restored to the hash element whose key was named in the local(), or the array element whose index was named in the local(). If that element was deleted while the local() was in effect (e.g. by a delete() from a hash or a shift() of an array), it will spring back into existence, possibly extending an array and filling in the skipped elements with undef. For instance, if you say

合成型のメンバー(たとえば配列やハッシュの要素)の局所化したときに どうなるかを説明しましょう。 この場合、その要素は 名前によって 局所化が行われます。 これはつまり、local のスコープが終わったときに、local で名前が 使われていたキーを持つハッシュの要素と local で名前が使われていた 配列要素に関して保存されていた値を復帰するということです。 local() が効果を持っているところでそういった要素が削除された場合 (例えばハッシュに対して delete() を使うとか配列に対して shift()を使う)に、local() の有効範囲を抜けたところで 削除された要素が復活し、配列の拡張と拡張した分の要素の値を undef にするということを行います。例えば以下のような場合、

    %hash = ( 'This' => 'is', 'a' => 'test' );
    @ary  = ( 0..5 );
    {
         local($ary[5]) = 6;
         local($hash{'a'}) = 'drill';
         while (my $e = pop(@ary)) {
             print "$e . . .\n";
             last unless $e > 3;
         }
         if (@ary) {
             $hash{'only a'} = 'test';
             delete $hash{'a'};
         }
    }
    print join(' ', map { "$_ $hash{$_}" } sort keys %hash),".\n";
    print "The array has ",scalar(@ary)," elements: ",
          join(', ', map { defined $_ ? $_ : 'undef' } @ary),"\n";

Perl will print

Perl は以下のような出力をします。

    6 . . .
    4 . . .
    3 . . .
    This is a test only a test.
    The array has 6 elements: 0, 1, 2, undef, undef, 5

The behavior of local() on non-existent members of composite types is subject to change in future.

複合型の存在していないメンバーに対する local() の振る舞いは 将来変更される予定です。

Localized deletion of elements of composite types

You can use the delete local $array[$idx] and delete local $hash{key} constructs to delete a composite type entry for the current block and restore it when it ends. They return the array/hash value before the localization, which means that they are respectively equivalent to

現在のブロックからある複合型エントリを削除してブロックの終了時に 復元するために、delete local $array[$idx] および delete local $hash{key} 構文を使えます。 これらはローカル化される前に配列/ハッシュの値を返すので、以下はそれぞれ 等価です:

    do {
        my $val = $array[$idx];
        local  $array[$idx];
        delete $array[$idx];
        $val
    }

and

および

    do {
        my $val = $hash{key};
        local  $hash{key};
        delete $hash{key};
        $val
    }

except that for those the local is scoped to the do block. Slices are also accepted.

但し localdo ブロックのスコープです。 スライスも受け付けられます。

    my %hash = (
     a => [ 7, 8, 9 ],
     b => 1,
    )

    {
     my $a = delete local $hash{a};
     # $a is [ 7, 8, 9 ]
     # %hash is (b => 1)

     {
      my @nums = delete local @$a[0, 2]
      # @nums is (7, 9)
      # $a is [ undef, 8 ]

      $a[0] = 999; # will be erased when the scope ends
     }
     # $a is back to [ 7, 8, 9 ]

    }
    # %hash is back to its original state

左辺値サブルーチン

WARNING: Lvalue subroutines are still experimental and the implementation may change in future versions of Perl.

警告: 左辺値サブルーチンは未だ実験的機能であり、 将来のバージョンの Perl では実装が変更されるかもしれません。

It is possible to return a modifiable value from a subroutine. To do this, you have to declare the subroutine to return an lvalue.

サブルーチンから、変更可能な値を返すことが可能です。 そうするためには、サブルーチンが左辺値を返すように宣言しなければなりません。

    my $val;
    sub canmod : lvalue {
        # return $val; this doesn't work, don't say "return"
        $val;
    }
    sub nomod {
        $val;
    }

    canmod() = 5;   # assigns to $val
    nomod()  = 5;   # ERROR

The scalar/list context for the subroutine and for the right-hand side of assignment is determined as if the subroutine call is replaced by a scalar. For example, consider:

サブルーチンと、代入の右側のスカラ/リストコンテキストは サブルーチン呼び出しがスカラに置き換えられたかのようにして 決定されます。 例として、以下を考えると:

    data(2,3) = get_data(3,4);

Both subroutines here are called in a scalar context, while in:

両方のサブルーチンはスカラコンテキストで呼び出され、一方:

    (data(2,3)) = get_data(3,4);

and in:

及び:

    (data(2),data(3)) = get_data(3,4);

all the subroutines are called in a list context.

については全てのサブルーチンはリストコンテキストで呼び出されます。

Lvalue subroutines are EXPERIMENTAL

They appear to be convenient, but there are several reasons to be circumspect.

これは便利なようですが、慎重になるべきいくつかの理由があります。

You can't use the return keyword, you must pass out the value before falling out of subroutine scope. (see comment in example above). This is usually not a problem, but it disallows an explicit return out of a deeply nested loop, which is sometimes a nice way out.

return キーワードは使えず、サブルーチンスコープから抜ける前に値を 渡さなければなりません。 (上述の例のコメントを参照してください)。 これは普通は問題になりませんが、深くネストしたループから明示的に return で 脱出するのは、時々よい方法となりますが、出来なくなります。

They violate encapsulation. A normal mutator can check the supplied argument before setting the attribute it is protecting, an lvalue subroutine never gets that chance. Consider;

これはカプセル化に違反しています。 通常の mutator は、自身が保護している属性にセットする前に 引数をチェックできますが、 左辺値サブルーチンにはその機会はありません。 以下を考えてみてください;

    my $some_array_ref = [];    # protected by mutators ??

    sub set_arr {               # normal mutator
        my $val = shift;
        die("expected array, you supplied ", ref $val)
           unless ref $val eq 'ARRAY';
        $some_array_ref = $val;
    }
    sub set_arr_lv : lvalue {   # lvalue mutator
        $some_array_ref;
    }

    # set_arr_lv cannot stop this !
    set_arr_lv() = { a => 1 };

シンボルテーブルのエントリを渡す(型グロブ)

WARNING: The mechanism described in this section was originally the only way to simulate pass-by-reference in older versions of Perl. While it still works fine in modern versions, the new reference mechanism is generally easier to work with. See below.

警告: このセクションで説明されている仕組みは、古いバージョンの Perl に おいて参照渡しをシミュレートするための唯一の方法でした。 これは現在でも使うことができるのですが、新しいリファレンスの仕組みは これをより簡単に行います。後の説明を参照してください。

Sometimes you don't want to pass the value of an array to a subroutine but rather the name of it, so that the subroutine can modify the global copy of it rather than working with a local copy. In perl you can refer to all objects of a particular name by prefixing the name with a star: *foo. This is often known as a "typeglob", because the star on the front can be thought of as a wildcard match for all the funny prefix characters on variables and subroutines and such.

サブルーチンが渡された配列のローカルなコピーではなくてグローバルな ものの変更ができるように、サブルーチンに対して配列の値ではなく 配列の名前を渡したくなることもあるでしょう。 perl においては、これを *foo のようにアスタリスクを使って 行うことができます。 これは前に置かれたアスタリスクが変数やサブルーチンなどに対して使われる 前置キャラクター全てにマッチするワイルドカードとしてみなすことが できるので、“型グロブ”としてよく知られています。

When evaluated, the typeglob produces a scalar value that represents all the objects of that name, including any filehandle, format, or subroutine. When assigned to, it causes the name mentioned to refer to whatever * value was assigned to it. Example:

型グロブは評価されたときにその名前を持つ全てのオブジェクト(ファイルハンドル、 フォーマット、サブルーチンも含まれます)を表すスカラ値を生成します。 代入が行われたとき、* は代入したものを反映するようになります。例えば:

    sub doubleary {
        local(*someary) = @_;
        foreach $elem (@someary) {
            $elem *= 2;
        }
    }
    doubleary(*foo);
    doubleary(*bar);

Scalars are already passed by reference, so you can modify scalar arguments without using this mechanism by referring explicitly to $_[0] etc. You can modify all the elements of an array by passing all the elements as scalars, but you have to use the * mechanism (or the equivalent reference mechanism) to push, pop, or change the size of an array. It will certainly be faster to pass the typeglob (or reference).

スカラは既に参照渡しされているので、陽に $_[0] などで参照して この機構を使わなくてもスカラ引数を変更することができます。 全ての要素がスカラとして渡された配列の要素はすべて 変更することができますが、pushpop、もしくは配列のサイズを 変更するためには * 機構(もしくは同等のリファレンス機構)を 使う必要があります。 これは型グロブ(もしくはリファレンス)を渡すのを確実に高速にするでしょう。

Even if you don't want to modify an array, this mechanism is useful for passing multiple arrays in a single LIST, because normally the LIST mechanism will merge all the array values so that you can't extract out the individual arrays. For more on typeglobs, see "Typeglobs and Filehandles" in perldata.

配列の変更を望まないとしても、この機構は単一のリストの中にある複数の リストを渡すのに便利です。 なぜなら、通常はリスト機構はすべての配列の値を結合してしまうので 独立した配列を取り出すことができないからです。 型グロブについては、 "Typeglobs and Filehandles" in perldata も参照してください。

今でも local() を使うとき

Despite the existence of my, there are still three places where the local operator still shines. In fact, in these three places, you must use local instead of my.

my があるにも関らず、今でも local 演算子を使うべき場所が三ヶ所あります。 実際にはその三ヶ所においては、my ではなく、必ず local を 使わなければなりません。

  1. You need to give a global variable a temporary value, especially $_.

    グローバル変数(特に $_) に関して、一時的な値を与えたいとき。

    The global variables, like @ARGV or the punctuation variables, must be localized with local(). This block reads in /etc/motd, and splits it up into chunks separated by lines of equal signs, which are placed in @Fields.

    @ARGV や句読点変数(punctuation variables)のようなグローバル変数では、 局所化のために local() を使わなければなりません。 以下のブロックは /etc/motd を読み込み、等価記号を使って行の塊を分割し、 その結果を @Fields に格納します。

        {
            local @ARGV = ("/etc/motd");
            local $/ = undef;
            local $_ = <>;  
            @Fields = split /^\s*=+\s*$/;
        } 

    It particular, it's important to localize $_ in any routine that assigns to it. Look out for implicit assignments in while conditionals.

    特に重要なのは、任意のルーチンの中で $_ を局所化し、それに対して 代入することです。 while 文での暗黙的な代入に注意してください。

  2. You need to create a local file or directory handle or a local function.

    ローカルなファイルハンドルやディレクトリハンドル、 もしくはローカル関数を作成する必要がある場合。

    A function that needs a filehandle of its own must use local() on a complete typeglob. This can be used to create new symbol table entries:

    関数に固有のファイルハンドルが必要な関数は、完全な型グロブを使った local() を使わなければなりません。 これによって、新しいシンボルテーブルのエントリが作成されます:

        sub ioqueue {
            local  (*READER, *WRITER);    # not my!
            pipe    (READER,  WRITER)     or die "pipe: $!";
            return (*READER, *WRITER);
        }
        ($head, $tail) = ioqueue();

    See the Symbol module for a way to create anonymous symbol table entries.

    無名シンボルテーブルを生成する方法については、Symbol モジュールを 参照してください。

    Because assignment of a reference to a typeglob creates an alias, this can be used to create what is effectively a local function, or at least, a local alias.

    型グロブに対するリファレンスの代入はエイリアスを生成するので、 以下の例では効果的にローカル関数(あるいは少なくともローカルエイリアス)を 生成するのに使うことができます。

        {
            local *grow = \&shrink; # only until this block exists
            grow();                 # really calls shrink()
            move();                 # if move() grow()s, it shrink()s too
        }
        grow();                     # get the real grow() again

    See "Function Templates" in perlref for more about manipulating functions by name in this way.

    こういったやり方で、名前によって関数を操作する方法に関しての詳細は "Function Templates" in perlref を参照してください。

  3. You want to temporarily change just one element of an array or hash.

    配列やハッシュのある要素だけを一時的に変更したい場合。

    You can localize just one element of an aggregate. Usually this is done on dynamics:

    一つの要素だけを局所化することが可能です。 通常はこれは動的に行われます。

        {
            local $SIG{INT} = 'IGNORE';
            funct();                            # uninterruptible
        } 
        # interruptibility automatically restored here

    But it also works on lexically declared aggregates. Prior to 5.005, this operation could on occasion misbehave.

    しかし、これはレキシカル変数であっても動作します。 5.005 より前のものでは、この操作は間違った状況になる可能性がありました。

参照渡し

If you want to pass more than one array or hash into a function--or return them from it--and have them maintain their integrity, then you're going to have to use an explicit pass-by-reference. Before you do that, you need to understand references as detailed in perlref. This section may not make much sense to you otherwise.

もし、配列やハッシュを二つ以上関数に渡したいとか関数から返したいと 考えていて、同時にそれらを完全な状態で扱いたいというのであれば、 明示的に参照渡しを使う必要があるでしょう。 これを行う前に、perlref で説明されているリファレンスを理解する 必要があります。 このセクションでは改めて説明するようなことはしません。

Here are a few simple examples. First, let's pass in several arrays to a function and have it pop all of then, returning a new list of all their former last elements:

幾つか単純な例を挙げましょう。 まず最初に、ある関数に幾つかの配列を渡し、 全ての配列に対して pop を行って引数として受け取った配列の それぞれの最後の要素からなる新しいリストを返すということを 考えてみましょう。

    @tailings = popmany ( \@a, \@b, \@c, \@d );

    sub popmany {
        my $aref;
        my @retlist = ();
        foreach $aref ( @_ ) {
            push @retlist, pop @$aref;
        }
        return @retlist;
    }

Here's how you might write a function that returns a list of keys occurring in all the hashes passed to it:

以下の例は、引数として渡された全てのハッシュにあるキーのリストを 返す関数です:

    @common = inter( \%foo, \%bar, \%joe );
    sub inter {
        my ($k, $href, %seen); # locals
        foreach $href (@_) {
            while ( $k = each %$href ) {
                $seen{$k}++;
            }
        }
        return grep { $seen{$_} == @_ } keys %seen;
    }

So far, we're using just the normal list return mechanism. What happens if you want to pass or return a hash? Well, if you're using only one of them, or you don't mind them concatenating, then the normal calling convention is ok, although a little expensive.

とはいうものの、これは通常のリストを返す機構を使っています。 ハッシュを渡そうとしたり、ハッシュを返そうとすると何が起きるのでしょうか? そうです、引数の一つだけを使い引数の連結が行われることを気にしなければ 通常の呼び出し規約と同じことです。 ただし、少々高くつきます。

Where people get into trouble is here:

このように書くのはトラブルのもとです:

    (@a, @b) = func(@c, @d);
or
    (%a, %b) = func(%c, %d);

That syntax simply won't work. It sets just @a or %a and clears the @b or %b. Plus the function didn't get passed into two separate arrays or hashes: it got one long list in @_, as always.

これらの構文は単純にうまくいきません。 戻り値は @a%aだけにセットされて、@b%b はクリアされます。 それに加え、この関数は引数として二つの配列、二つのハッシュを受け取りません。 受け取るのは常に @_ に格納されている(二つの引数の内容が連結された)一つの 長いリストなのです。

If you can arrange for everyone to deal with this through references, it's cleaner code, although not so nice to look at. Here's a function that takes two array references as arguments, returning the two array elements in order of how many elements they have in them:

これをリファレンス経由で扱うように変更できるのであれば、見た目はそれ程 良くありませんがプログラムを明確にできます。 以下の例は引数として配列のリファレンスを二つ取り、その配列の要素の数によって 順序づけられた二つの配列を返す関数です:

    ($aref, $bref) = func(\@c, \@d);
    print "@$aref has more than @$bref\n";
    sub func {
        my ($cref, $dref) = @_;
        if (@$cref > @$dref) {
            return ($cref, $dref);
        } else {
            return ($dref, $cref);
        }
    }

It turns out that you can actually do this also:

これは以下のように書くこともできます:

    (*a, *b) = func(\@c, \@d);
    print "@a has more than @b\n";
    sub func {
        local (*c, *d) = @_;
        if (@c > @d) {
            return (\@c, \@d);
        } else {
            return (\@d, \@c);
        }
    }

Here we're using the typeglobs to do symbol table aliasing. It's a tad subtle, though, and also won't work if you're using my variables, because only globals (even in disguise as locals) are in the symbol table.

この例では、シンボルテーブルの別名づけをするために型グロブを使っています。 しかし、これは微妙なものであり、my 変数を使った場合にはうまくいきません。 なぜなら、グローバルなもの(local() で偽装したものを含む)だけが シンボルテーブルにあるからです。

If you're passing around filehandles, you could usually just use the bare typeglob, like *STDOUT, but typeglobs references work, too. For example:

ファイルハンドルを扱おうというのであれば、通常は *STDOUT のような 裸の型グロブ(bare typeglob)を使うことができますが、型グロブの リファレンスも動作します。 例を挙げましょう:

    splutter(\*STDOUT);
    sub splutter {
        my $fh = shift;
        print $fh "her um well a hmmm\n";
    }

    $rec = get_rec(\*STDIN);
    sub get_rec {
        my $fh = shift;
        return scalar <$fh>;
    }

If you're planning on generating new filehandles, you could do this. Notice to pass back just the bare *FH, not its reference.

新しいファイルハンドルを生成することを考えているのであれば、 以下のようにできます。 リファレンスではなく、生の *FH を渡すことに注意してください。

    sub openit {
        my $path = shift;
        local *FH;
        return open (FH, $path) ? *FH : undef;
    }

プロトタイプ

Perl supports a very limited kind of compile-time argument checking using function prototyping. If you declare

Perl はとても限られた形のコンパイル時引数チェックに対応しています。 以下のように宣言すると:

    sub mypush (+@)

then mypush() takes arguments exactly like push() does. The function declaration must be visible at compile time. The prototype affects only interpretation of new-style calls to the function, where new-style is defined as not using the & character. In other words, if you call it like a built-in function, then it behaves like a built-in function. If you call it like an old-fashioned subroutine, then it behaves like an old-fashioned subroutine. It naturally falls out from this rule that prototypes have no influence on subroutine references like \&foo or on indirect subroutine calls like &{$subref} or $subref->().

mypush()push() が取るのとまったく同じ引数を取ります。 関数宣言はコンパイル時に可視でなければなりません。 プロトタイプの効果は、関数を & を使わない新しい形式の呼び出しで 解釈したときのみです。 言い換えれば、組み込み関数と同じように関数を呼び出せば、 それは組み込み関数と同じように振る舞う、ということです。 古い形式のサブルーチンと同じように呼び出せば、それは古い形式の サブルーチンと同じように振る舞います。 \&foo のようなサブルーチンのリファレンスや &{$subref} のような 間接的なサブルーチン呼び出し、$subref->() に関してはプロトタイプは なんの影響も及ぼさないので、この規則から外れます。

Method calls are not influenced by prototypes either, because the function to be called is indeterminate at compile time, since the exact code called depends on inheritance.

メソッド呼び出しはプロトタイプを行う/行わないによる影響を受けません。 なぜなら正確な呼び出しコードは、継承に依存しているために コンパイル時には不確定だからです。

Because the intent of this feature is primarily to let you define subroutines that work like built-in functions, here are prototypes for some other functions that parse almost exactly like the corresponding built-in.

組み込み関数のように動作するサブルーチンをあなたに定義させるのが この機能の基本的な目的なので、ここで対応した組み込み関数のように 解釈される幾つかの関数プロトタイプを挙げておきましょう。

    Declared as                 Called as
    宣言                  呼び出し

    sub mylink ($$)          mylink $old, $new
    sub myvec ($$$)          myvec $var, $offset, 1
    sub myindex ($$;$)       myindex &getstring, "substr"
    sub mysyswrite ($$$;$)   mysyswrite $buf, 0, length($buf) - $off, $off
    sub myreverse (@)        myreverse $a, $b, $c
    sub myjoin ($@)          myjoin ":", $a, $b, $c
    sub mypop (+)            mypop @array
    sub mysplice (+$$@)      mysplice @array, 0, 2, @pushme
    sub mykeys (+)           mykeys %{$hashref}
    sub myopen (*;$)         myopen HANDLE, $name
    sub mypipe (**)          mypipe READHANDLE, WRITEHANDLE
    sub mygrep (&@)          mygrep { /foo/ } $a, $b, $c
    sub myrand (;$)          myrand 42
    sub mytime ()            mytime

Any backslashed prototype character represents an actual argument that must start with that character (optionally preceded by my, our or local), with the exception of $, which will accept a hash or array element even without a dollar sign, such as my_function()->[0]. The value passed as part of @_ will be a reference to the actual argument given in the subroutine call, obtained by applying \ to that argument.

バックスラッシュが付けられたプロトタイプ文字は、実引数が その文字で始まるものでなければならないことを表します (オプションとして my, our, local が前置されます); my_function()->[0] のようにマークなしでもハッシュや配列の要素を 受け付ける $ は例外です。 @_ の一部として渡された引数は、そのサブルーチン呼び出しにおいて 与えられた実引数に \ を適用したリファレンスとなります。

You can use the \[] backslash group notation to specify more than one allowed argument type. For example:

\[] バックスラッシュ記法を、一つまたは複数の引数型を指定するために 使えます。 例えば:

    sub myref (\[$@%&*])

will allow calling myref() as

というのは以下のように myref() を呼び出すのを許し:

    myref $var
    myref @array
    myref %hash
    myref &sub
    myref *glob

and the first argument of myref() will be a reference to a scalar, an array, a hash, a code, or a glob.

myref() の最初の引数は スカラ、配列、ハッシュ、コード、グロブのいずれかへの リファレンスです。

Unbackslashed prototype characters have special meanings. Any unbackslashed @ or % eats all remaining arguments, and forces list context. An argument represented by $ forces scalar context. An & requires an anonymous subroutine, which, if passed as the first argument, does not require the sub keyword or a subsequent comma.

バックスラッシュが付けられていない文字は特別な意味を持っています。 バックスラッシュを付けられていない すべての @% は残りの引数全てを 取ってしまい、さらにリストコンテキストを強制します。 $ で表される引数はスカラコンテキストを強制されます。 第一引数として渡された場合、&sub キーワードや連続したカンマを 要求しないような無名サブルーチンを要求します。

A * allows the subroutine to accept a bareword, constant, scalar expression, typeglob, or a reference to a typeglob in that slot. The value will be available to the subroutine either as a simple scalar, or (in the latter two cases) as a reference to the typeglob. If you wish to always convert such arguments to a typeglob reference, use Symbol::qualify_to_ref() as follows:

* は、スロットにある裸の単語、定数、スカラ式、型グロブ、 型グロブに対するリファレンスを許しています。 その値はサブルーチンにとって単純なスカラとすることも 型グロブに対するリファレンスにもなります。 そのような引数を常に型グロブリファレンスに変換したい場合は、 Symbol::qualify_to_ref() を以下のように使います:

    use Symbol 'qualify_to_ref';

    sub foo (*) {
        my $fh = qualify_to_ref(shift, caller);
        ...
    }

The + prototype is a special alternative to $ that will act like \[@%] when given a literal array or hash variable, but will otherwise force scalar context on the argument. This is useful for functions which should accept either a literal array or an array reference as the argument:

+ プロトタイプは $ の特殊な代替物で、リテラルな配列やハッシュ変数が 与えられると \[@%] のように動作しますが、さもなければ引数に スカラコンテキストを強制します。 これは引数としてリテラルの配列または配列リファレンスのどちらかを 受け付けるような関数に有用です:

    sub mypush (+@) {
        my $aref = shift;
        die "Not an array or arrayref" unless ref $aref eq 'ARRAY';
        push @$aref, @_;
    }

When using the + prototype, your function must check that the argument is of an acceptable type.

関数で + プロトタイプを使ったとき、引数が受け付けられる型かを チェックしなければなりません。

A semicolon (;) separates mandatory arguments from optional arguments. It is redundant before @ or %, which gobble up everything else.

セミコロン (;) は、必須の引数と省略可能な引数とを分割します。 @% の前ではこれは冗長です。 その他のものを吸収します。

As the last character of a prototype, or just before a semicolon, you can use _ in place of $: if this argument is not provided, $_ will be used instead.

プロトタイプの最後の文字、あるいはセミコロンの直前に、$ の代わりに _ を使えます: 引数が与えられない場合、$_ が代わりに使われます。

Note how the last three examples in the table above are treated specially by the parser. mygrep() is parsed as a true list operator, myrand() is parsed as a true unary operator with unary precedence the same as rand(), and mytime() is truly without arguments, just like time(). That is, if you say

上の例の最後の三つのものは、構文解析器が特別扱いするということに 注意してください。 mygrep() は本当のリスト演算子として解析され、myrand()rand() と 同じく単項演算子の優先順位を持った真の単項演算子として、 そして mytime()time() と同じ様な引数を取らないものとして 解析されます。つまり、

    mytime +2;

you'll get mytime() + 2, not mytime(2), which is how it would be parsed without a prototype.

とすると、プロトタイプなしの場合にパースされる mytime(2) ではなく、 mytime() + 2 となります。

The interesting thing about & is that you can generate new syntax with it, provided it's in the initial position:

& に関して興味深いことは、初期位置を使って新しい構文を 生成できるということです:

    sub try (&@) {
        my($try,$catch) = @_;
        eval { &$try };
        if ($@) {
            local $_ = $@;
            &$catch;
        }
    }
    sub catch (&) { $_[0] }

    try {
        die "phooey";
    } catch {
        /phooey/ and print "unphooey\n";
    };

That prints "unphooey". (Yes, there are still unresolved issues having to do with visibility of @_. I'm ignoring that question for the moment. (But note that if we make @_ lexically scoped, those anonymous subroutines can act like closures... (Gee, is this sounding a little Lispish? (Never mind.))))

これは “unphooey” を出力します。(そう、@_ の可視性に関して 解決していないことがあります。 現時点ではこれを無視します(ただし、@_ をレキシカルスコープにすれば 上記のサブルーチンはクロージャーのように振る舞うことができます... (んー、これってちょーっと Lisp っぽいかな? (気にしないでね))))

And here's a reimplementation of the Perl grep operator:

以下の例は Perl の grep 演算子のの再実装です:

    sub mygrep (&@) {
        my $code = shift;
        my @result;
        foreach $_ (@_) {
            push(@result, $_) if &$code;
        }
        @result;
    }

Some folks would prefer full alphanumeric prototypes. Alphanumerics have been intentionally left out of prototypes for the express purpose of someday in the future adding named, formal parameters. The current mechanism's main goal is to let module writers provide better diagnostics for module users. Larry feels the notation quite understandable to Perl programmers, and that it will not intrude greatly upon the meat of the module, nor make it harder to read. The line noise is visually encapsulated into a small pill that's easy to swallow.

一部の人は、完全に英数字を使ったプロトタイプを好むかもしれません。 英数字は、将来名前付き仮引数を追加するときのために意図的に使わずに 残してあるのです。 現時点でのプロトタイプの機構の主な目的はモジュール作者がそのユーザーに 対してより良い診断メッセージを提供させるようにするためのものなのです。 この記法は Perl プログラマが非常に理解しやすく、モジュールの中身に 進入するようなことも読みづらくしたりすることもないと Larry は考えています。 回線の雑音は飲み込みやすい小さな丸薬に視覚的に押し込められるのです。

If you try to use an alphanumeric sequence in a prototype you will generate an optional warning - "Illegal character in prototype...". Unfortunately earlier versions of Perl allowed the prototype to be used as long as its prefix was a valid prototype. The warning may be upgraded to a fatal error in a future version of Perl once the majority of offending code is fixed.

もしプロトタイプに英数字を使おうとすると、オプションの警告 "Illegal character in prototype..." が生成されます。 残念ながら、過去のバージョンの Perl では、有効なプロトタイプが前置されてさえ いれば、英数字を含むプロトタイプも認められていました。 この警告は、目障りなコードの大部分が修正されたなら、将来のバージョンの Perl で致命的エラーに格上げされるかもしれません。

It's probably best to prototype new functions, not retrofit prototyping into older ones. That's because you must be especially careful about silent impositions of differing list versus scalar contexts. For example, if you decide that a function should take just one parameter, like this:

新しい関数にプロトタイプを付けるのがおそらく最善であり、古い関数に対して プロトタイプを付けることはよくありません。 なぜなら、(プロトタイプを付けたことによって)リストコンテキストと スカラコンテキストを黙って変えてしまうようなことに関して 特に注意しなければならないからです。 たとえば以下の例のようなただ一つの引数を取ると決めた関数を考えてみましょう:

    sub func ($) {
        my $n = shift;
        print "you gave me $n\n";
    }

and someone has been calling it with an array or expression returning a list:

そして、誰かがこの関数をリストを返すような配列や式を引数として 渡したとすると:

    func(@foo);
    func( split /:/ );

Then you've just supplied an automatic scalar in front of their argument, which can be more than a bit surprising. The old @foo which used to hold one thing doesn't get passed in. Instead, func() now gets passed in a 1; that is, the number of elements in @foo. And the split gets called in scalar context so it starts scribbling on your @_ parameter list. Ouch!

自動的に scalar が引数の前に(こっそりと)付けられます。 これにはいささかびっくりすることになるかもしれません。 これまでそうであったような、要素を持った @foo が渡されることはありません。 その代わり、func()1、つまり @foo の要素の数を得るようになります。 そして split はスカラコンテキストで呼び出され、パラメータリスト @_ に落書きを始めてしまいます。 あいたた。

This is all very powerful, of course, and should be used only in moderation to make the world a better place.

もちろんこれは十分強力なものであり、世界をより良い場所にするという目的に 限って使用すべきものでしょう。

定数関数

Functions with a prototype of () are potential candidates for inlining. If the result after optimization and constant folding is either a constant or a lexically-scoped scalar which has no other references, then it will be used in place of function calls made without &. Calls made using & are never inlined. (See constant.pm for an easy way to declare most constants.)

() というプロトタイプを持った関数はインライン展開される可能性を 持っています。 最適化と定数畳み込み後の結果が一つの定数か、何のリファレンスも持たない レキシカルスコープのスカラであったならば、その関数が & を使わずに 呼びされたときに呼び出しのその場に置かれます。 & を使って呼び出された関数は決してインライン展開されることは ありません(ほとんどの定数を宣言するための簡単な方法は constant.pm を参照してください)。

The following functions would all be inlined:

以下に挙げた関数は全てインライン展開されるでしょう:

    sub pi ()           { 3.14159 }             # Not exact, but close.
    sub PI ()           { 4 * atan2 1, 1 }      # As good as it gets,
                                                # and it's inlined, too!
    sub ST_DEV ()       { 0 }
    sub ST_INO ()       { 1 }
    sub pi ()           { 3.14159 }             # 正確ではないが近い
    sub PI ()           { 4 * atan2 1, 1 }      # 可能な限り良いもの
                                                # そしてこれも展開されます!
    sub ST_DEV ()       { 0 }
    sub ST_INO ()       { 1 }

    sub FLAG_FOO ()     { 1 << 8 }
    sub FLAG_BAR ()     { 1 << 9 }
    sub FLAG_MASK ()    { FLAG_FOO | FLAG_BAR }

    sub OPT_BAZ ()      { not (0x1B58 & FLAG_MASK) }

    sub N () { int(OPT_BAZ) / 3 }

    sub FOO_SET () { 1 if FLAG_MASK & FLAG_FOO }

Be aware that these will not be inlined; as they contain inner scopes, the constant folding doesn't reduce them to a single constant:

以下のものはインライン展開されないことに注意してください; より内側にスコープがあるので、畳み込まれた定数が一つの定数に削減できません:

    sub foo_set () { if (FLAG_MASK & FLAG_FOO) { 1 } }

    sub baz_val () {
        if (OPT_BAZ) {
            return 23;
        }
        else {
            return 42;
        }
    }

If you redefine a subroutine that was eligible for inlining, you'll get a mandatory warning. (You can use this warning to tell whether or not a particular subroutine is considered constant.) The warning is considered severe enough not to be optional because previously compiled invocations of the function will still be using the old value of the function. If you need to be able to redefine the subroutine, you need to ensure that it isn't inlined, either by dropping the () prototype (which changes calling semantics, so beware) or by thwarting the inlining mechanism in some other way, such as

インライン展開するのに適切であったサブルーチンを再定義すると強制的な 警告を受けることになります。 (この警告を、あるサブルーチンが定数サブルーチンとして認識されるかどうかを 区別するために使うことができます。) 以前にコンパイルした関数の起動によってこの関数の古い値を使い続けるので、 この警告はオプションにするには重大すぎると考えられました。 そのサブルーチンを再定義できる必要があるのならば、() プロトタイプを やめる(これは呼び出しのセマンティクスを変えてしまうので注意してください)か、 以下の例のようにしてインライン展開機構を 働かせないようにしてサブルーチンがインライン展開されていないことを 保証する必要があります。

    sub not_inlined () {
        23 if $];
    }

組み込み関数のオーバーライド

Many built-in functions may be overridden, though this should be tried only occasionally and for good reason. Typically this might be done by a package attempting to emulate missing built-in functionality on a non-Unix system.

多くの組み込み関数はオーバーライドすることができます。 ただし、これを行うのは、そうする理由と状況とがあるときのみに 限るべきでしょう。 これが行われる典型的な例は、非 UNIX システムにおいて欠けている組み込み機能を エミュレートするためにパッケージを使おうとする場合でしょう。

Overriding may be done only by importing the name from a module at compile time--ordinary predeclaration isn't good enough. However, the use subs pragma lets you, in effect, predeclare subs via the import syntax, and these names may then override built-in ones:

オーバーライドはコンパイル時にモジュールからのみ名前を import できます-- 通常の先行宣言では十分ではありません。 しかしながら、use subs プラグマは import 構文を通して先行宣言を行い、 さらにそれらの名前が組み込みのものをオーバーライドできるようにします:

    use subs 'chdir', 'chroot', 'chmod', 'chown';
    chdir $somewhere;
    sub chdir { ... }

To unambiguously refer to the built-in form, precede the built-in name with the special package qualifier CORE::. For example, saying CORE::open() always refers to the built-in open(), even if the current package has imported some other subroutine called &open() from elsewhere. Even though it looks like a regular function call, it isn't: you can't take a reference to it, such as the incorrect \&CORE::open might appear to produce.

曖昧さなく組み込みのものを参照するために、特別なパッケージ修飾子 CORE:: を組み込みの名前に前置することができます。 たとえば CORE::open() とすると、たとえカレントパッケージが &open() といった別のサブルーチンを import していたとしても これは常に組み込み関数の open() を参照します。 これは通常の関数呼び出しのように見えますが、そうではありません: \&CORE::open のようにしてリファレンスを得ることは出来ません。

Library modules should not in general export built-in names like open or chdir as part of their default @EXPORT list, because these may sneak into someone else's namespace and change the semantics unexpectedly. Instead, if the module adds that name to @EXPORT_OK, then it's possible for a user to import the name explicitly, but not implicitly. That is, they could say

ライブラリモジュールは、open だとか chdir のような組み込みの名前を デフォルトの @EXPORT リストの一部としてexport すべきではありません。 なぜなら、ライブラリを使った人の名前空間を汚し、予期しない動作を 呼び起こす可能性があるからです。 @EXPORT_OK に名前を追加すれば、ユーザーがその名前を陽に指定すれば import することができ、暗黙の内に import されてしまうことはありません。 つまり、

    use Module 'open';

and it would import the open override. But if they said

とすると、open() の import を行い、さらにそれのオーバーライドを 行いますが、

    use Module;

they would get the default imports without overrides.

とした場合にはデフォルトの import を行い、オーバーライドはしません。

The foregoing mechanism for overriding built-in is restricted, quite deliberately, to the package that requests the import. There is a second method that is sometimes applicable when you wish to override a built-in everywhere, without regard to namespace boundaries. This is achieved by importing a sub into the special namespace CORE::GLOBAL::. Here is an example that quite brazenly replaces the glob operator with something that understands regular expressions.

組み込みのものに対するオーバーライディングのための機構は インポートを要求したパッケージに制限されています。 名前空間に関係なく組み込みの任意のものをオーバーライドしたいと 考えたときに使えることもある二番目の方法があります。 それは特殊な名前空間 CORE::GLOBAL に対して sub を インポートすることによるものです。 以下にちょっとした正規表現を使って glob 演算子を置き換える例を挙げます。

    package REGlob;
    require Exporter;
    @ISA = 'Exporter';
    @EXPORT_OK = 'glob';

    sub import {
        my $pkg = shift;
        return unless @_;
        my $sym = shift;
        my $where = ($sym =~ s/^GLOBAL_// ? 'CORE::GLOBAL' : caller(0));
        $pkg->export($where, $sym, @_);
    }

    sub glob {
        my $pat = shift;
        my @got;
        if (opendir my $d, '.') { 
            @got = grep /$pat/, readdir $d; 
            closedir $d;   
        }
        return @got;
    }
    1;

And here's how it could be (ab)used:

そして以下は悪い使い方(かもしれない)例です:

    #use REGlob 'GLOBAL_glob';      # override glob() in ALL namespaces
    package Foo;
    use REGlob 'glob';              # override glob() in Foo:: only
    print for <^[a-z_]+\.pm\$>;     # show all pragmatic modules

The initial comment shows a contrived, even dangerous example. By overriding glob globally, you would be forcing the new (and subversive) behavior for the glob operator for every namespace, without the complete cognizance or cooperation of the modules that own those namespaces. Naturally, this should be done with extreme caution--if it must be done at all.

最初のコメント部分は不自然で、危険ですらある例です。 グローバルに glob をオーバーライドすることによって、 完全に認識されていなかったりモジュール固有の名前空間との協調を 考慮せずに、glob の動作が 全ての 名前空間で強制的に新しい(そして破壊的な)ものになります。 こういったことは(それが本当にやらなければいけないこととした上で) 大いに注意したうえで行うべきものです。

The REGlob example above does not implement all the support needed to cleanly override perl's glob operator. The built-in glob has different behaviors depending on whether it appears in a scalar or list context, but our REGlob doesn't. Indeed, many perl built-in have such context sensitive behaviors, and these must be adequately supported by a properly written override. For a fully functional example of overriding glob, study the implementation of File::DosGlob in the standard library.

前述の REGlob の例は、perl の glob 演算子をオーバーライドするのに 必要な全てのことを実装してはいません。 組み込みの glob はそれがスカラコンテキストで使われたのか リストコンテキストで使われたのかによって異なる動作をしますが、 先程の例の REGlob ではそうなっていません。 perl の組み込みのものの多くがこのようにコンテキストによって違う動作をし、 オーバーライドするものを記述する場合にはきちんとそれを サポートしていなければなりません。 glob のオーバーライディングの完全版は、 標準ライブラリにある File::DosGlob の実装で勉強してください。

When you override a built-in, your replacement should be consistent (if possible) with the built-in native syntax. You can achieve this by using a suitable prototype. To get the prototype of an overridable built-in, use the prototype function with an argument of "CORE::builtin_name" (see "prototype" in perlfunc).

組み込み関数をオーバーライドするとき、オーバーライドした関数は 組み込み関数の元の文法(もしあれば)と一貫しているべきです。 これは適切なプロトタイプを使うことで達成できます。 オーバーライドできる組み込み関数のプロトタイプを得るためには、 "CORE::builtin_name" を引き数として prototype 関数を使ってください ("prototype" in perlfunc を参照してください)。

Note however that some built-ins can't have their syntax expressed by a prototype (such as system or chomp). If you override them you won't be able to fully mimic their original syntax.

(systemchomp のように)文法をプロトタイプで表現できない 組み込み関数に注意してください。 もしこれらをオーバーライドすると、もとの文法を完全に真似ることは できません。

The built-ins do, require and glob can also be overridden, but due to special magic, their original syntax is preserved, and you don't have to define a prototype for their replacements. (You can't override the do BLOCK syntax, though).

組み込みの do, require, glob もオーバーライドできますが、特別な 魔法によって、これらの元の文法は保存され、オーバーライドしたもののために プロトタイプを定義する必要はありません (しかし、do BLOCK 文法はオーバーライドできません)。

require has special additional dark magic: if you invoke your require replacement as require Foo::Bar, it will actually receive the argument "Foo/Bar.pm" in @_. See "require" in perlfunc.

require は特別な追加の黒魔法を持ちます: require をオーバーライドした ものを require Foo::Bar として起動すると、実際には @_ に引き数として "Foo/Bar.pm" を受け取ります。 "require" in perlfunc を参照してください。

And, as you'll have noticed from the previous example, if you override glob, the <*> glob operator is overridden as well.

そして、以前の例から気付いたかもしれませんが、もし glob を オーバーライドすると、グロブ演算子 <*> もオーバーライドされます。

In a similar fashion, overriding the readline function also overrides the equivalent I/O operator <FILEHANDLE>. Also, overriding readpipe also overrides the operators `` and qx//.

同様に、readline 関数をオーバーライドすると 等価な I/O 演算子である <FILEHANDLE> もオーバーライドします。 また、readpipe をオーバーライドすると、演算子 ``qx// も オーバーライドします。

Finally, some built-ins (e.g. exists or grep) can't be overridden.

最後に、いくつかの組み込み関数(existsgrep) は オーバーライドできません。

オートロード

If you call a subroutine that is undefined, you would ordinarily get an immediate, fatal error complaining that the subroutine doesn't exist. (Likewise for subroutines being used as methods, when the method doesn't exist in any base class of the class's package.) However, if an AUTOLOAD subroutine is defined in the package or packages used to locate the original subroutine, then that AUTOLOAD subroutine is called with the arguments that would have been passed to the original subroutine. The fully qualified name of the original subroutine magically appears in the global $AUTOLOAD variable of the same package as the AUTOLOAD routine. The name is not passed as an ordinary argument because, er, well, just because, that's why. (As an exception, a method call to a nonexistent import or unimport method is just skipped instead. Also, if the AUTOLOAD subroutine is an XSUB, $AUTOLOAD is not populated; instead, you should call SvPVX/SvCUR on the CV for AUTOLOAD to retrieve the method name.)

定義されていないサブルーチンを呼び出した場合、通常はそんなサブルーチンは ないといった内容のエラーが即座に発生するでしょう(メソッドとして 使われているサブルーチンも同様に、メソッドがそのクラスのパッケージの すべての基底クラス中に存在していなかったとき)。 しかし、AUTOLOAD サブルーチンがそのパッケージの中で定義されているか 元のサブルーチンで使われるパッケージの中で定義されていれば、 元のサブルーチンに対して渡されたであろう引数を伴ってその AUTOLOAD サブルーチンが呼び出されます。 元のサブルーチンの完全修飾された名前は AUTOLOAD ルーチンと同じ パッケージのグローバルな変数 $AUTOLOAD の中に現れます。 この名前は通常の引数として渡されることはありません。 なぜなら、その、あー、なんというかこれが理由です。 (例外として、存在しない importunimport メソッドへの呼び出しは 単に無視されます。 また、AUTOLOAD サブルーチンが XSUB なら、$AUTOLOAD は呼び出されません; 代わりに、メソッド名を取得するために AUTOLOADCVSvPVX/SvCUR を呼び出すべきです。)

Many AUTOLOAD routines load in a definition for the requested subroutine using eval(), then execute that subroutine using a special form of goto() that erases the stack frame of the AUTOLOAD routine without a trace. (See the source to the standard module documented in AutoLoader, for example.) But an AUTOLOAD routine can also just emulate the routine and never define it. For example, let's pretend that a function that wasn't defined should just invoke system with those arguments. All you'd do is:

多くの AUTOLOAD ルーチンは eval() を使った要求サブルーチンに 対する定義でロードされ、AUTOLOAD ルーチンのスタックフレームを トレースなしに消してしまうような特別な形式の goto() を使うサブルーチンを 実行します(実例は標準の AutoLoader モジュールのソースを参照してください)。 しかし、AUTOLOAD ルーチンはそのようなルーチンの模倣をすることもできて、 かつそれを定義しません。 たとえば、定義されてない関数があったときに、それを引数として system() を起動するようにさせます。 あなたがやろうとしていることはこういうことです:

    sub AUTOLOAD {
        my $program = $AUTOLOAD;
        $program =~ s/.*:://;
        system($program, @_);
    }
    date();
    who('am', 'i');
    ls('-l');

In fact, if you predeclare functions you want to call that way, you don't even need parentheses:

このやり方で呼び出したい関数を先行宣言しておけば、括弧すら 必要ではなくなります。

    use subs qw(date who ls);
    date;
    who "am", "i";
    ls '-l';

A more complete example of this is the standard Shell module, which can treat undefined subroutine calls as calls to external programs.

この例のもっと完全なものが、標準の Shell モジュールです。 これは未定義のサブルーチンの呼び出しを外部プログラムの呼び出しとして扱います。

Mechanisms are available to help modules writers split their modules into autoloadable files. See the standard AutoLoader module described in AutoLoader and in AutoSplit, the standard SelfLoader modules in SelfLoader, and the document on adding C functions to Perl code in perlxs.

この仕掛けは、モジュール作者がモジュールを自動ロード可能なファイルに 分割するのを助けるために使用可能です。 AutoLoaderAutoSplit で説明されている 標準の AutoLoader モジュールと、SelfLoader で説明されている 標準の SelfLoader モジュール、そして perlxs にある Perl プログラムに C の関数を追加することに関するドキュメントを参照してください。

サブルーチン属性

A subroutine declaration or definition may have a list of attributes associated with it. If such an attribute list is present, it is broken up at space or colon boundaries and treated as though a use attributes had been seen. See attributes for details about what attributes are currently supported. Unlike the limitation with the obsolescent use attrs, the sub : ATTRLIST syntax works to associate the attributes with a pre-declaration, and not just with a subroutine definition.

サブルーチン宣言や定義には、それと結び付けられた属性のリストを 持たせることが出来ます。 このような属性が合った場合、スペースかコロンを区切りとして分割され、 use attributes があったかのように扱われます。 属性が現在対応している詳細については attributes を参照してください。 古い use attrs の制限と違って、sub : ATTRLIST の文法は 前方宣言でも動作し、サブルーチン定義だけではありません。

The attributes must be valid as simple identifier names (without any punctuation other than the '_' character). They may have a parameter list appended, which is only checked for whether its parentheses ('(',')') nest properly.

属性は単純な識別子名('_' 以外の句読点なし)でなければなりません。 パラメータリストを追加するときに、括弧('(',')')のネストが 正しいかどうかだけをチェックします。

Examples of valid syntax (even though the attributes are unknown):

(属性が不明だとしても)正常な文法の例です:

    sub fnord (&\%) : switch(10,foo(7,3))  :  expensive;
    sub plugh () : Ugly('\(") :Bad;
    sub xyzzy : _5x5 { ... }

Examples of invalid syntax:

不正な文法の例です:

    sub fnord : switch(10,foo(); # ()-string not balanced
    sub snoid : Ugly('(');        # ()-string not balanced
    sub xyzzy : 5x5;              # "5x5" not a valid identifier
    sub plugh : Y2::north;        # "Y2::north" not a simple identifier
    sub snurt : foo + bar;        # "+" not a colon or space

The attribute list is passed as a list of constant strings to the code which associates them with the subroutine. In particular, the second example of valid syntax above currently looks like this in terms of how it's parsed and invoked:

属性リストはサブルーチンと結び付けられたコードに定数文字列の リストとして渡されます。 特に、上記の有効な文法の第二の例では、どのようにパースと起動が 行われるかという点においては以下のようになります:

    use attributes __PACKAGE__, \&plugh, q[Ugly('\(")], 'Bad';

For further details on attribute lists and their manipulation, see attributes and Attribute::Handlers.

属性リストとその操作に関するさらなる詳細については、 attributes を参照してください。

SEE ALSO

See "Function Templates" in perlref for more about references and closures. See perlxs if you'd like to learn about calling C subroutines from Perl. See perlembed if you'd like to learn about calling Perl subroutines from C. See perlmod to learn about bundling up your functions in separate files. See perlmodlib to learn what library modules come standard on your system. See perltoot to learn how to make object method calls.

リファレンスとクロージャーについては "Function Templates" in perlref を 参照してください。 Perl から C のサブルーチンを呼び出すことに関して知りたければ、 perlxsを参照してください。 Perl のサブルーチンを C から呼び出したい場合は perlembed を参照してください。 自分の関数を別のファイルで構築することに関しては perlmod を参照してください。 どのライブラリモジュールがあなたのシステムで標準となっているかを 学ぶためには perlmodlib を参照してください。 オブジェクトメソッド呼び出しの作り方を学ぶには perltoot を参照してください。