=encoding euc-jp =head1 NAME =begin original warnings - Perl pragma to control optional warnings =end original warnings - 選択的な警告を調整する Perl プラグマ =head1 SYNOPSIS use warnings; no warnings; use warnings "all"; no warnings "all"; use warnings::register; if (warnings::enabled()) { warnings::warn("some warning"); } if (warnings::enabled("void")) { warnings::warn("void", "some warning"); } if (warnings::enabled($object)) { warnings::warn($object, "some warning"); } warnings::warnif("some warning"); warnings::warnif("void", "some warning"); warnings::warnif($object, "some warning"); =head1 DESCRIPTION =begin original The C pragma gives control over which warnings are enabled in which parts of a Perl program. It's a more flexible alternative for both the command line flag B<-w> and the equivalent Perl variable, C<$^W>. =end original C プラグマは、Perl プログラムのある部分に対してどの警告を 有効にするかを制御できるようにします。 これはコマンドラインオプション B<-w> および等価な Perl 変数 C<$^W> よりも より柔軟な代替案です。 =begin original This pragma works just like the C pragma. This means that the scope of the warning pragma is limited to the enclosing block. It also means that the pragma setting will not leak across files (via C, C or C). This allows authors to independently define the degree of warning checks that will be applied to their module. =end original このプラグマはちょうど C プラグマと同様に動作します。 つまり、警告プラグマのスコープは閉じたブロック内に限定されます。 また、プラグマ設定は (C, C, C を通して)ファイルを超えて 漏洩することはありません。 これにより、モジュール作者は警告チェックの度合いを独立に 設定できるようになります。 =begin original By default, optional warnings are disabled, so any legacy code that doesn't attempt to control the warnings will work unchanged. =end original デフォルトでは、オプションの警告は無効なので、警告を制御しようとしない レガシーコードは変更なしで動作します。 =begin original All warnings are enabled in a block by either of these: =end original あるブロック内で全ての警告を有効にするには以下のどちらかのようにします: use warnings; use warnings 'all'; =begin original Similarly all warnings are disabled in a block by either of these: =end original 同様に、あるブロック内で全ての警告を無効にするには以下のどちらかのように します: no warnings; no warnings 'all'; =begin original For example, consider the code below: =end original 例えば、以下のコードを考えます: use warnings; my @a; { no warnings; my $b = @a[0]; } my $c = @a[0]; =begin original The code in the enclosing block has warnings enabled, but the inner block has them disabled. In this case that means the assignment to the scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]"> warning, but the assignment to the scalar C<$b> will not. =end original 外側のブロックでは警告は有効ですが、内側のブロックでは無効です。 この場合、スカラ C<$c> への代入では C<"Scalar value @a[0] better written as $a[0]"> 警告が出ますが、 スカラ C<$b> への代入では出ません。 =head2 Default Warnings and Optional Warnings (デフォルトの警告とオプションの警告) =begin original Before the introduction of lexical warnings, Perl had two classes of warnings: mandatory and optional. =end original レキシカル警告の説明の前に、Perl は二つの警告クラスがあります: 強制的(mandatory)とオプション(optional)です。 =begin original As its name suggests, if your code tripped a mandatory warning, you would get a warning whether you wanted it or not. For example, the code below would always produce an C<"isn't numeric"> warning about the "2:". =end original 名前が示しているように、コードが強制的な警告に引っかかると、望むと 望まな意図にかかわらず警告を出力します。 例えば、以下のコードは "2:" の部分に対して常に C<"isn't numeric"> 警告を 出力します。 my $a = "2:" + 3; =begin original With the introduction of lexical warnings, mandatory warnings now become I warnings. The difference is that although the previously mandatory warnings are still enabled by default, they can then be subsequently enabled or disabled with the lexical warning pragma. For example, in the code below, an C<"isn't numeric"> warning will only be reported for the C<$a> variable. =end original レキシカルな警告の導入によって、強制的な警告は I<デフォルトの> 警告と なりました。 違いは、以前の強制的な警告は今でもデフォルトで有効ですが、引き続く レキシカルな警告プラグマで有効/無効にに出来ることです。 例えば、以下のコードでは、C<"isn't numeric"> 警告は C<$a> 変数に対してだけ 報告されます。 my $a = "2:" + 3; no warnings; my $b = "2:" + 3; =begin original Note that neither the B<-w> flag or the C<$^W> can be used to disable/enable default warnings. They are still mandatory in this case. =end original B<-w> オプションや C<$^W> はデフォルトの警告を無効/有効にするのには 使えないことに注意してください。 この場合は強制的なままです。 =head2 What's wrong with B<-w> and C<$^W> (B<-w> や C<$^W> の何が悪いの?) =begin original Although very useful, the big problem with using B<-w> on the command line to enable warnings is that it is all or nothing. Take the typical scenario when you are writing a Perl program. Parts of the code you will write yourself, but it's very likely that you will make use of pre-written Perl modules. If you use the B<-w> flag in this case, you end up enabling warnings in pieces of code that you haven't written. =end original 警告を有効にするのにコマンドラインで B<-w> を使うというのはとても 便利ですが、オールオアナッシングであるという問題があります。 Perl のプログラムを書いているときのよくある状況を考えます。 コードの一部はあなた自身が書きますが、かなり確実に既に書かれている Perl モジュールを利用します。 このような場合に B<-w> フラグを使うと、あなたが書いていないコードに 対しても警告を有効にすることになります。 =begin original Similarly, using C<$^W> to either disable or enable blocks of code is fundamentally flawed. For a start, say you want to disable warnings in a block of code. You might expect this to be enough to do the trick: =end original 同様に、コードブロックで有効または無効にするために C<$^W> を使うことにも 本質的な欠点があります。 まず、コードブロックで警告を無効にしたいとします。 以下のようにすれば十分だと考えるかもしれません: { local ($^W) = 0; my $a =+ 2; my $b; chop $b; } =begin original When this code is run with the B<-w> flag, a warning will be produced for the C<$a> line: C<"Reversed += operator">. =end original このコードが B<-w> フラグ付きで実行されると、C<$a> の行で警告が 出ます: C<"Reversed += operator">。 =begin original The problem is that Perl has both compile-time and run-time warnings. To disable compile-time warnings you need to rewrite the code like this: =end original 問題は、Perl にはコンパイル時警告と実行時警告があると言うことです。 コンパイル時警告を無効にするには、以下のようにコードを書き直す必要が あります: { BEGIN { $^W = 0 } my $a =+ 2; my $b; chop $b; } =begin original The other big problem with C<$^W> is the way you can inadvertently change the warning setting in unexpected places in your code. For example, when the code below is run (without the B<-w> flag), the second call to C will trip a C<"Use of uninitialized value"> warning, whereas the first will not. =end original C<$^W> に関するもう一つの問題は、コード中の予想外の位置の設定で不用意に 警告設定が変わるということです。 例えば、以下のコードが(B<-w> フラグなしで)実行されると、C の 2 回目の呼び出しで C<"Use of uninitialized value"> 警告が出ますが、 1 回目では出ません。 sub doit { my $b; chop $b; } doit(); { local ($^W) = 1; doit() } =begin original This is a side-effect of C<$^W> being dynamically scoped. =end original これは C<$^W> が動的スコープを持つことの副作用です。 =begin original Lexical warnings get around these limitations by allowing finer control over where warnings can or can't be tripped. =end original レキシカルな警告は、どこで警告に引っかかるか引っかからないかに関して より精度の高い制御をすることで、これらの制限を回避します。 =head2 Controlling Warnings from the Command Line (コマンドラインから警告を制御する) =begin original There are three Command Line flags that can be used to control when warnings are (or aren't) produced: =end original いつ警告が発生する(あるいは発生しない)かを制御するために使われる 三つのコマンドラインフラグがあります: =over 5 =item B<-w> X<-w> =begin original This is the existing flag. If the lexical warnings pragma is B used in any of you code, or any of the modules that you use, this flag will enable warnings everywhere. See L for details of how this flag interacts with lexical warnings. =end original これは既存のフラグです。 レキシカル警告プラグマがあなたのコードやあなたが使っているモジュールの どこでも B<使われていない> なら、このフラグは全ての場所で警告を 有効にします。 このフラグがレキシカル警告とどのように相互作用するかに関する詳細については L を参照してください。 =item B<-W> X<-W> =begin original If the B<-W> flag is used on the command line, it will enable all warnings throughout the program regardless of whether warnings were disabled locally using C or C<$^W =0>. This includes all files that get included via C, C or C. Think of it as the Perl equivalent of the "lint" command. =end original コマンドラインで B<-W> フラグが使われると、プログラム中で C や C<$^W =0> を使って警告を無効にしていても無視して、全ての 警告を有効にします。 これは C, C, C 経由で読み込まれる全てのファイルにも 適用されます。 Perl 用の "lint" コマンドの等価物と考えられます。 =item B<-X> X<-X> =begin original Does the exact opposite to the B<-W> flag, i.e. it disables all warnings. =end original 正確に B<-W> フラグの逆を行います; つまり、全ての警告を無効にします。 =back =head2 Backward Compatibility (後方互換性) =begin original If you are used to working with a version of Perl prior to the introduction of lexically scoped warnings, or have code that uses both lexical warnings and C<$^W>, this section will describe how they interact. =end original レキシカルスコープ警告が導入される前のバージョンの Perl で動作させていたり、 レキシカル警告と C<$^W> の両方のコードがある場合、この節はこれらが どのように相互作用するかを記述しています。 =begin original How Lexical Warnings interact with B<-w>/C<$^W>: =end original レキシカル警告と B<-w>/C<$^W> の相互作用: =over 5 =item 1. =begin original If none of the three command line flags (B<-w>, B<-W> or B<-X>) that control warnings is used and neither C<$^W> nor the C pragma are used, then default warnings will be enabled and optional warnings disabled. This means that legacy code that doesn't attempt to control the warnings will work unchanged. =end original 警告を制御する三つのコマンドラインフラグ (B<-w>, B<-W> or B<-X>) の どれも使われておらず、C<$^W> や the C プラグマも使われていない 場合、デフォルトの警告が有効になり、オプションの警告は無効になります。 これにより、警告を制御しようとしないレガシーコードは無変更で動作します。 =item 2. =begin original The B<-w> flag just sets the global C<$^W> variable as in 5.005. This means that any legacy code that currently relies on manipulating C<$^W> to control warning behavior will still work as is. =end original 5.005 から B<-w> フラグはグローバルな C<$^W> 変数を設定します。 これにより、警告の振る舞いを制御するために C<$^W> を操作することに 依存しているレガシーコードはそのままで動作します。 =item 3. =begin original Apart from now being a boolean, the C<$^W> variable operates in exactly the same horrible uncontrolled global way, except that it cannot disable/enable default warnings. =end original 真偽値になったことは別として、C<$^W> 変数は正確に同じ恐ろしく 制御不能なグローバルな方法で操作しますが、デフォルトの警告を有効化/ 無効化することは出来ません。 =item 4. =begin original If a piece of code is under the control of the C pragma, both the C<$^W> variable and the B<-w> flag will be ignored for the scope of the lexical warning. =end original コード片が C プラグマの制御下にある場合、C<$^W> 変数と B<-w> フラグの両方はレキシカル警告のスコープで無視されます。 =item 5. =begin original The only way to override a lexical warnings setting is with the B<-W> or B<-X> command line flags. =end original レキシカル警告設定を上書きする唯一の方法は B<-W> または B<-X> コマンドラインフラグを使うことです。 =back =begin original The combined effect of 3 & 4 is that it will allow code which uses the C pragma to control the warning behavior of $^W-type code (using a C) if it really wants to, but not vice-versa. =end original 3 & 4 の組み合わせの効果により、本当に警告したいときに $^W 型のコードの 警告の振る舞いを (C を使って) 制御するために C プラグマを使えますが、逆はできません。 =head2 Category Hierarchy X (カテゴリ階層) =begin original A hierarchy of "categories" have been defined to allow groups of warnings to be enabled/disabled in isolation. =end original 「カテゴリ」の階層は、警告のグループを分離して警告を有効/無効にできるように するために定義されています。 =begin original The current hierarchy is: =end original 現在の階層は: all -+ | +- closure | +- deprecated | +- exiting | +- experimental --+ | | | +- experimental::autoderef | | | +- experimental::lexical_subs | | | +- experimental::lexical_topic | | | +- experimental::postderef | | | +- experimental::regex_sets | | | +- experimental::signatures | | | +- experimental::smartmatch | +- glob | +- imprecision | +- io ------------+ | | | +- closed | | | +- exec | | | +- layer | | | +- newline | | | +- pipe | | | +- syscalls | | | +- unopened | +- misc | +- numeric | +- once | +- overflow | +- pack | +- portable | +- recursion | +- redefine | +- regexp | +- severe --------+ | | | +- debugging | | | +- inplace | | | +- internal | | | +- malloc | +- signal | +- substr | +- syntax --------+ | | | +- ambiguous | | | +- bareword | | | +- digit | | | +- illegalproto | | | +- parenthesis | | | +- precedence | | | +- printf | | | +- prototype | | | +- qw | | | +- reserved | | | +- semicolon | +- taint | +- threads | +- uninitialized | +- unpack | +- untie | +- utf8 ----------+ | | | +- non_unicode | | | +- nonchar | | | +- surrogate | +- void =begin original Just like the "strict" pragma any of these categories can be combined =end original "strict" プラグマと同様、これらのカテゴリは組み合わせることができます use warnings qw(void redefine); no warnings qw(io syntax untie); =begin original Also like the "strict" pragma, if there is more than one instance of the C pragma in a given scope the cumulative effect is additive. =end original これも "strict" プラグマと同様、現在のスコープに複数の C プラグマの実体があるときは、効果は加算されます。 use warnings qw(void); # only "void" warnings enabled ... use warnings qw(io); # only "void" & "io" warnings enabled ... no warnings qw(void); # only "io" warnings enabled =begin original To determine which category a specific warning has been assigned to see L. =end original ある特定の警告がどのカテゴリに割り当てられているかを知るには L を参照してください。 =begin original Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a sub-category of the "syntax" category. It is now a top-level category in its own right. =end original 注意: Perl 5.8.0 以前では、レキシカル警告カテゴリ "deprecated" は "syntax" カテゴリの副カテゴリでした。 今ではそれ自体でトップレベルカテゴリです。 =head2 Fatal Warnings X (致命的警告) =begin original The presence of the word "FATAL" in the category list will escalate any warnings detected from the categories specified in the lexical scope into fatal errors. In the code below, the use of C