warnings-1.23 > 1.06 との差分

warnings 1.06 と 1.23 の差分

11
22=encoding euc-jp
33
44=head1 NAME
55
66=begin original
77
88warnings - Perl pragma to control optional warnings
99
1010=end original
1111
1212warnings - 選択的な警告を調整する Perl プラグマ
1313
1414=head1 SYNOPSIS
1515
1616 use warnings;
1717 no warnings;
1818
1919 use warnings "all";
2020 no warnings "all";
2121
2222 use warnings::register;
2323 if (warnings::enabled()) {
2424 warnings::warn("some warning");
2525 }
2626
2727 if (warnings::enabled("void")) {
2828 warnings::warn("void", "some warning");
2929 }
3030
3131 if (warnings::enabled($object)) {
3232 warnings::warn($object, "some warning");
3333 }
3434
3535 warnings::warnif("some warning");
3636 warnings::warnif("void", "some warning");
3737 warnings::warnif($object, "some warning");
3838
3939=head1 DESCRIPTION
4040
4141=begin original
4242
43The C<warnings> pragma is a replacement for the command line flag C<-w>,
43The C<warnings> pragma gives control over which warnings are enabled in
44but the pragma is limited to the enclosing block, while the flag is global.
44which parts of a Perl program. It's a more flexible alternative for
45See L<perllexwarn> for more information.
45both the command line flag B<-w> and the equivalent Perl variable,
46C<$^W>.
4647
4748=end original
4849
49C<warnings> プラグマは、コマンドラインフラグ C<-w> 置き換えです;
50C<use warnings> プラグマは、Perl プログラムある部分に対してどの警告を
50し、このフラグはグローバルなのですが、このプラグマは閉じられた
51有効にするを制御きるようにしま
51ブロックに限定さます。
52はコマンドラインオプション B<-w> および等価な Perl 変数 C<$^W> よりも
52る情報は L<perllexwarn> を見てください
53より柔軟代替案です
5354
5455=begin original
5556
56If no import list is supplied, all possible warnings are either enabled
57This pragma works just like the C<strict> pragma.
57or disabled.
58This means that the scope of the warning pragma is limited to the
59enclosing block. It also means that the pragma setting will not
60leak across files (via C<use>, C<require> or C<do>). This allows
61authors to independently define the degree of warning checks that will
62be applied to their module.
5863
5964=end original
6065
61インポートリストを与えない場合は、可能な限り全て警告を有効にしたり
66プラグマはちょうど C<strict> プラグマと同様動作ます。
62無効にしたます。
67つま、警告プラグマのスコープは閉じたブロック内に限定されます。
68また、プラグマ設定は (C<use>, C<require>, C<do> を通して)ファイルを超えて
69漏洩することはありません。
70これにより、モジュール作者は警告チェックの度合いを独立に
71設定できるようになります。
6372
6473=begin original
6574
66A number of functions are provided to assist module authors.
75By default, optional warnings are disabled, so any legacy code that
76doesn't attempt to control the warnings will work unchanged.
6777
6878=end original
6979
70いくつかの関数は、モジュール作成者手助けをします。
80デフォルトでは、オプション警告は無効なので、警告制御ようとしない
81レガシーコードは変更なしで動作します。
7182
83=begin original
84
85All warnings are enabled in a block by either of these:
86
87=end original
88
89あるブロック内で全ての警告を有効にするには以下のどちらかのようにします:
90
91 use warnings;
92 use warnings 'all';
93
94=begin original
95
96Similarly all warnings are disabled in a block by either of these:
97
98=end original
99
100同様に、あるブロック内で全ての警告を無効にするには以下のどちらかのように
101します:
102
103 no warnings;
104 no warnings 'all';
105
106=begin original
107
108For example, consider the code below:
109
110=end original
111
112例えば、以下のコードを考えます:
113
114 use warnings;
115 my @a;
116 {
117 no warnings;
118 my $b = @a[0];
119 }
120 my $c = @a[0];
121
122=begin original
123
124The code in the enclosing block has warnings enabled, but the inner
125block has them disabled. In this case that means the assignment to the
126scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]">
127warning, but the assignment to the scalar C<$b> will not.
128
129=end original
130
131外側のブロックでは警告は有効ですが、内側のブロックでは無効です。
132この場合、スカラ C<$c> への代入では
133C<"Scalar value @a[0] better written as $a[0]"> 警告が出ますが、
134スカラ C<$b> への代入では出ません。
135
136=head2 Default Warnings and Optional Warnings
137
138(デフォルトの警告とオプションの警告)
139
140=begin original
141
142Before the introduction of lexical warnings, Perl had two classes of
143warnings: mandatory and optional.
144
145=end original
146
147レキシカル警告の説明の前に、Perl は二つの警告クラスがあります:
148強制的(mandatory)とオプション(optional)です。
149
150=begin original
151
152As its name suggests, if your code tripped a mandatory warning, you
153would get a warning whether you wanted it or not.
154For example, the code below would always produce an C<"isn't numeric">
155warning about the "2:".
156
157=end original
158
159名前が示しているように、コードが強制的な警告に引っかかると、望むと
160望まな意図にかかわらず警告を出力します。
161例えば、以下のコードは "2:" の部分に対して常に C<"isn't numeric"> 警告を
162出力します。
163
164 my $a = "2:" + 3;
165
166=begin original
167
168With the introduction of lexical warnings, mandatory warnings now become
169I<default> warnings. The difference is that although the previously
170mandatory warnings are still enabled by default, they can then be
171subsequently enabled or disabled with the lexical warning pragma. For
172example, in the code below, an C<"isn't numeric"> warning will only
173be reported for the C<$a> variable.
174
175=end original
176
177レキシカルな警告の導入によって、強制的な警告は I<デフォルトの> 警告と
178なりました。
179違いは、以前の強制的な警告は今でもデフォルトで有効ですが、引き続く
180レキシカルな警告プラグマで有効/無効にに出来ることです。
181例えば、以下のコードでは、C<"isn't numeric"> 警告は C<$a> 変数に対してだけ
182報告されます。
183
184 my $a = "2:" + 3;
185 no warnings;
186 my $b = "2:" + 3;
187
188=begin original
189
190Note that neither the B<-w> flag or the C<$^W> can be used to
191disable/enable default warnings. They are still mandatory in this case.
192
193=end original
194
195B<-w> オプションや C<$^W> はデフォルトの警告を無効/有効にするのには
196使えないことに注意してください。
197この場合は強制的なままです。
198
199=head2 What's wrong with B<-w> and C<$^W>
200
201(B<-w> や C<$^W> の何が悪いの?)
202
203=begin original
204
205Although very useful, the big problem with using B<-w> on the command
206line to enable warnings is that it is all or nothing. Take the typical
207scenario when you are writing a Perl program. Parts of the code you
208will write yourself, but it's very likely that you will make use of
209pre-written Perl modules. If you use the B<-w> flag in this case, you
210end up enabling warnings in pieces of code that you haven't written.
211
212=end original
213
214警告を有効にするのにコマンドラインで B<-w> を使うというのはとても
215便利ですが、オールオアナッシングであるという問題があります。
216Perl のプログラムを書いているときのよくある状況を考えます。
217コードの一部はあなた自身が書きますが、かなり確実に既に書かれている
218Perl モジュールを利用します。
219このような場合に B<-w> フラグを使うと、あなたが書いていないコードに
220対しても警告を有効にすることになります。
221
222=begin original
223
224Similarly, using C<$^W> to either disable or enable blocks of code is
225fundamentally flawed. For a start, say you want to disable warnings in
226a block of code. You might expect this to be enough to do the trick:
227
228=end original
229
230同様に、コードブロックで有効または無効にするために C<$^W> を使うことにも
231本質的な欠点があります。
232まず、コードブロックで警告を無効にしたいとします。
233以下のようにすれば十分だと考えるかもしれません:
234
235 {
236 local ($^W) = 0;
237 my $a =+ 2;
238 my $b; chop $b;
239 }
240
241=begin original
242
243When this code is run with the B<-w> flag, a warning will be produced
244for the C<$a> line: C<"Reversed += operator">.
245
246=end original
247
248このコードが B<-w> フラグ付きで実行されると、C<$a> の行で警告が
249出ます: C<"Reversed += operator">。
250
251=begin original
252
253The problem is that Perl has both compile-time and run-time warnings. To
254disable compile-time warnings you need to rewrite the code like this:
255
256=end original
257
258問題は、Perl にはコンパイル時警告と実行時警告があると言うことです。
259コンパイル時警告を無効にするには、以下のようにコードを書き直す必要が
260あります:
261
262 {
263 BEGIN { $^W = 0 }
264 my $a =+ 2;
265 my $b; chop $b;
266 }
267
268=begin original
269
270The other big problem with C<$^W> is the way you can inadvertently
271change the warning setting in unexpected places in your code. For example,
272when the code below is run (without the B<-w> flag), the second call
273to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas
274the first will not.
275
276=end original
277
278C<$^W> に関するもう一つの問題は、コード中の予想外の位置の設定で不用意に
279警告設定が変わるということです。
280例えば、以下のコードが(B<-w> フラグなしで)実行されると、C<doit> の
2812 回目の呼び出しで C<"Use of uninitialized value"> 警告が出ますが、
2821 回目では出ません。
283
284 sub doit
285 {
286 my $b; chop $b;
287 }
288
289 doit();
290
291 {
292 local ($^W) = 1;
293 doit()
294 }
295
296=begin original
297
298This is a side-effect of C<$^W> being dynamically scoped.
299
300=end original
301
302これは C<$^W> が動的スコープを持つことの副作用です。
303
304=begin original
305
306Lexical warnings get around these limitations by allowing finer control
307over where warnings can or can't be tripped.
308
309=end original
310
311レキシカルな警告は、どこで警告に引っかかるか引っかからないかに関して
312より精度の高い制御をすることで、これらの制限を回避します。
313
314=head2 Controlling Warnings from the Command Line
315
316(コマンドラインから警告を制御する)
317
318=begin original
319
320There are three Command Line flags that can be used to control when
321warnings are (or aren't) produced:
322
323=end original
324
325いつ警告が発生する(あるいは発生しない)かを制御するために使われる
326三つのコマンドラインフラグがあります:
327
328=over 5
329
330=item B<-w>
331X<-w>
332
333=begin original
334
335This is the existing flag. If the lexical warnings pragma is B<not>
336used in any of you code, or any of the modules that you use, this flag
337will enable warnings everywhere. See L<Backward Compatibility> for
338details of how this flag interacts with lexical warnings.
339
340=end original
341
342これは既存のフラグです。
343レキシカル警告プラグマがあなたのコードやあなたが使っているモジュールの
344どこでも B<使われていない> なら、このフラグは全ての場所で警告を
345有効にします。
346このフラグがレキシカル警告とどのように相互作用するかに関する詳細については
347L<Backward Compatibility> を参照してください。
348
349=item B<-W>
350X<-W>
351
352=begin original
353
354If the B<-W> flag is used on the command line, it will enable all warnings
355throughout the program regardless of whether warnings were disabled
356locally using C<no warnings> or C<$^W =0>.
357This includes all files that get
358included via C<use>, C<require> or C<do>.
359Think of it as the Perl equivalent of the "lint" command.
360
361=end original
362
363コマンドラインで B<-W> フラグが使われると、プログラム中で
364C<no warnings> や C<$^W =0> を使って警告を無効にしていても無視して、全ての
365警告を有効にします。
366これは C<use>, C<require>, C<do> 経由で読み込まれる全てのファイルにも
367適用されます。
368Perl 用の "lint" コマンドの等価物と考えられます。
369
370=item B<-X>
371X<-X>
372
373=begin original
374
375Does the exact opposite to the B<-W> flag, i.e. it disables all warnings.
376
377=end original
378
379正確に B<-W> フラグの逆を行います; つまり、全ての警告を無効にします。
380
381=back
382
383=head2 Backward Compatibility
384
385(後方互換性)
386
387=begin original
388
389If you are used to working with a version of Perl prior to the
390introduction of lexically scoped warnings, or have code that uses both
391lexical warnings and C<$^W>, this section will describe how they interact.
392
393=end original
394
395レキシカルスコープ警告が導入される前のバージョンの Perl で動作させていたり、
396レキシカル警告と C<$^W> の両方のコードがある場合、この節はこれらが
397どのように相互作用するかを記述しています。
398
399=begin original
400
401How Lexical Warnings interact with B<-w>/C<$^W>:
402
403=end original
404
405レキシカル警告と B<-w>/C<$^W> の相互作用:
406
407=over 5
408
409=item 1.
410
411=begin original
412
413If none of the three command line flags (B<-w>, B<-W> or B<-X>) that
414control warnings is used and neither C<$^W> nor the C<warnings> pragma
415are used, then default warnings will be enabled and optional warnings
416disabled.
417This means that legacy code that doesn't attempt to control the warnings
418will work unchanged.
419
420=end original
421
422警告を制御する三つのコマンドラインフラグ (B<-w>, B<-W> or B<-X>) の
423どれも使われておらず、C<$^W> や the C<warnings> プラグマも使われていない
424場合、デフォルトの警告が有効になり、オプションの警告は無効になります。
425これにより、警告を制御しようとしないレガシーコードは無変更で動作します。
426
427=item 2.
428
429=begin original
430
431The B<-w> flag just sets the global C<$^W> variable as in 5.005. This
432means that any legacy code that currently relies on manipulating C<$^W>
433to control warning behavior will still work as is.
434
435=end original
436
4375.005 から B<-w> フラグはグローバルな C<$^W> 変数を設定します。
438これにより、警告の振る舞いを制御するために C<$^W> を操作することに
439依存しているレガシーコードはそのままで動作します。
440
441=item 3.
442
443=begin original
444
445Apart from now being a boolean, the C<$^W> variable operates in exactly
446the same horrible uncontrolled global way, except that it cannot
447disable/enable default warnings.
448
449=end original
450
451真偽値になったことは別として、C<$^W> 変数は正確に同じ恐ろしく
452制御不能なグローバルな方法で操作しますが、デフォルトの警告を有効化/
453無効化することは出来ません。
454
455=item 4.
456
457=begin original
458
459If a piece of code is under the control of the C<warnings> pragma,
460both the C<$^W> variable and the B<-w> flag will be ignored for the
461scope of the lexical warning.
462
463=end original
464
465コード片が C<warnings> プラグマの制御下にある場合、C<$^W> 変数と
466B<-w> フラグの両方はレキシカル警告のスコープで無視されます。
467
468=item 5.
469
470=begin original
471
472The only way to override a lexical warnings setting is with the B<-W>
473or B<-X> command line flags.
474
475=end original
476
477レキシカル警告設定を上書きする唯一の方法は B<-W> または B<-X>
478コマンドラインフラグを使うことです。
479
480=back
481
482=begin original
483
484The combined effect of 3 & 4 is that it will allow code which uses
485the C<warnings> pragma to control the warning behavior of $^W-type
486code (using a C<local $^W=0>) if it really wants to, but not vice-versa.
487
488=end original
489
4903 & 4 の組み合わせの効果により、本当に警告したいときに $^W 型のコードの
491警告の振る舞いを (C<local $^W=0> を使って) 制御するために
492C<warnings> プラグマを使えますが、逆はできません。
493
494=head2 Category Hierarchy
495X<warning, categories>
496
497(カテゴリ階層)
498
499=begin original
500
501A hierarchy of "categories" have been defined to allow groups of warnings
502to be enabled/disabled in isolation.
503
504=end original
505
506「カテゴリ」の階層は、警告のグループを分離して警告を有効/無効にできるように
507するために定義されています。
508
509=begin original
510
511The current hierarchy is:
512
513=end original
514
515現在の階層は:
516
517 all -+
518 |
519 +- closure
520 |
521 +- deprecated
522 |
523 +- exiting
524 |
525 +- experimental --+
526 | |
527 | +- experimental::autoderef
528 | |
529 | +- experimental::lexical_subs
530 | |
531 | +- experimental::lexical_topic
532 | |
533 | +- experimental::postderef
534 | |
535 | +- experimental::regex_sets
536 | |
537 | +- experimental::signatures
538 | |
539 | +- experimental::smartmatch
540 |
541 +- glob
542 |
543 +- imprecision
544 |
545 +- io ------------+
546 | |
547 | +- closed
548 | |
549 | +- exec
550 | |
551 | +- layer
552 | |
553 | +- newline
554 | |
555 | +- pipe
556 | |
557 | +- syscalls
558 | |
559 | +- unopened
560 |
561 +- misc
562 |
563 +- numeric
564 |
565 +- once
566 |
567 +- overflow
568 |
569 +- pack
570 |
571 +- portable
572 |
573 +- recursion
574 |
575 +- redefine
576 |
577 +- regexp
578 |
579 +- severe --------+
580 | |
581 | +- debugging
582 | |
583 | +- inplace
584 | |
585 | +- internal
586 | |
587 | +- malloc
588 |
589 +- signal
590 |
591 +- substr
592 |
593 +- syntax --------+
594 | |
595 | +- ambiguous
596 | |
597 | +- bareword
598 | |
599 | +- digit
600 | |
601 | +- illegalproto
602 | |
603 | +- parenthesis
604 | |
605 | +- precedence
606 | |
607 | +- printf
608 | |
609 | +- prototype
610 | |
611 | +- qw
612 | |
613 | +- reserved
614 | |
615 | +- semicolon
616 |
617 +- taint
618 |
619 +- threads
620 |
621 +- uninitialized
622 |
623 +- unpack
624 |
625 +- untie
626 |
627 +- utf8 ----------+
628 | |
629 | +- non_unicode
630 | |
631 | +- nonchar
632 | |
633 | +- surrogate
634 |
635 +- void
636
637=begin original
638
639Just like the "strict" pragma any of these categories can be combined
640
641=end original
642
643"strict" プラグマと同様、これらのカテゴリは組み合わせることができます
644
645 use warnings qw(void redefine);
646 no warnings qw(io syntax untie);
647
648=begin original
649
650Also like the "strict" pragma, if there is more than one instance of the
651C<warnings> pragma in a given scope the cumulative effect is additive.
652
653=end original
654
655これも "strict" プラグマと同様、現在のスコープに複数の
656C<warnings> プラグマの実体があるときは、効果は加算されます。
657
658 use warnings qw(void); # only "void" warnings enabled
659 ...
660 use warnings qw(io); # only "void" & "io" warnings enabled
661 ...
662 no warnings qw(void); # only "io" warnings enabled
663
664=begin original
665
666To determine which category a specific warning has been assigned to see
667L<perldiag>.
668
669=end original
670
671ある特定の警告がどのカテゴリに割り当てられているかを知るには
672L<perldiag> を参照してください。
673
674=begin original
675
676Note: Before Perl 5.8.0, the lexical warnings category "deprecated" was a
677sub-category of the "syntax" category. It is now a top-level category
678in its own right.
679
680=end original
681
682注意: Perl 5.8.0 以前では、レキシカル警告カテゴリ "deprecated" は "syntax"
683カテゴリの副カテゴリでした。
684今ではそれ自体でトップレベルカテゴリです。
685
686=head2 Fatal Warnings
687X<warning, fatal>
688
689(致命的警告)
690
691=begin original
692
693The presence of the word "FATAL" in the category list will escalate any
694warnings detected from the categories specified in the lexical scope
695into fatal errors. In the code below, the use of C<time>, C<length>
696and C<join> can all produce a C<"Useless use of xxx in void context">
697warning.
698
699=end original
700
701カテゴリ一覧中に "FATAL" の文字があると、レキシカルスコープで
702指定されたカテゴリで検出された全ての警告を致命的エラーに昇格させます。
703以下のコードでは、C<time>, C<length>, C<join> の使用は全て
704C<"Useless use of xxx in void context"> 警告を出力します。
705
706 use warnings;
707
708 time;
709
710 {
711 use warnings FATAL => qw(void);
712 length "abc";
713 }
714
715 join "", 1,2,3;
716
717 print "done\n";
718
719=begin original
720
721When run it produces this output
722
723=end original
724
725実行すると、以下の出力を生成します
726
727 Useless use of time in void context at fatal line 3.
728 Useless use of length in void context at fatal line 7.
729
730=begin original
731
732The scope where C<length> is used has escalated the C<void> warnings
733category into a fatal error, so the program terminates immediately when it
734encounters the warning.
735
736=end original
737
738C<length> が使われているスコープでは C<void> 警告カテゴリを致命的エラーに
739昇格させるので、この警告に出会うとプログラムは直ちに終了します。
740
741=begin original
742
743To explicitly turn off a "FATAL" warning you just disable the warning
744it is associated with. So, for example, to disable the "void" warning
745in the example above, either of these will do the trick:
746
747=end original
748
749明示的に "FATAL" 警告をオフにするには、単に関連する警告を無効にします。
750それで、例えば、上述の例で "void" 警告を無効にするには、以下の二つの
751技のどちらかを使います:
752
753 no warnings qw(void);
754 no warnings FATAL => qw(void);
755
756=begin original
757
758If you want to downgrade a warning that has been escalated into a fatal
759error back to a normal warning, you can use the "NONFATAL" keyword. For
760example, the code below will promote all warnings into fatal errors,
761except for those in the "syntax" category.
762
763=end original
764
765致命的エラーに昇格した警告を通常の警告に降格させたい場合、
766"NONFATAL" きーわーどが使えます。
767例えば、以下のコードは "syntax" カテゴリ以外の全ての警告を致命的エラーに
768昇格させます。
769
770 use warnings FATAL => 'all', NONFATAL => 'syntax';
771
772=begin original
773
774As of Perl 5.20, instead of C<< use warnings FATAL => 'all'; >> you can
775use:
776
777=end original
778
779Perl 5.20 から、C<< use warnings FATAL => 'all'; >> の代わりに以下のものが
780使えます:
781
782 use v5.20; # Perl 5.20 or greater is required for the following
783 use warnings 'FATAL'; # short form of "use warnings FATAL => 'all';"
784
785=begin original
786
787If you want your program to be compatible with versions of Perl before
7885.20, you must use C<< use warnings FATAL => 'all'; >> instead. (In
789previous versions of Perl, the behavior of the statements
790C<< use warnings 'FATAL'; >>, C<< use warnings 'NONFATAL'; >> and
791C<< no warnings 'FATAL'; >> was unspecified; they did not behave as if
792they included the C<< => 'all' >> portion. As of 5.20, they do.)
793
794=end original
795
7965.20 以前の Perl との互換性が必要なら、代わりに
797C<< use warnings FATAL => 'all'; >> を使わなければなりません。
798(以前のバージョンの Perl では C<< use warnings 'FATAL'; >>,
799C<< use warnings 'NONFATAL'; >>, C<< no warnings 'FATAL'; >> という文の
800振る舞いは未定義です; C<< => 'all' >> 部を含むようには振る舞いません。
8015.20 からはそうなります。)
802
803=begin original
804
805B<NOTE:> Users of FATAL warnings, especially
806those using C<< FATAL => 'all' >>
807should be fully aware that they are risking future portability of their
808programs by doing so. Perl makes absolutely no commitments to not
809introduce new warnings, or warnings categories in the future, and indeed
810we explicitly reserve the right to do so. Code that may not warn now may
811warn in a future release of Perl if the Perl5 development team deems it
812in the best interests of the community to do so. Should code using FATAL
813warnings break due to the introduction of a new warning we will NOT
814consider it an incompatible change. Users of FATAL warnings should take
815special caution during upgrades to check to see if their code triggers
816any new warnings and should pay particular attention to the fine print of
817the documentation of the features they use to ensure they do not exploit
818features that are documented as risky, deprecated, or unspecified, or where
819the documentation says "so don't do that", or anything with the same sense
820and spirit. Use of such features in combination with FATAL warnings is
821ENTIRELY AT THE USER'S RISK.
822
823=end original
824
825B<注意:> FATAL 警告の使用、特に C<< FATAL => 'all' >> の使用は、
826そうすることによる将来の移植性のリスクによく注意する必要があります。
827Perl は将来新しい警告や警告カテゴリを導入しないという保証は全くなく、
828明示的にそうする権利を保留します。
829そうするのがコミュニティにとって最良であると Perl5 開発チームが判断すれば、
830現在警告されていないコードが将来警告されるかもしれません。
831新しい警告の導入によって FATAL 警告を使っているコードが壊れても、これは
832互換性のない変更とは考えません。
833FATAL 警告を使う場合は、コードが新しい警告を引き起こさないかを調べて、
834特に、文書に、リスクがある、廃止予定、未定義、「しないでください」と
835書いてある、あるいは同じような意味と精神の機能を乱用しないように、機能の文書に
836特に注意を払うべきです。
837これらの機能を FATAL 警告と組み合わせるのは自身のリスクで行ってください。
838
839=head2 Reporting Warnings from a Module
840X<warning, reporting> X<warning, registering>
841
842(モジュールから警告を報告する)
843
844=begin original
845
846The C<warnings> pragma provides a number of functions that are useful for
847module authors. These are used when you want to report a module-specific
848warning to a calling module has enabled warnings via the C<warnings>
849pragma.
850
851=end original
852
853C<warnings> プラグマはモジュール作者にとって有用ないくつかの関数を
854提供します。
855C<warnings> プラグマ経由で有効になったモジュール固有の警告を呼び出し元に
856報告するときに使われます。
857
858=begin original
859
860Consider the module C<MyMod::Abc> below.
861
862=end original
863
864以下の C<MyMod::Abc> モジュールを考えます。
865
866 package MyMod::Abc;
867
868 use warnings::register;
869
870 sub open {
871 my $path = shift;
872 if ($path !~ m#^/#) {
873 warnings::warn("changing relative path to /var/abc")
874 if warnings::enabled();
875 $path = "/var/abc/$path";
876 }
877 }
878
879 1;
880
881=begin original
882
883The call to C<warnings::register> will create a new warnings category
884called "MyMod::Abc", i.e. the new category name matches the current
885package name. The C<open> function in the module will display a warning
886message if it gets given a relative path as a parameter. This warnings
887will only be displayed if the code that uses C<MyMod::Abc> has actually
888enabled them with the C<warnings> pragma like below.
889
890=end original
891
892C<warnings::register> の呼び出しにより、"MyMod::Abc" という名前の新しい
893警告カテゴリを作成します; つまり、新しいカテゴリ名は現在のパッケージ名に
894一致します。
895このモジュールの C<open> 関数は、引数として相対パスが与えられると
896警告メッセージを出力します。
897この警告は、C<MyMod::Abc> を使うコードが 以下のようにして
898C<warnings> によって有効にされた場合にのみ出力されます。
899
900 use MyMod::Abc;
901 use warnings 'MyMod::Abc';
902 ...
903 abc::open("../fred.txt");
904
905=begin original
906
907It is also possible to test whether the pre-defined warnings categories are
908set in the calling module with the C<warnings::enabled> function. Consider
909this snippet of code:
910
911=end original
912
913また、C<warnings::enabled> 関数を使って、既に定義されているカテゴリが
914呼び出しモジュールで設定されているかどうかをテストすることも可能です。
915以下のコード片を考えます:
916
917 package MyMod::Abc;
918
919 sub open {
920 warnings::warnif("deprecated",
921 "open is deprecated, use new instead");
922 new(@_);
923 }
924
925 sub new
926 ...
927 1;
928
929=begin original
930
931The function C<open> has been deprecated, so code has been included to
932display a warning message whenever the calling module has (at least) the
933"deprecated" warnings category enabled. Something like this, say.
934
935=end original
936
937C<open> 関数は非推奨なので、呼び出しモジュールで (少なくとも)
938"deprecated" 警告カテゴリが有効のとき警告を出力するコードが含まれています。
939つまりこんな感じです。
940
941 use warnings 'deprecated';
942 use MyMod::Abc;
943 ...
944 MyMod::Abc::open($filename);
945
946=begin original
947
948Either the C<warnings::warn> or C<warnings::warnif> function should be
949used to actually display the warnings message. This is because they can
950make use of the feature that allows warnings to be escalated into fatal
951errors. So in this case
952
953=end original
954
955実際に警告メッセージを出力するには、C<warnings::warn> 関数と
956C<warnings::warnif> 関数のどちらかを使うべきです。
957これは、警告を致命的エラーに昇格させる機能を使えるようにするためです。
958それで、このような場合:
959
960 use MyMod::Abc;
961 use warnings FATAL => 'MyMod::Abc';
962 ...
963 MyMod::Abc::open('../fred.txt');
964
965=begin original
966
967the C<warnings::warnif> function will detect this and die after
968displaying the warning message.
969
970=end original
971
972C<warnings::warnif> 関数はこれを検出して、警告を出力した後 die します。
973
974=begin original
975
976The three warnings functions, C<warnings::warn>, C<warnings::warnif>
977and C<warnings::enabled> can optionally take an object reference in place
978of a category name. In this case the functions will use the class name
979of the object as the warnings category.
980
981=end original
982
983三つの警告関数 C<warnings::warn>, C<warnings::warnif>,
984C<warnings::enabled> は、オプションとしてカテゴリ名の代わりにオブジェクトの
985リファレンスをとることができます。
986この場合関数は警告カテゴリとしてオブジェクトのクラス名を使います。
987
988=begin original
989
990Consider this example:
991
992=end original
993
994この例を考えます:
995
996 package Original;
997
998 no warnings;
999 use warnings::register;
1000
1001 sub new
1002 {
1003 my $class = shift;
1004 bless [], $class;
1005 }
1006
1007 sub check
1008 {
1009 my $self = shift;
1010 my $value = shift;
1011
1012 if ($value % 2 && warnings::enabled($self))
1013 { warnings::warn($self, "Odd numbers are unsafe") }
1014 }
1015
1016 sub doit
1017 {
1018 my $self = shift;
1019 my $value = shift;
1020 $self->check($value);
1021 # ...
1022 }
1023
1024 1;
1025
1026 package Derived;
1027
1028 use warnings::register;
1029 use Original;
1030 our @ISA = qw( Original );
1031 sub new
1032 {
1033 my $class = shift;
1034 bless [], $class;
1035 }
1036
1037 1;
1038
1039=begin original
1040
1041The code below makes use of both modules, but it only enables warnings from
1042C<Derived>.
1043
1044=end original
1045
1046以下のコードは両方のモジュールを使っていますが、C<Derived> からの警告だけを
1047有効にしています。
1048
1049 use Original;
1050 use Derived;
1051 use warnings 'Derived';
1052 my $a = Original->new();
1053 $a->doit(1);
1054 my $b = Derived->new();
1055 $a->doit(1);
1056
1057=begin original
1058
1059When this code is run only the C<Derived> object, C<$b>, will generate
1060a warning.
1061
1062=end original
1063
1064このコードが C<Derived> オブジェクトからのみ実行されているとき、
1065C<$b> は警告を出力します。
1066
1067 Odd numbers are unsafe at main.pl line 7
1068
1069=begin original
1070
1071Notice also that the warning is reported at the line where the object is first
1072used.
1073
1074=end original
1075
1076オブジェクトが最初に使われた行で警告が報告されることにも注意してください。
1077
1078=begin original
1079
1080When registering new categories of warning, you can supply more names to
1081warnings::register like this:
1082
1083=end original
1084
1085警告の新しいカテゴリを登録するとき、以下のようにして warnings::register に
1086さらなる名前を提供できます:
1087
1088 package MyModule;
1089 use warnings::register qw(format precision);
1090
1091 ...
1092
1093 warnings::warnif('MyModule::format', '...');
1094
1095=head1 FUNCTIONS
1096
1097(関数)
1098
721099=over 4
731100
741101=item use warnings::register
751102
761103=begin original
771104
781105Creates a new warnings category with the same name as the package where
791106the call to the pragma is used.
801107
811108=end original
821109
831110プラグマを呼び出したパッケージと同じ名前の新しい警告カテゴリを作成します。
841111
851112=item warnings::enabled()
861113
871114=begin original
881115
891116Use the warnings category with the same name as the current package.
901117
911118=end original
921119
931120現在のパッケージと同じ名前の警告カテゴリを使います。
941121
951122=begin original
961123
971124Return TRUE if that warnings category is enabled in the calling module.
981125Otherwise returns FALSE.
991126
1001127=end original
1011128
1021129呼ばれたモジュール内でその警告カテゴリが有効ならば真(TRUE)を返します。
1031130そうでなければ偽(FALSE)を返します。
1041131
1051132=item warnings::enabled($category)
1061133
1071134=begin original
1081135
1091136Return TRUE if the warnings category, C<$category>, is enabled in the
1101137calling module.
1111138Otherwise returns FALSE.
1121139
1131140=end original
1141141
1151142呼ばれたモジュール内で警告カテゴリ(C<$category>)が有効ならば真
1161143(TRUE)を返します。
1171144そうでなければ偽(FALSE)を返します。
1181145
1191146=item warnings::enabled($object)
1201147
1211148=begin original
1221149
1231150Use the name of the class for the object reference, C<$object>, as the
1241151warnings category.
1251152
1261153=end original
1271154
1281155オブジェクトリファレンス(C<$object>)のクラス名を警告カテゴリとして
1291156使います。
1301157
1311158=begin original
1321159
1331160Return TRUE if that warnings category is enabled in the first scope
1341161where the object is used.
1351162Otherwise returns FALSE.
1361163
1371164=end original
1381165
1391166そのオブジェクトが使われた最初のスコープ内でその警告カテゴリが有効ならば
1401167真(TRUE)を返します。
1411168そうでなければ偽(FALSE)を返します。
1421169
1170=item warnings::fatal_enabled()
1171
1172=begin original
1173
1174Return TRUE if the warnings category with the same name as the current
1175package has been set to FATAL in the calling module.
1176Otherwise returns FALSE.
1177
1178=end original
1179
1180呼ばれたモジュール内で、現在のパッケージと同じ名前の警告カテゴリが FATAL に
1181設定されているならば真(TRUE)を返します。
1182そうでなければ偽(FALSE)を返します。
1183
1184=item warnings::fatal_enabled($category)
1185
1186=begin original
1187
1188Return TRUE if the warnings category C<$category> has been set to FATAL in
1189the calling module.
1190Otherwise returns FALSE.
1191
1192=end original
1193
1194呼ばれたモジュール内で、警告カテゴリ(C<$category>)が FATAL に
1195設定されているならば真(TRUE)を返します。
1196そうでなければ偽(FALSE)を返します。
1197
1198=item warnings::fatal_enabled($object)
1199
1200=begin original
1201
1202Use the name of the class for the object reference, C<$object>, as the
1203warnings category.
1204
1205=end original
1206
1207オブジェクトリファレンス(C<$object>)のクラス名を警告カテゴリとして
1208使います。
1209
1210=begin original
1211
1212Return TRUE if that warnings category has been set to FATAL in the first
1213scope where the object is used.
1214Otherwise returns FALSE.
1215
1216=end original
1217
1218そのオブジェクトが使われた最初のスコープ内でその警告カテゴリが FATAL に
1219設定されているならば真(TRUE)を返します。
1220そうでなければ偽(FALSE)を返します。
1221
1431222=item warnings::warn($message)
1441223
1451224=begin original
1461225
1471226Print C<$message> to STDERR.
1481227
1491228=end original
1501229
1511230STDERR に C<$message> を出力します。
1521231
1531232=begin original
1541233
1551234Use the warnings category with the same name as the current package.
1561235
1571236=end original
1581237
1591238現在のパッケージと同じ名前の警告カテゴリを使います。
1601239
1611240=begin original
1621241
1631242If that warnings category has been set to "FATAL" in the calling module
1641243then die. Otherwise return.
1651244
1661245=end original
1671246
1681247もし、呼ばれたモジュール内でその警告カテゴリーに "FATAL" が
1691248設定されていたならば、終了(die)します。
1701249
1711250=item warnings::warn($category, $message)
1721251
1731252=begin original
1741253
1751254Print C<$message> to STDERR.
1761255
1771256=end original
1781257
1791258STDERR に C<$message> を出力します。
1801259
1811260=begin original
1821261
1831262If the warnings category, C<$category>, has been set to "FATAL" in the
1841263calling module then die. Otherwise return.
1851264
1861265=end original
1871266
1881267もし、呼ばれたモジュール内で警告カテゴリ(C<$category>)に "FATAL" が
1891268設定されていたならば、終了(die)します。
1901269
1911270=item warnings::warn($object, $message)
1921271
1931272=begin original
1941273
1951274Print C<$message> to STDERR.
1961275
1971276=end original
1981277
1991278STDERR に C<$message> を出力します。
2001279
2011280=begin original
2021281
2031282Use the name of the class for the object reference, C<$object>, as the
2041283warnings category.
2051284
2061285=end original
2071286
2081287オブジェクトリファレンス(C<$object>)のクラス名を警告カテゴリとして
2091288使います。
2101289
2111290=begin original
2121291
2131292If that warnings category has been set to "FATAL" in the scope where C<$object>
2141293is first used then die. Otherwise return.
2151294
2161295=end original
2171296
2181297もし、C<$object> が最初に使われたスコープ内でその警告カテゴリに
2191298"FATAL" が設定されていたならば、終了(die)します。
2201299
2211300=item warnings::warnif($message)
2221301
2231302=begin original
2241303
2251304Equivalent to:
2261305
2271306=end original
2281307
2291308以下のものと等価です:
2301309
2311310 if (warnings::enabled())
2321311 { warnings::warn($message) }
2331312
2341313=item warnings::warnif($category, $message)
2351314
2361315=begin original
2371316
2381317Equivalent to:
2391318
2401319=end original
2411320
2421321以下のものと等価です:
2431322
2441323 if (warnings::enabled($category))
2451324 { warnings::warn($category, $message) }
2461325
2471326=item warnings::warnif($object, $message)
2481327
2491328=begin original
2501329
2511330Equivalent to:
2521331
2531332=end original
2541333
2551334以下のものと等価です:
2561335
2571336 if (warnings::enabled($object))
2581337 { warnings::warn($object, $message) }
2591338
1339=item warnings::register_categories(@names)
1340
1341=begin original
1342
1343This registers warning categories for the given names and is primarily for
1344use by the warnings::register pragma.
1345
1346=end original
1347
1348これは指定された名前の警告カテゴリを登録します; 主に
1349warnings::register プラグマで使われるものです。
1350
2601351=back
2611352
2621353=begin original
2631354
264See L<perlmodlib/Pragmatic Modules> and L<perllexwarn>.
1355See also L<perlmodlib/Pragmatic Modules> and L<perldiag>.
2651356
2661357=end original
2671358
268L<perlmodlib/Pragmatic Modules> と L<perllexwarn> 見てください。
1359L<perlmodlib/Pragmatic Modules> と L<perldiag> 見てください。
1360
1361=begin meta
1362
1363Translate: SHIRAKATA Kentaro <argrath@ub32.org>
1364Status: completed
1365
1366=end meta
2691367
2701368=cut