perllexwarn >
5.10.1
との差分
perllexwarn 5.10.1 と 5.26.1 の差分
1 | 1 | |
2 | 2 | =encoding euc-jp |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | X<warning, lexical> X<warnings> X<warning> | |
6 | 5 | |
7 | 6 | =begin original |
8 | 7 | |
9 | 8 | perllexwarn - Perl Lexical Warnings |
10 | 9 | |
11 | 10 | =end original |
12 | 11 | |
13 | 12 | perllexwarn - Perl のレキシカルな警告 |
14 | 13 | |
15 | 14 | =head1 DESCRIPTION |
16 | 15 | |
17 | 16 | =begin original |
18 | 17 | |
19 | ||
18 | Perl v5.6.0 introduced lexical control over the handling of warnings by | |
20 | to | |
19 | category. The C<warnings> pragma generally replaces the command line flag | |
21 | ||
20 | B<-w>. Documentation on the use of lexical warnings, once partly found in | |
22 | ||
21 | this document, is now found in the L<warnings> documentation. | |
23 | 22 | |
24 | 23 | =end original |
25 | 24 | |
26 | ||
25 | Perl v5.6.0 ではカテゴリ単位で警告の扱いをレキシカルに | |
27 | ||
26 | 制御できるようになりました。 | |
28 | ||
27 | C<warnings> プラグマは一般的にコマンドラインオプション B<-w> を置き換えます。 | |
29 | ||
28 | レキシカル警告の使用に関する文書は、一時期は部分的にこの文書にありましたが、 | |
29 | 今は L<warnings> 文書にあります。 | |
30 | 30 | |
31 | =begin original | |
32 | ||
33 | This pragma works just like the C<strict> pragma. | |
34 | This means that the scope of the warning pragma is limited to the | |
35 | enclosing block. It also means that the pragma setting will not | |
36 | leak across files (via C<use>, C<require> or C<do>). This allows | |
37 | authors to independently define the degree of warning checks that will | |
38 | be applied to their module. | |
39 | ||
40 | =end original | |
41 | ||
42 | このプラグマはちょうど C<strict> プラグマと同様に動作します。 | |
43 | つまり、警告プラグマのスコープは閉じたブロック内に限定されます。 | |
44 | また、プラグマ設定は (C<use>, C<require>, C<do> を通して)ファイルを超えて | |
45 | 漏洩することはありません。 | |
46 | これにより、モジュール作者は警告チェックの度合いを独立に | |
47 | 設定できるようになります。 | |
48 | ||
49 | =begin original | |
50 | ||
51 | By default, optional warnings are disabled, so any legacy code that | |
52 | doesn't attempt to control the warnings will work unchanged. | |
53 | ||
54 | =end original | |
55 | ||
56 | デフォルトでは、オプションの警告は無効なので、警告を制御しようとしない | |
57 | レガシーコードは変更なしで動作します。 | |
58 | ||
59 | =begin original | |
60 | ||
61 | All warnings are enabled in a block by either of these: | |
62 | ||
63 | =end original | |
64 | ||
65 | あるブロック内で全ての警告を有効にするには以下のどちらかのようにします: | |
66 | ||
67 | use warnings; | |
68 | use warnings 'all'; | |
69 | ||
70 | =begin original | |
71 | ||
72 | Similarly all warnings are disabled in a block by either of these: | |
73 | ||
74 | =end original | |
75 | ||
76 | 同様に、あるブロック内で全ての警告を無効にするには以下のどちらかのように | |
77 | します: | |
78 | ||
79 | no warnings; | |
80 | no warnings 'all'; | |
81 | ||
82 | =begin original | |
83 | ||
84 | For example, consider the code below: | |
85 | ||
86 | =end original | |
87 | ||
88 | 例えば、以下のコードを考えます: | |
89 | ||
90 | use warnings; | |
91 | my @a; | |
92 | { | |
93 | no warnings; | |
94 | my $b = @a[0]; | |
95 | } | |
96 | my $c = @a[0]; | |
97 | ||
98 | =begin original | |
99 | ||
100 | The code in the enclosing block has warnings enabled, but the inner | |
101 | block has them disabled. In this case that means the assignment to the | |
102 | scalar C<$c> will trip the C<"Scalar value @a[0] better written as $a[0]"> | |
103 | warning, but the assignment to the scalar C<$b> will not. | |
104 | ||
105 | =end original | |
106 | ||
107 | 外側のブロックでは警告は有効ですが、内側のブロックでは無効です。 | |
108 | この場合、スカラ C<$c> への代入では | |
109 | C<"Scalar value @a[0] better written as $a[0]"> 警告が出ますが、 | |
110 | スカラ C<$b> への代入では出ません。 | |
111 | ||
112 | =head2 Default Warnings and Optional Warnings | |
113 | ||
114 | (デフォルトの警告とオプションの警告) | |
115 | ||
116 | =begin original | |
117 | ||
118 | Before the introduction of lexical warnings, Perl had two classes of | |
119 | warnings: mandatory and optional. | |
120 | ||
121 | =end original | |
122 | ||
123 | レキシカル警告の説明の前に、Perl は二つの警告クラスがあります: | |
124 | 強制的(mandatory)とオプション(optional)です。 | |
125 | ||
126 | =begin original | |
127 | ||
128 | As its name suggests, if your code tripped a mandatory warning, you | |
129 | would get a warning whether you wanted it or not. | |
130 | For example, the code below would always produce an C<"isn't numeric"> | |
131 | warning about the "2:". | |
132 | ||
133 | =end original | |
134 | ||
135 | 名前が示しているように、コードが強制的な警告に引っかかると、望むと | |
136 | 望まな意図にかかわらず警告を出力します。 | |
137 | 例えば、以下のコードは "2:" の部分に対して常に C<"isn't numeric"> 警告を | |
138 | 出力します。 | |
139 | ||
140 | my $a = "2:" + 3; | |
141 | ||
142 | =begin original | |
143 | ||
144 | With the introduction of lexical warnings, mandatory warnings now become | |
145 | I<default> warnings. The difference is that although the previously | |
146 | mandatory warnings are still enabled by default, they can then be | |
147 | subsequently enabled or disabled with the lexical warning pragma. For | |
148 | example, in the code below, an C<"isn't numeric"> warning will only | |
149 | be reported for the C<$a> variable. | |
150 | ||
151 | =end original | |
152 | ||
153 | レキシカルな警告の導入によって、強制的な警告は I<デフォルトの> 警告と | |
154 | なりました。 | |
155 | 違いは、以前の強制的な警告は今でもデフォルトで有効ですが、引き続く | |
156 | レキシカルな警告プラグマで有効/無効にに出来ることです。 | |
157 | 例えば、以下のコードでは、C<"isn't numeric"> 警告は C<$a> 変数に対してだけ | |
158 | 報告されます。 | |
159 | ||
160 | my $a = "2:" + 3; | |
161 | no warnings; | |
162 | my $b = "2:" + 3; | |
163 | ||
164 | =begin original | |
165 | ||
166 | Note that neither the B<-w> flag or the C<$^W> can be used to | |
167 | disable/enable default warnings. They are still mandatory in this case. | |
168 | ||
169 | =end original | |
170 | ||
171 | B<-w> オプションや C<$^W> はデフォルトの警告を無効/有効にするのには | |
172 | 使えないことに注意してください。 | |
173 | この場合は強制的なままです。 | |
174 | ||
175 | =head2 What's wrong with B<-w> and C<$^W> | |
176 | ||
177 | (B<-w> や C<$^W> の何が悪いの?) | |
178 | ||
179 | =begin original | |
180 | ||
181 | Although very useful, the big problem with using B<-w> on the command | |
182 | line to enable warnings is that it is all or nothing. Take the typical | |
183 | scenario when you are writing a Perl program. Parts of the code you | |
184 | will write yourself, but it's very likely that you will make use of | |
185 | pre-written Perl modules. If you use the B<-w> flag in this case, you | |
186 | end up enabling warnings in pieces of code that you haven't written. | |
187 | ||
188 | =end original | |
189 | ||
190 | 警告を有効にするのにコマンドラインで B<-w> を使うというのはとても | |
191 | 便利ですが、オールオアナッシングであるという問題があります。 | |
192 | Perl のプログラムを書いているときのよくある状況を考えます。 | |
193 | コードの一部はあなた自身が書きますが、かなり確実に既に書かれている | |
194 | Perl モジュールを利用します。 | |
195 | このような場合に B<-w> フラグを使うと、あなたが書いていないコードに | |
196 | 対しても警告を有効にすることになります。 | |
197 | ||
198 | =begin original | |
199 | ||
200 | Similarly, using C<$^W> to either disable or enable blocks of code is | |
201 | fundamentally flawed. For a start, say you want to disable warnings in | |
202 | a block of code. You might expect this to be enough to do the trick: | |
203 | ||
204 | =end original | |
205 | ||
206 | 同様に、コードブロックで有効または無効にするために C<$^W> を使うことにも | |
207 | 本質的な欠点があります。 | |
208 | まず、コードブロックで警告を無効にしたいとします。 | |
209 | 以下のようにすれば十分だと考えるかもしれません: | |
210 | ||
211 | { | |
212 | local ($^W) = 0; | |
213 | my $a =+ 2; | |
214 | my $b; chop $b; | |
215 | } | |
216 | ||
217 | =begin original | |
218 | ||
219 | When this code is run with the B<-w> flag, a warning will be produced | |
220 | for the C<$a> line -- C<"Reversed += operator">. | |
221 | ||
222 | =end original | |
223 | ||
224 | このコードが B<-w> フラグ付きで実行されると、C<$a> の行で警告が | |
225 | 出ます -- C<"Reversed += operator">。 | |
226 | ||
227 | =begin original | |
228 | ||
229 | The problem is that Perl has both compile-time and run-time warnings. To | |
230 | disable compile-time warnings you need to rewrite the code like this: | |
231 | ||
232 | =end original | |
233 | ||
234 | 問題は、Perl にはコンパイル時警告と実行時警告があると言うことです。 | |
235 | コンパイル時警告を無効にするには、以下のようにコードを書き直す必要が | |
236 | あります: | |
237 | ||
238 | { | |
239 | BEGIN { $^W = 0 } | |
240 | my $a =+ 2; | |
241 | my $b; chop $b; | |
242 | } | |
243 | ||
244 | =begin original | |
245 | ||
246 | The other big problem with C<$^W> is the way you can inadvertently | |
247 | change the warning setting in unexpected places in your code. For example, | |
248 | when the code below is run (without the B<-w> flag), the second call | |
249 | to C<doit> will trip a C<"Use of uninitialized value"> warning, whereas | |
250 | the first will not. | |
251 | ||
252 | =end original | |
253 | ||
254 | C<$^W> に関するもう一つの問題は、コード中の予想外の位置の設定で不用意に | |
255 | 警告設定が変わるということです。 | |
256 | 例えば、以下のコードが(B<-w> フラグなしで)実行されると、C<doit> の | |
257 | 2 回目の呼び出しで C<"Use of uninitialized value"> 警告が出ますが、 | |
258 | 1 回目では出ません。 | |
259 | ||
260 | sub doit | |
261 | { | |
262 | my $b; chop $b; | |
263 | } | |
264 | ||
265 | doit(); | |
266 | ||
267 | { | |
268 | local ($^W) = 1; | |
269 | doit() | |
270 | } | |
271 | ||
272 | =begin original | |
273 | ||
274 | This is a side-effect of C<$^W> being dynamically scoped. | |
275 | ||
276 | =end original | |
277 | ||
278 | これは C<$^W> が動的スコープを持つことの副作用です。 | |
279 | ||
280 | =begin original | |
281 | ||
282 | Lexical warnings get around these limitations by allowing finer control | |
283 | over where warnings can or can't be tripped. | |
284 | ||
285 | =end original | |
286 | ||
287 | レキシカルな警告は、どこで警告に引っかかるか引っかからないかに関して | |
288 | より精度の高い制御をすることで、これらの制限を回避します。 | |
289 | ||
290 | =head2 Controlling Warnings from the Command Line | |
291 | ||
292 | (コマンドラインから警告を制御する) | |
293 | ||
294 | =begin original | |
295 | ||
296 | There are three Command Line flags that can be used to control when | |
297 | warnings are (or aren't) produced: | |
298 | ||
299 | =end original | |
300 | ||
301 | いつ警告が発生する(あるいは発生しない)かを制御するために使われる | |
302 | 三つのコマンドラインフラグがあります: | |
303 | ||
304 | =over 5 | |
305 | ||
306 | =item B<-w> | |
307 | X<-w> | |
308 | ||
309 | =begin original | |
310 | ||
311 | This is the existing flag. If the lexical warnings pragma is B<not> | |
312 | used in any of you code, or any of the modules that you use, this flag | |
313 | will enable warnings everywhere. See L<Backward Compatibility> for | |
314 | details of how this flag interacts with lexical warnings. | |
315 | ||
316 | =end original | |
317 | ||
318 | これは既存のフラグです。 | |
319 | レキシカル警告プラグマがあなたのコードやあなたが使っているモジュールの | |
320 | どこでも B<使われていない> なら、このフラグは全ての場所で警告を | |
321 | 有効にします。 | |
322 | このフラグがレキシカル警告とどのように相互作用するかに関する詳細については | |
323 | L<Backward Compatibility> を参照してください。 | |
324 | ||
325 | =item B<-W> | |
326 | X<-W> | |
327 | ||
328 | =begin original | |
329 | ||
330 | If the B<-W> flag is used on the command line, it will enable all warnings | |
331 | throughout the program regardless of whether warnings were disabled | |
332 | locally using C<no warnings> or C<$^W =0>. This includes all files that get | |
333 | included via C<use>, C<require> or C<do>. | |
334 | Think of it as the Perl equivalent of the "lint" command. | |
335 | ||
336 | =end original | |
337 | ||
338 | コマンドラインで B<-W> フラグが使われると、プログラム中で | |
339 | C<no warnings> や C<$^W =0> を使って警告を無効にしていても無視して、全ての | |
340 | 警告を有効にします。 | |
341 | これは C<use>, C<require>, C<do> 経由で読み込まれる全てのファイルにも | |
342 | 適用されます。 | |
343 | Perl 用の "lint" コマンドの等価物と考えられます。 | |
344 | ||
345 | =item B<-X> | |
346 | X<-X> | |
347 | ||
348 | =begin original | |
349 | ||
350 | Does the exact opposite to the B<-W> flag, i.e. it disables all warnings. | |
351 | ||
352 | =end original | |
353 | ||
354 | 正確に B<-W> フラグの逆を行います; つまり、全ての警告を無効にします。 | |
355 | ||
356 | =back | |
357 | ||
358 | =head2 Backward Compatibility | |
359 | ||
360 | (後方互換性) | |
361 | ||
362 | =begin original | |
363 | ||
364 | If you are used with working with a version of Perl prior to the | |
365 | introduction of lexically scoped warnings, or have code that uses both | |
366 | lexical warnings and C<$^W>, this section will describe how they interact. | |
367 | ||
368 | =end original | |
369 | ||
370 | レキシカルスコープ警告が導入される前のバージョンの Perl で動作させていたり、 | |
371 | レキシカル警告と C<$^W> の両方のコードがある場合、この節はこれらが | |
372 | どのように相互作用するかを記述しています。 | |
373 | ||
374 | =begin original | |
375 | ||
376 | How Lexical Warnings interact with B<-w>/C<$^W>: | |
377 | ||
378 | =end original | |
379 | ||
380 | レキシカル警告と B<-w>/C<$^W> の相互作用: | |
381 | ||
382 | =over 5 | |
383 | ||
384 | =item 1. | |
385 | ||
386 | =begin original | |
387 | ||
388 | If none of the three command line flags (B<-w>, B<-W> or B<-X>) that | |
389 | control warnings is used and neither C<$^W> or the C<warnings> pragma | |
390 | are used, then default warnings will be enabled and optional warnings | |
391 | disabled. | |
392 | This means that legacy code that doesn't attempt to control the warnings | |
393 | will work unchanged. | |
394 | ||
395 | =end original | |
396 | ||
397 | 警告を制御する三つのコマンドラインフラグ (B<-w>, B<-W> or B<-X>) の | |
398 | どれも使われておらず、C<$^W> や the C<warnings> プラグマも使われていない | |
399 | 場合、デフォルトの警告が有効になり、オプションの警告は無効になります。 | |
400 | これにより、警告を制御しようとしないレガシーコードは無変更で動作します。 | |
401 | ||
402 | =item 2. | |
403 | ||
404 | =begin original | |
405 | ||
406 | The B<-w> flag just sets the global C<$^W> variable as in 5.005 -- this | |
407 | means that any legacy code that currently relies on manipulating C<$^W> | |
408 | to control warning behavior will still work as is. | |
409 | ||
410 | =end original | |
411 | ||
412 | 5.005 から B<-w> フラグはグローバルな C<$^W> 変数を設定します -- これにより、 | |
413 | 警告の振る舞いを制御するために C<$^W> を操作することに依存している | |
414 | レガシーコードはそのままで動作します。 | |
415 | ||
416 | =item 3. | |
417 | ||
418 | =begin original | |
419 | ||
420 | Apart from now being a boolean, the C<$^W> variable operates in exactly | |
421 | the same horrible uncontrolled global way, except that it cannot | |
422 | disable/enable default warnings. | |
423 | ||
424 | =end original | |
425 | ||
426 | 真偽値になったことは別として、C<$^W> 変数は正確に同じ恐ろしく | |
427 | 制御不能なグローバルな方法で操作しますが、デフォルトの警告を有効化/ | |
428 | 無効化することは出来ません。 | |
429 | ||
430 | =item 4. | |
431 | ||
432 | =begin original | |
433 | ||
434 | If a piece of code is under the control of the C<warnings> pragma, | |
435 | both the C<$^W> variable and the B<-w> flag will be ignored for the | |
436 | scope of the lexical warning. | |
437 | ||
438 | =end original | |
439 | ||
440 | コード片が C<warnings> プラグマの制御下にある場合、C<$^W> 変数と | |
441 | B<-w> フラグの両方はレキシカル警告のスコープで無視されます。 | |
442 | ||
443 | =item 5. | |
444 | ||
445 | =begin original | |
446 | ||
447 | The only way to override a lexical warnings setting is with the B<-W> | |
448 | or B<-X> command line flags. | |
449 | ||
450 | =end original | |
451 | ||
452 | レキシカル警告設定を上書きする唯一の方法は B<-W> または B<-X> | |
453 | コマンドラインフラグを使うことです。 | |
454 | ||
455 | =back | |
456 | ||
457 | =begin original | |
458 | ||
459 | The combined effect of 3 & 4 is that it will allow code which uses | |
460 | the C<warnings> pragma to control the warning behavior of $^W-type | |
461 | code (using a C<local $^W=0>) if it really wants to, but not vice-versa. | |
462 | ||
463 | =end original | |
464 | ||
465 | 3 & 4 の組み合わせの効果により、本当に警告したいときに $^W 型のコードの | |
466 | 警告の振る舞いを (C<local $^W=0> を使って) 制御するために | |
467 | C<warnings> プラグマを使えますが、逆はできません。 | |
468 | ||
469 | =head2 Category Hierarchy | |
470 | X<warning, categories> | |
471 | ||
472 | (カテゴリ階層) | |
473 | ||
474 | =begin original | |
475 | ||
476 | A hierarchy of "categories" have been defined to allow groups of warnings | |
477 | to be enabled/disabled in isolation. | |
478 | ||
479 | =end original | |
480 | ||
481 | 「カテゴリ」の階層は、警告のグループを分離して警告を有効/無効にできるように | |
482 | するために定義されています。 | |
483 | ||
484 | =begin original | |
485 | ||
486 | The current hierarchy is: | |
487 | ||
488 | =end original | |
489 | ||
490 | 現在の階層は: | |
491 | ||
492 | all -+ | |
493 | | | |
494 | +- closure | |
495 | | | |
496 | +- deprecated | |
497 | | | |
498 | +- exiting | |
499 | | | |
500 | +- glob | |
501 | | | |
502 | +- io -----------+ | |
503 | | | | |
504 | | +- closed | |
505 | | | | |
506 | | +- exec | |
507 | | | | |
508 | | +- layer | |
509 | | | | |
510 | | +- newline | |
511 | | | | |
512 | | +- pipe | |
513 | | | | |
514 | | +- unopened | |
515 | | | |
516 | +- misc | |
517 | | | |
518 | +- numeric | |
519 | | | |
520 | +- once | |
521 | | | |
522 | +- overflow | |
523 | | | |
524 | +- pack | |
525 | | | |
526 | +- portable | |
527 | | | |
528 | +- recursion | |
529 | | | |
530 | +- redefine | |
531 | | | |
532 | +- regexp | |
533 | | | |
534 | +- severe -------+ | |
535 | | | | |
536 | | +- debugging | |
537 | | | | |
538 | | +- inplace | |
539 | | | | |
540 | | +- internal | |
541 | | | | |
542 | | +- malloc | |
543 | | | |
544 | +- signal | |
545 | | | |
546 | +- substr | |
547 | | | |
548 | +- syntax -------+ | |
549 | | | | |
550 | | +- ambiguous | |
551 | | | | |
552 | | +- bareword | |
553 | | | | |
554 | | +- digit | |
555 | | | | |
556 | | +- parenthesis | |
557 | | | | |
558 | | +- precedence | |
559 | | | | |
560 | | +- printf | |
561 | | | | |
562 | | +- prototype | |
563 | | | | |
564 | | +- qw | |
565 | | | | |
566 | | +- reserved | |
567 | | | | |
568 | | +- semicolon | |
569 | | | |
570 | +- taint | |
571 | | | |
572 | +- threads | |
573 | | | |
574 | +- uninitialized | |
575 | | | |
576 | +- unpack | |
577 | | | |
578 | +- untie | |
579 | | | |
580 | +- utf8 | |
581 | | | |
582 | +- void | |
583 | ||
584 | =begin original | |
585 | ||
586 | Just like the "strict" pragma any of these categories can be combined | |
587 | ||
588 | =end original | |
589 | ||
590 | "strict" プラグマと同様、これらのカテゴリは組み合わせることができます | |
591 | ||
592 | use warnings qw(void redefine); | |
593 | no warnings qw(io syntax untie); | |
594 | ||
595 | =begin original | |
596 | ||
597 | Also like the "strict" pragma, if there is more than one instance of the | |
598 | C<warnings> pragma in a given scope the cumulative effect is additive. | |
599 | ||
600 | =end original | |
601 | ||
602 | これも "strict" プラグマと同様、現在のスコープに複数の | |
603 | C<warnings> プラグマの実体があるときは、効果は加算されます。 | |
604 | ||
605 | use warnings qw(void); # only "void" warnings enabled | |
606 | ... | |
607 | use warnings qw(io); # only "void" & "io" warnings enabled | |
608 | ... | |
609 | no warnings qw(void); # only "io" warnings enabled | |
610 | ||
611 | =begin original | |
612 | ||
613 | To determine which category a specific warning has been assigned to see | |
614 | L<perldiag>. | |
615 | ||
616 | =end original | |
617 | ||
618 | ある特定の警告がどのカテゴリに割り当てられているかを知るには | |
619 | L<perldiag> を参照してください。 | |
620 | ||
621 | =begin original | |
622 | ||
623 | Note: In Perl 5.6.1, the lexical warnings category "deprecated" was a | |
624 | sub-category of the "syntax" category. It is now a top-level category | |
625 | in its own right. | |
626 | ||
627 | =end original | |
628 | ||
629 | 注意: Perl 5.6.1 では、レキシカル警告カテゴリ "deprecated" は "syntax" | |
630 | カテゴリの副カテゴリでした。 | |
631 | 今ではそれ自体でトップレベルカテゴリです。 | |
632 | ||
633 | =head2 Fatal Warnings | |
634 | X<warning, fatal> | |
635 | ||
636 | (致命的警告) | |
637 | ||
638 | =begin original | |
639 | ||
640 | The presence of the word "FATAL" in the category list will escalate any | |
641 | warnings detected from the categories specified in the lexical scope | |
642 | into fatal errors. In the code below, the use of C<time>, C<length> | |
643 | and C<join> can all produce a C<"Useless use of xxx in void context"> | |
644 | warning. | |
645 | ||
646 | =end original | |
647 | ||
648 | カテゴリ一覧中に "FATAL" の文字があると、レキシカルスコープで | |
649 | 指定されたカテゴリで検出された全ての警告を致命的エラーに昇格させます。 | |
650 | 以下のコードでは、C<time>, C<length>, C<join> の使用は全て | |
651 | C<"Useless use of xxx in void context"> 警告を出力します。 | |
652 | ||
653 | use warnings; | |
654 | ||
655 | time; | |
656 | ||
657 | { | |
658 | use warnings FATAL => qw(void); | |
659 | length "abc"; | |
660 | } | |
661 | ||
662 | join "", 1,2,3; | |
663 | ||
664 | print "done\n"; | |
665 | ||
666 | =begin original | |
667 | ||
668 | When run it produces this output | |
669 | ||
670 | =end original | |
671 | ||
672 | 実行すると、以下の出力を生成します | |
673 | ||
674 | Useless use of time in void context at fatal line 3. | |
675 | Useless use of length in void context at fatal line 7. | |
676 | ||
677 | =begin original | |
678 | ||
679 | The scope where C<length> is used has escalated the C<void> warnings | |
680 | category into a fatal error, so the program terminates immediately it | |
681 | encounters the warning. | |
682 | ||
683 | =end original | |
684 | ||
685 | C<length> が使われているスコープでは C<void> 警告カテゴリを致命的エラーに | |
686 | 昇格させるので、この警告に出会うとプログラムは直ちに終了します。 | |
687 | ||
688 | =begin original | |
689 | ||
690 | To explicitly turn off a "FATAL" warning you just disable the warning | |
691 | it is associated with. So, for example, to disable the "void" warning | |
692 | in the example above, either of these will do the trick: | |
693 | ||
694 | =end original | |
695 | ||
696 | 明示的に "FATAL" 警告をオフにするには、単に関連する警告を無効にします。 | |
697 | それで、例えば、上述の例で "void" 警告を無効にするには、以下の二つの | |
698 | 技のどちらかを使います: | |
699 | ||
700 | no warnings qw(void); | |
701 | no warnings FATAL => qw(void); | |
702 | ||
703 | =begin original | |
704 | ||
705 | If you want to downgrade a warning that has been escalated into a fatal | |
706 | error back to a normal warning, you can use the "NONFATAL" keyword. For | |
707 | example, the code below will promote all warnings into fatal errors, | |
708 | except for those in the "syntax" category. | |
709 | ||
710 | =end original | |
711 | ||
712 | 致命的エラーに昇格した警告を通常の警告に降格させたい場合、 | |
713 | "NONFATAL" きーわーどが使えます。 | |
714 | 例えば、以下のコードは "syntax" カテゴリ以外の全ての警告を致命的エラーに | |
715 | 昇格させます。 | |
716 | ||
717 | use warnings FATAL => 'all', NONFATAL => 'syntax'; | |
718 | ||
719 | =head2 Reporting Warnings from a Module | |
720 | X<warning, reporting> X<warning, registering> | |
721 | ||
722 | (モジュールから警告を報告する) | |
723 | ||
724 | =begin original | |
725 | ||
726 | The C<warnings> pragma provides a number of functions that are useful for | |
727 | module authors. These are used when you want to report a module-specific | |
728 | warning to a calling module has enabled warnings via the C<warnings> | |
729 | pragma. | |
730 | ||
731 | =end original | |
732 | ||
733 | C<warnings> プラグマはモジュール作者にとって有用ないくつかの関数を | |
734 | 提供します。 | |
735 | C<warnings> プラグマ経由で有効になったモジュール固有の警告を呼び出し元に | |
736 | 報告するときに使われます。 | |
737 | ||
738 | =begin original | |
739 | ||
740 | Consider the module C<MyMod::Abc> below. | |
741 | ||
742 | =end original | |
743 | ||
744 | 以下の C<MyMod::Abc> モジュールを考えます。 | |
745 | ||
746 | package MyMod::Abc; | |
747 | ||
748 | use warnings::register; | |
749 | ||
750 | sub open { | |
751 | my $path = shift; | |
752 | if ($path !~ m#^/#) { | |
753 | warnings::warn("changing relative path to /var/abc") | |
754 | if warnings::enabled(); | |
755 | $path = "/var/abc/$path"; | |
756 | } | |
757 | } | |
758 | ||
759 | 1; | |
760 | ||
761 | =begin original | |
762 | ||
763 | The call to C<warnings::register> will create a new warnings category | |
764 | called "MyMod::abc", i.e. the new category name matches the current | |
765 | package name. The C<open> function in the module will display a warning | |
766 | message if it gets given a relative path as a parameter. This warnings | |
767 | will only be displayed if the code that uses C<MyMod::Abc> has actually | |
768 | enabled them with the C<warnings> pragma like below. | |
769 | ||
770 | =end original | |
771 | ||
772 | C<warnings::register> の呼び出しにより、"MyMod::abc" という名前の新しい | |
773 | 警告カテゴリを作成します; つまり、新しいカテゴリ名は現在のパッケージ名に | |
774 | 一致します。 | |
775 | このモジュールの C<open> 関数は、引数として相対パスが与えられると | |
776 | 警告メッセージを出力します。 | |
777 | この警告は、C<MyMod::Abc> を使うコードが 以下のようにして | |
778 | C<warnings> によって有効にされた場合にのみ出力されます。 | |
779 | ||
780 | use MyMod::Abc; | |
781 | use warnings 'MyMod::Abc'; | |
782 | ... | |
783 | abc::open("../fred.txt"); | |
784 | ||
785 | =begin original | |
786 | ||
787 | It is also possible to test whether the pre-defined warnings categories are | |
788 | set in the calling module with the C<warnings::enabled> function. Consider | |
789 | this snippet of code: | |
790 | ||
791 | =end original | |
792 | ||
793 | また、C<warnings::enabled> 関数を使って、既に定義されているカテゴリが | |
794 | 呼び出しモジュールで設定されているかどうかをテストすることも可能です。 | |
795 | 以下のコード片を考えます: | |
796 | ||
797 | package MyMod::Abc; | |
798 | ||
799 | sub open { | |
800 | warnings::warnif("deprecated", | |
801 | "open is deprecated, use new instead"); | |
802 | new(@_); | |
803 | } | |
804 | ||
805 | sub new | |
806 | ... | |
807 | 1; | |
808 | ||
809 | =begin original | |
810 | ||
811 | The function C<open> has been deprecated, so code has been included to | |
812 | display a warning message whenever the calling module has (at least) the | |
813 | "deprecated" warnings category enabled. Something like this, say. | |
814 | ||
815 | =end original | |
816 | ||
817 | C<open> 関数は非推奨なので、呼び出しモジュールで (少なくとも) | |
818 | "deprecated" 警告カテゴリが有効のとき警告を出力するコードが含まれています。 | |
819 | つまりこんな感じです。 | |
820 | ||
821 | use warnings 'deprecated'; | |
822 | use MyMod::Abc; | |
823 | ... | |
824 | MyMod::Abc::open($filename); | |
825 | ||
826 | =begin original | |
827 | ||
828 | Either the C<warnings::warn> or C<warnings::warnif> function should be | |
829 | used to actually display the warnings message. This is because they can | |
830 | make use of the feature that allows warnings to be escalated into fatal | |
831 | errors. So in this case | |
832 | ||
833 | =end original | |
834 | ||
835 | 実際に警告メッセージを出力するには、C<warnings::warn> 関数と | |
836 | C<warnings::warnif> 関数のどちらかを使うべきです。 | |
837 | これは、警告を致命的エラーに昇格させる機能を使えるようにするためです。 | |
838 | それで、このような場合: | |
839 | ||
840 | use MyMod::Abc; | |
841 | use warnings FATAL => 'MyMod::Abc'; | |
842 | ... | |
843 | MyMod::Abc::open('../fred.txt'); | |
844 | ||
845 | =begin original | |
846 | ||
847 | the C<warnings::warnif> function will detect this and die after | |
848 | displaying the warning message. | |
849 | ||
850 | =end original | |
851 | ||
852 | C<warnings::warnif> 関数はこれを検出して、警告を出力した後 die します。 | |
853 | ||
854 | =begin original | |
855 | ||
856 | The three warnings functions, C<warnings::warn>, C<warnings::warnif> | |
857 | and C<warnings::enabled> can optionally take an object reference in place | |
858 | of a category name. In this case the functions will use the class name | |
859 | of the object as the warnings category. | |
860 | ||
861 | =end original | |
862 | ||
863 | 三つの警告関数 C<warnings::warn>, C<warnings::warnif>, | |
864 | C<warnings::enabled> は、オプションとしてカテゴリ名の代わりにオブジェクトの | |
865 | リファレンスをとることができます。 | |
866 | この場合関数は警告カテゴリとしてオブジェクトのクラス名を使います。 | |
867 | ||
868 | =begin original | |
869 | ||
870 | Consider this example: | |
871 | ||
872 | =end original | |
873 | ||
874 | この例を考えます: | |
875 | ||
876 | package Original; | |
877 | ||
878 | no warnings; | |
879 | use warnings::register; | |
880 | ||
881 | sub new | |
882 | { | |
883 | my $class = shift; | |
884 | bless [], $class; | |
885 | } | |
886 | ||
887 | sub check | |
888 | { | |
889 | my $self = shift; | |
890 | my $value = shift; | |
891 | ||
892 | if ($value % 2 && warnings::enabled($self)) | |
893 | { warnings::warn($self, "Odd numbers are unsafe") } | |
894 | } | |
895 | ||
896 | sub doit | |
897 | { | |
898 | my $self = shift; | |
899 | my $value = shift; | |
900 | $self->check($value); | |
901 | # ... | |
902 | } | |
903 | ||
904 | 1; | |
905 | ||
906 | package Derived; | |
907 | ||
908 | use warnings::register; | |
909 | use Original; | |
910 | our @ISA = qw( Original ); | |
911 | sub new | |
912 | { | |
913 | my $class = shift; | |
914 | bless [], $class; | |
915 | } | |
916 | ||
917 | ||
918 | 1; | |
919 | ||
920 | =begin original | |
921 | ||
922 | The code below makes use of both modules, but it only enables warnings from | |
923 | C<Derived>. | |
924 | ||
925 | =end original | |
926 | ||
927 | 以下のコードは両方のモジュールを使っていますが、C<Derived> からの警告だけを | |
928 | 有効にしています。 | |
929 | ||
930 | use Original; | |
931 | use Derived; | |
932 | use warnings 'Derived'; | |
933 | my $a = Original->new(); | |
934 | $a->doit(1); | |
935 | my $b = Derived->new(); | |
936 | $a->doit(1); | |
937 | ||
938 | =begin original | |
939 | ||
940 | When this code is run only the C<Derived> object, C<$b>, will generate | |
941 | a warning. | |
942 | ||
943 | =end original | |
944 | ||
945 | このコードが C<Derived> オブジェクトからのみ実行されているとき、 | |
946 | C<$b> は警告を出力します。 | |
947 | ||
948 | Odd numbers are unsafe at main.pl line 7 | |
949 | ||
950 | =begin original | |
951 | ||
952 | Notice also that the warning is reported at the line where the object is first | |
953 | used. | |
954 | ||
955 | =end original | |
956 | ||
957 | オブジェクトが最初に使われた行で警告が報告されることにも注意してください。 | |
958 | ||
959 | =head1 SEE ALSO | |
960 | ||
961 | =begin original | |
962 | ||
963 | L<warnings>, L<perldiag>. | |
964 | ||
965 | =end original | |
966 | ||
967 | L<warnings>, L<perldiag> | |
968 | ||
969 | =head1 AUTHOR | |
970 | ||
971 | Paul Marquess | |
972 | ||
973 | 31 | =begin meta |
974 | 32 | |
975 | Translate: Kentaro | |
33 | Translate: SHIRAKATA Kentaro <argrath@ub32.org> | |
976 | 34 | Status: completed |
977 | 35 | |
978 | 36 | =end meta |