AutoLoader-5.59 > AutoLoader
AutoLoader-5.59
Other versions:
AutoLoader-5.68

名前

AutoLoader - 要求に応じてのみ、サブルーチンを読み込む。

概要

    package Foo;
    use AutoLoader 'AUTOLOAD';   # デフォルトのAUTOLOADサブルーチンを読み込む。

    package Bar;
    use AutoLoader;              # AUTOLOADをインポートしないで、自分自身のものを定義する。
    sub AUTOLOAD {
        ...
        $AutoLoader::AUTOLOAD = "...";
        goto &AutoLoader::AUTOLOAD;
    }

説明

The AutoLoader module works with the AutoSplit module and the __END__ token to defer the loading of some subroutines until they are used rather than loading them all at once.

AutoLoaderモジュールは、AutoSplitモジュールと__END__トークン と一緒に働き、サブルーチンすべてを一度に読み込むのではなくて、 使われるまで、サブルーチンの読み込みを延期します。

To use AutoLoader, the author of a module has to place the definitions of subroutines to be autoloaded after an __END__ token. (See perldata.) The AutoSplit module can then be run manually to extract the definitions into individual files auto/funcname.al.

AutoLoaderを使うために、モジュールの作者は、 自動的に読み込むブルーチンの定義を__END__トークン(perldataを見てください)の後に置く必要があります。 AutoSplitモジュールを走らせると、手動で定義を、個々のauto/funcname.alファイル抜きだします。

AutoLoader implements an AUTOLOAD subroutine. When an undefined subroutine in is called in a client module of AutoLoader, AutoLoader's AUTOLOAD subroutine attempts to locate the subroutine in a file with a name related to the location of the file from which the client module was read. As an example, if POSIX.pm is located in /usr/local/lib/perl5/POSIX.pm, AutoLoader will look for perl subroutines POSIX in /usr/local/lib/perl5/auto/POSIX/*.al, where the .al file has the same name as the subroutine, sans package. If such a file exists, AUTOLOAD will read and evaluate it, thus (presumably) defining the needed subroutine. AUTOLOAD will then goto the newly defined subroutine.

AutoLoaderは、AUTOLOADサブルーチンを実装します。 未定義のサブルーチンが、AutoLoaderのクライアントモジュールで、呼び出されると、 AutoLoaderのAUTOLOADサブルーチンは、 クライアントモジュールが読まれたそのファイルの位置に関係する名前で、 ファイル中のサブルーチンを見つけようとします。 例えば、POSIX.pmなら、/usr/local/lib/perl5/POSIX.pmに見つかります。 AutoLoaderは、perlのPOSIXのサブルーチンを/usr/local/lib/perl5/auto/POSIX/*.alに見つけます。 .alファイルは、POSIXのサブルーチンと同じ名前です。パッケージ名はついていません。 そのようなファイルがあれば、AUTOLOAD は、それを読んで評価します。 このようにして、(たぶん、)必要とされるサブルーチンを定義します。 それから、AUTOLOADは新たに定義されたサブルーチンへgotoします。

Once this process completes for a given function, it is defined, so future calls to the subroutine will bypass the AUTOLOAD mechanism.

与えられた関数のために、一度このプロセスが実行され関数が定義されると、 (訳註:次回以降の)未来のサブルーチンの呼び出しは、AUTOLOADのメカニズムを無視します。

Subroutine Stubs

In order for object method lookup and/or prototype checking to operate correctly even when methods have not yet been defined it is necessary to "forward declare" each subroutine (as in sub NAME;). See "SYNOPSIS" in perlsub. Such forward declaration creates "subroutine stubs", which are place holders with no code.

メソッドがまだ定義されていないときにでも、 オブジェクトメソッドの調査および/または、プロトタイプチェックを、 正確に行うために、それぞれのサブルーチンを(sub NAME;のように)、 "前宣言"する必要があります。"SYNOPSIS" in pelsubを見てください。 このような前宣言は、"subroutine stubs"を作ります。 これは、コード無しのプレースホルダです。

The AutoSplit and AutoLoader modules automate the creation of forward declarations. The AutoSplit module creates an 'index' file containing forward declarations of all the AutoSplit subroutines. When the AutoLoader module is 'use'd it loads these declarations into its callers package.

AutoSplit と、AutoLoaderモジュールは前宣言の作成を自動化します。 AutoSplit モジュールは、'index'ファイルを作り、そこに、 AutoSplitが分割した、すべてのサブルーチンの前宣言を置きます。 AutoLoaderモジュールが、'use'されると、その呼び出しパッケージに、 それらの定義を読み込みます。

Because of this mechanism it is important that AutoLoader is always used and not required.

このメカニズムのために、AutoLoaderが、つねに、useされ、 requireされないということが重要です。

AutoLoader の AUTOLOAD サブルーチンを使う

In order to use AutoLoader's AUTOLOAD subroutine you must explicitly import it:

AutoLoaderのAUTOLOADサブルーチンために、 明示的にそれをインポートしなければなりません

    use AutoLoader 'AUTOLOAD';

AutoLoader's AUTOLOAD サブルーチンをオーバーロードする

Some modules, mainly extensions, provide their own AUTOLOAD subroutines. They typically need to check for some special cases (such as constants) and then fallback to AutoLoader's AUTOLOAD for the rest.

いくつかのモジュールには、主な拡張として、自分自身のAUTOLOADサブルーチンをもっています。 それらは、典型的に、いくつかの特別なケース(定数のような)のために、チェックする必要があり、 そして、それから、残りのために、AutoLoaderのAUTOLOADにフォールバックする必要があります。

Such modules should not import AutoLoader's AUTOLOAD subroutine. Instead, they should define their own AUTOLOAD subroutines along these lines:

このようなモジュールはAutoLoaderのAUTOLOADサブルーチンをインポートすべきではありません。 そのかわりに、そういうモジュールは、自分自身のAUTOLOADサブルーチンを定義すべきです。 次のようにします:

    use AutoLoader;
    use Carp;

    sub AUTOLOAD {
        my $sub = $AUTOLOAD;
        (my $constname = $sub) =~ s/.*:://;
        my $val = constant($constname, @_ ? $_[0] : 0);
        if ($! != 0) {
            if ($! =~ /Invalid/ || $!{EINVAL}) {
                $AutoLoader::AUTOLOAD = $sub;
                goto &AutoLoader::AUTOLOAD;
            }
            else {
                croak "Your vendor has not defined constant $constname";
            }
        }
        *$sub = sub { $val }; # same as: eval "sub $sub { $val }";
        goto &$sub;
    }

If any module's own AUTOLOAD subroutine has no need to fallback to the AutoLoader's AUTOLOAD subroutine (because it doesn't have any AutoSplit subroutines), then that module should not use AutoLoader at all.

(AutoSplitするサブルーチンを全くもっていないために)、モジュールのAUTOLOADサブルーチンが、 AutoLoaderのAUTOLOADサブルーチンにフォールバックする必要がなければ、 そのモジュールはAutoLoaderを、全く使わないべきです。

Package Lexicals

Package lexicals declared with my in the main block of a package using AutoLoader will not be visible to auto-loaded subroutines, due to the fact that the given scope ends at the __END__ marker. A module using such variables as package globals will not work properly under the AutoLoader.

AutoLoaderを使っているパッケージの main ブロックで、 myで宣言されたパッケージレキシカルの変数は、自動的に読み込まれたサブルーチンには見えません。 これは、与えられたスコープが__END__マーカーで終るという事実によります。 そのような変数をパッケージのグローバル変数のように使っているモジュールは、 AutoLoaderのもとでは、うまく動きません。

The vars pragma (see "vars" in perlmod) may be used in such situations as an alternative to explicitly qualifying all globals with the package namespace. Variables pre-declared with this pragma will be visible to any autoloaded routines (but will not be invisible outside the package, unfortunately).

varsプラグマ("vars" in perlmodを見てください)が、こういった状況で、 パッケージの名前空間で明示的に完全修飾したグローバル変数に変わるものとして、使われるでしょう。 このプラグマで先に宣言された変数は、どんな、自動的に読み込まれるルーチンにも見えます。 (ですが、不運なことに、パッケージの外側からは見ることが出来ません。)

AutoLoaderを使わない

You can stop using AutoLoader by simply

AutoLoaderを使わなくするには、単純に次のようにします。

        no AutoLoader;

AutoLoader vs. SelfLoader

The AutoLoader is similar in purpose to SelfLoader: both delay the loading of subroutines.

AutoLoaderは、SelfLoaderの目的と類似性があります: 両方ともサブルーチンの読み込みを遅らせるものです。

SelfLoader uses the __DATA__ marker rather than __END__. While this avoids the use of a hierarchy of disk files and the associated open/close for each routine loaded, SelfLoader suffers a startup speed disadvantage in the one-time parsing of the lines after __DATA__, after which routines are cached. SelfLoader can also handle multiple packages in a file.

SelfLoader は、__END__ではなくて、__DATA__ マーカーを使います。 このことにより、それぞれのルーチンを読み込むために、 ディスクファイルの階層と関連する open/close を使うのを避ける一方で、 SelfLoaderは、ルーチンがキャッシュされている__DATA__を一行一行、 一時的に解析するので、スタートアップのスピードのディスアドバンテージに耐えなければなりません。 SelfLoaderは、一つのファイルに複数のパッケージを取り扱えます。

AutoLoader only reads code as it is requested, and in many cases should be faster, but requires a mechanism like AutoSplit be used to create the individual files. ExtUtils::MakeMaker will invoke AutoSplit automatically if AutoLoader is used in a module source file.

AutoLoaderは、それがリクエストされた時に、コードを読むだけです。 多くのケースで、これは、速いです。ですが、独立したファイルを作成するのに、 使われるAutoSplitのようなメカニズムを必要とします。 AutoLoaderが、モジュールのソースファイルで使われると、 ExtUtils::MakeMakerは、AutoSplitを自動的に呼び出します。

警告

AutoLoaders prior to Perl 5.002 had a slightly different interface. Any old modules which use AutoLoader should be changed to the new calling style. Typically this just means changing a require to a use, adding the explicit 'AUTOLOAD' import if needed, and removing AutoLoader from @ISA.

Perl 5.002 より前のAutoLoaderは、ちょっと違ったインターフェースでした。 AutoLoaderを使っている古いモジュールは、どんなものでも、 新しいスタイルの呼び出しに変更するべきです。一般に、このことは、次のことを意味します。 require を use に変えること、必要なら明示的な'AUTOLOAD'のインポートを加えること、 @ISAから、AutoLoaderを削除することです。

On systems with restrictions on file name length, the file corresponding to a subroutine may have a shorter name that the routine itself. This can lead to conflicting file names. The AutoSplit package warns of these potential conflicts when used to split a module.

システムに、ファイル名の長さに関する制限があると、 ファイルとサブルーチンの一致のために、ルーチン自身がより短い名前になるかもしれません。 このことは、ファイル名の衝突を引き起こすかも知れません。 AutoSplitパッケージは、モジュールを分割するときに、 こういった潜在的な衝突を警告します。

AutoLoader may fail to find the autosplit files (or even find the wrong ones) in cases where @INC contains relative paths, and the program does chdir.

@INCの中に相対パスがあるときに、プログラムがchdirした場合、 AutoLoader は、自動的に分割されたファイルを見付けるのに失敗するかも知れません (または、間違ったファイルを見つけるかも知れません)。

SEE ALSO

SelfLoader - 外部ファイルを使わないオートローダー