- use Module VERSION LIST
- use Module VERSION
- use Module LIST
- use Module
- use VERSION
-
Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package. It is exactly equivalent to
指定したモジュールから、現在のパッケージにさまざまな内容をインポートします; 多くは、パッケージのサブルーチン名や、変数名に別名を付けることで、 実現されています。 これは、以下は等価ですが:
BEGIN { require Module; Module->import( LIST ); }
except that Module must be a bareword. The importation can be made conditional by using the if module.
Module が 裸の単語でなければならない ことを除けば、です。 インポートは、if を使って条件付きで行うことができます。
In the
use VERSION
form, VERSION may be either a v-string such as v5.24.1, which will be compared to$^V
(aka $PERL_VERSION), or a numeric argument of the form 5.024001, which will be compared to$]
. An exception is raised if VERSION is greater than the version of the current Perl interpreter; Perl will not attempt to parse the rest of the file. Compare withrequire
, which can do a similar check at run time. Symmetrically,no VERSION
allows you to specify that you want a version of Perl older than the specified one.use VERSION
の形式では、VERSION は v5.24.1 のような v-文字列 ($^V
(またの名を $PERL_VERSION) と比較されます) か、5.024001 の形の数値形式 ($]
と比較されます) で 指定します。 VERSION が Perl の現在のバージョンより大きいと、例外が発生します; Perl はファイルの残りを読み込みません。require
と似ていますが、これは実行時に チェックされます。 対称的に、no VERSION
は指定されたバージョンより古いバージョンの Perl で 動作させたいことを意味します。Specifying VERSION as a numeric argument of the form 5.024001 should generally be avoided as older less readable syntax compared to v5.24.1. Before perl 5.8.0 released in 2002 the more verbose numeric form was the only supported syntax, which is why you might see it in
VERSION に 5.024001 の形の数値引数を指定することは一般的には避けるべきです; v5.24.1 に比べてより古く読みにくい文法だからです。 (2002 年にリリースされた) perl 5.8.0 より前では、より冗長な 数値形式が唯一対応している文法でした; これが古いコードでこれを 見るかも知れない理由です。
use v5.24.1; # compile time version check use 5.24.1; # ditto use 5.024_001; # ditto; older syntax compatible with perl 5.6
use v5.24.1; # 実行時バージョンチェック use 5.24.1; # 同様 use 5.024_001; # 同様; perl 5.6 と互換性のある古い文法
This is often useful if you need to check the current Perl version before
use
ing library modules that won't work with older versions of Perl. (We try not to do this more than we have to.)これは古いバージョンの Perl で動かなくなったライブラリモジュールを
use
する前に、現在の Perl のバージョンを 調べたい場合に有用です。 (我々は必要な場合以外にそのようなことがないように努力していますが。)use VERSION
also lexically enables all features available in the requested version as defined by the feature pragma, disabling any features not in the requested version's feature bundle. See feature. Similarly, if the specified Perl version is greater than or equal to 5.12.0, strictures are enabled lexically as withuse strict
. Any explicit use ofuse strict
orno strict
overridesuse VERSION
, even if it comes before it. Later use ofuse VERSION
will override all behavior of a previoususe VERSION
, possibly removing thestrict
andfeature
added byuse VERSION
.use VERSION
does not load the feature.pm or strict.pm files.use VERSION
は、feature プラグマで定義されたように、指定された バージョンで利用可能な全ての機能を有効にし、指定されたバージョンの機能の 束にない機能をレキシカルに無効にします。 feature を参照してください。 同様に、指定された Perl のバージョンが 5.12.0 以上の場合、 制限はuse strict
と同様にレキシカルに有効になります。 明示的にuse strict
やno strict
を使うと、例え先に 指定されていたとしても、use VERSION
を上書きします。 後から使ったuse VERSION
は先のuse VERSION
の全ての振る舞いを上書きするので、use VERSION
によって追加されたstrict
とfeature
を 削除することがあります。use VERSION
は feature.pm と strict.pm ファイルは読み込みません。The
BEGIN
forces therequire
andimport
to happen at compile time. Therequire
makes sure the module is loaded into memory if it hasn't been yet. Theimport
is not a builtin; it's just an ordinary static method call into theModule
package to tell the module to import the list of features back into the current package. The module can implement itsimport
method any way it likes, though most modules just choose to derive theirimport
method via inheritance from theExporter
class that is defined in theExporter
module. See Exporter. If noimport
method can be found, then the call is skipped, even if there is an AUTOLOAD method.BEGIN
によって、require
やimport
は、コンパイル時に 実行されることになります。require
は、モジュールがまだメモリに ロードされていなければ、ロードします。import
は、組込みの関数ではありません; さまざまな機能を 現在のパッケージにインポートするようにModule
パッケージに伝えるために 呼ばれる、通常の静的メソッドです。 モジュール側では、import
メソッドをどのようにでも 実装することができますが、多くのモジュールでは、Exporter
モジュールで定義された、Exporter
クラスからの継承によって、import
メソッドを 行なうようにしています。 Exporterモジュールを参照してください。import
メソッドが見つからなかった場合、AUTOLOAD メソッドが あったとしても呼び出しはスキップされます。If you do not want to call the package's
import
method (for instance, to stop your namespace from being altered), explicitly supply the empty list:パッケージの
import
メソッドを呼び出したくない場合(例えば、 名前空間を変更したくない場合など)は、明示的に空リストを指定してください:use Module ();
That is exactly equivalent to
これは以下と完全に等価です:
BEGIN { require Module }
If the VERSION argument is present between Module and LIST, then the
use
will call theVERSION
method in class Module with the given version as an argument:Module と LIST の間に VERSION 引数がある場合、
use
は Module クラスのVERSION
メソッドを、与えられたバージョンを引数として呼び出します:use Module 12.34;
is equivalent to:
は以下と等価です:
BEGIN { require Module; Module->VERSION(12.34) }
The default
VERSION
method, inherited from theUNIVERSAL
class, croaks if the given version is larger than the value of the variable$Module::VERSION
.デフォルトの default
VERSION
メソッド は、UNIVERSAL
クラスから継承したもので、 与えられたバージョンが 変数$Module::VERSION
の値より大きい場合に 警告を出します。The VERSION argument cannot be an arbitrary expression. It only counts as a VERSION argument if it is a version number literal, starting with either a digit or
v
followed by a digit. Anything that doesn't look like a version literal will be parsed as the start of the LIST. Nevertheless, many attempts to use an arbitrary expression as a VERSION argument will appear to work, because Exporter'simport
method handles numeric arguments specially, performing version checks rather than treating them as things to export.VERSION 引数は任意の式ではありません。 VERSION 引数が数値または
v
に引き続いて数値であるバージョン番号リテラルの 場合にのみ扱われます。 バージョンリテラルのように見えないものは LIST の開始としてパースされます。 それにも関わらず、VERSION 引数として任意の式を使おうとする 多くの試みは動作しているように見えます; なぜなら Exporter のimport
メソッドは数値引数を特別に扱い、 それらをエクスポートするべきものと扱わずにバージョンチェックを 実行するからです。Again, there is a distinction between omitting LIST (
import
called with no arguments) and an explicit empty LIST()
(import
not called). Note that there is no comma after VERSION!繰り返すと、LIST を省略する(
import
が引数なしで 呼び出される)ことと明示的に空の LIST()
を指定する (import
は呼び出されない)ことは違います。 VERSION の後ろにカンマが不要なことに注意してください!Because this is a wide-open interface, pragmas (compiler directives) are also implemented this way. Some of the currently implemented pragmas are:
これは、広く公開されているインタフェースですので、 プラグマ (コンパイラディレクティブ) も、この方法で実装されています。 現在実装されているプラグマには、以下のようなものがあります:
use constant; use diagnostics; use integer; use sigtrap qw(SEGV BUS); use strict qw(subs vars refs); use subs qw(afunc blurfl); use warnings qw(all); use sort qw(stable);
Some of these pseudo-modules import semantics into the current block scope (like
strict
orinteger
, unlike ordinary modules, which import symbols into the current package (which are effective through the end of the file).通常のモジュールが、現在のパッケージにシンボルをインポートする (これは、ファイルの終わりまで有効です) のに対して、これらの擬似モジュールの 一部(
strict
やinteger
など)は、現在の ブロックスコープにインポートを行ないます。Because
use
takes effect at compile time, it doesn't respect the ordinary flow control of the code being compiled. In particular, putting ause
inside the false branch of a conditional doesn't prevent it from being processed. If a module or pragma only needs to be loaded conditionally, this can be done using the if pragma:use
はコンパイル時に有効なので、コードが コンパイルされる際の通常の流れ制御には従いません。 特に、条件文のうち成立しない側の中にuse
を 書いても、処理を妨げられません。 モジュールやプラグマを条件付きでのみ読み込みたい場合、 if プラグマを使って実現できます:use if $] < 5.008, "utf8"; use if WANT_WARNINGS, warnings => qw(all);
There's a corresponding
no
declaration that unimports meanings imported byuse
, i.e., it callsModule->unimport(LIST)
instead ofimport
. It behaves just asimport
does with VERSION, an omitted or empty LIST, or no unimport method being found.これに対して、
no
宣言という、use
によってインポートされたものを、 インポートされていないことにするものがあります; つまり、import
の代わりにModule->unimport(LIST)
を呼び出します。 これは VERSION、省略された LIST、空の LIST、unimport メソッドが見つからない 場合などの観点では、import
と同様に振る舞います。no integer; no strict 'refs'; no warnings;
Care should be taken when using the
no VERSION
form ofno
. It is only meant to be used to assert that the running Perl is of a earlier version than its argument and not to undo the feature-enabling side effects ofuse VERSION
.no
のno VERSION
形式を使うときには 注意を払うべきです。 これは引数で指定されたバージョンよりも前の Perl で実行されたときに アサートされることを意味する だけ で、use VERSION
によって 有効にされた副作用をなかったことにするもの ではありません。See perlmodlib for a list of standard modules and pragmas. See perlrun for the
-M
and-m
command-line options to Perl that giveuse
functionality from the command-line.標準モジュールやプラグマの一覧は、perlmodlib を参照してください。 コマンドラインから
use
機能を 指定するための-M
と-m
の コマンドラインオプションについては perlrun を参照してください。