perl-5.38.0
warn LIST

Emits a warning, usually by printing it to STDERR. warn interprets its operand LIST in the same way as die, but is slightly different in what it defaults to when LIST is empty or makes an empty string. If it is empty and $@ already contains an exception value then that value is used after appending "\t...caught". If it is empty and $@ is also empty then the string "Warning: Something's wrong" is used.

(通常は STDERR に表示することで) 警告を出力します。 warn はそのオペランド LIST を die と同様に解釈しますが、 LIST が空や空文字列を作る時に何をデフォルトとするかが少し異なります。 それが空かつ、$@ に既に 例外値が入っている場合、"\t...caught" を追加した値が 用いられます。 もしこれが空で $@ も空の場合は、"Warning: Something's wrong" という 文字列が使われます。

By default, the exception derived from the operand LIST is stringified and printed to STDERR. This behaviour can be altered by installing a $SIG{__WARN__} handler. If there is such a handler then no message is automatically printed; it is the handler's responsibility to deal with the exception as it sees fit (like, for instance, converting it into a die). Most handlers must therefore arrange to actually display the warnings that they are not prepared to deal with, by calling warn again in the handler. Note that this is quite safe and will not produce an endless loop, since __WARN__ hooks are not called from inside one.

デフォルトでは、LIST オペランドから生成された例外は 文字列化されて STDERR に表示されます。 この振る舞いは、$SIG{__WARN__} ハンドラを設定することで 置き換えられます。 このようなハンドラが設定されている場合は何の メッセージも自動的には表示されません; 例外をどう扱うか(例えば die に変換するか)はハンドラの 責任ということです。 従ってほとんどのハンドラは、扱おうと準備していない警告を表示するために、 ハンドラの中で warn を再び呼び出します。 __WARN__ フックはハンドラ内では呼び出されないので、これは十分安全で、 無限ループを引き起こすことはないということに注意してください。

You will find this behavior is slightly different from that of $SIG{__DIE__} handlers (which don't suppress the error text, but can instead call die again to change it).

この振る舞いは $SIG{__DIE__} ハンドラ(エラーテキストは 削除しませんが、代わりに die をもう一度呼び出すことで 変更できます)とは少し違うことに気付くことでしょう。

Using a __WARN__ handler provides a powerful way to silence all warnings (even the so-called mandatory ones). An example:

__WARN__ ハンドラを使うと、(いわゆる必須のものを含む)全ての 警告を黙らせる強力な手段となります。 例:

    # wipe out *all* compile-time warnings
    BEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } }
    my $foo = 10;
    my $foo = 20;          # no warning about duplicate my $foo,
                           # but hey, you asked for it!
    # no compile-time or run-time warnings before here
    $DOWARN = 1;

    # run-time warnings enabled after here
    warn "\$foo is alive and $foo!";     # does show up

See perlvar for details on setting %SIG entries and for more examples. See the Carp module for other kinds of warnings using its carp and cluck functions.

%SIG エントリのセットに関する詳細とさらなる例に関しては perlvar を参照してください。 carp 関数と cluck 関数を用いた警告の方法に関しては Carp モジュールを参照してください。