perl-5.38.0
die LIST

die raises an exception. Inside an eval the exception is stuffed into $@ and the eval is terminated with the undefined value. If the exception is outside of all enclosing evals, then the uncaught exception is printed to STDERR and perl exits with an exit code indicating failure. If you need to exit the process with a specific exit code, see exit.

die は例外を発生させます。 eval の中で使用すると、例外が $@ に入り、eval は 未定義値を返して終了します。 例外が全ての eval の外側の場合は、捕捉されなかった例外は STDERR に表示され、perl は失敗を示す終了コードで終了します。 特定の終了コードでプロセスを終了させる必要がある場合は、 exit を参照してください。

Equivalent examples:

等価な例:

    die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news';
    chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"

Most of the time, die is called with a string to use as the exception. You may either give a single non-reference operand to serve as the exception, or a list of two or more items, which will be stringified and concatenated to make the exception.

ほとんどの場合、die は例外として使うための文字列と共に呼び出されます。 例外として扱われる単一の非リファレンスオペランドか、 例外を作るために文字列化されて連結される、二つ以上のアイテムのリストを 指定することができます。

If the string exception does not end in a newline, the current script line number and input line number (if any) and a newline are appended to it. Note that the "input line number" (also known as "chunk") is subject to whatever notion of "line" happens to be currently in effect, and is also available as the special variable $.. See "$/" in perlvar and "$." in perlvar.

文字列例外が改行で終わっていなければ、その時点のスクリプト名と スクリプトの行番号、(もしあれば) 入力ファイルの行番号と改行文字が それに追加されます。 「入力行番号」("chunk" とも呼ばれます)は「行」という概念が現在有効であると 仮定しています; また特殊変数 $. でも利用可能です。 "$/" in perlvar"$." in perlvar も参照してください。

Hint: sometimes appending ", stopped" to your message will cause it to make better sense when the string "at foo line 123" is appended. Suppose you are running script "canasta".

ヒント: メッセージの最後を ", stopped" のようなもので 終わるようにしておけば、"at foo line 123" のように 追加されて、わかりやすくなります。 "canasta" というスクリプトを実行しているとします。

    die "/etc/games is no good";
    die "/etc/games is no good, stopped";

produce, respectively

これは、それぞれ以下のように表示します。

    /etc/games is no good at canasta line 123.
    /etc/games is no good, stopped at canasta line 123.

If LIST was empty or made an empty string, and $@ already contains an exception value (typically from a previous eval), then that value is reused after appending "\t...propagated". This is useful for propagating exceptions:

出力が空か空文字列を作り、$@ が (典型的には前回の eval で) 既に例外値を持っている場合、 値は "\t...propagated" を追加した後再利用されます。 これは例外を伝播させる場合に有効です:

    eval { ... };
    die unless $@ =~ /Expected exception/;

If LIST was empty or made an empty string, and $@ contains an object reference that has a PROPAGATE method, that method will be called with additional file and line number parameters. The return value replaces the value in $@; i.e., as if $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; were called.

出力が空か空文字列を作り、$@PROPAGATE メソッドを 含むオブジェクトへのリファレンスを含む場合、 このメソッドが追加ファイルと行番号を引数として呼び出されます。 返り値は $@ の値を置き換えます; つまり、$@ = eval { $@->PROPAGATE(__FILE__, __LINE__) }; が 呼び出されたかのようになります。

If LIST was empty or made an empty string, and $@ is also empty, then the string "Died" is used.

LIST が空か空文字列を作り、$@ も空の場合、 "Died" が使われます。

You can also call die with a reference argument, and if this is trapped within an eval, $@ contains that reference. This permits more elaborate exception handling using objects that maintain arbitrary state about the exception. Such a scheme is sometimes preferable to matching particular string values of $@ with regular expressions.

die はリファレンス引数と共に呼び出すこともでき、これが eval 内部でトラップされた場合、$@ は そのリファレンスを持ちます。 これは、例外の性質について任意の状態を管理するオブジェクトを使った より複雑な例外処理の実装を可能にします。 このようなスキームは $@ の特定の文字列値を正規表現を使って マッチングするときに時々好まれます。

Because Perl stringifies uncaught exception messages before display, you'll probably want to overload stringification operations on exception objects. See overload for details about that. The stringified message should be non-empty, and should end in a newline, in order to fit in with the treatment of string exceptions. Also, because an exception object reference cannot be stringified without destroying it, Perl doesn't attempt to append location or other information to a reference exception. If you want location information with a complex exception object, you'll have to arrange to put the location information into the object yourself.

perl は捕らえられなかった例外のメッセージを表示する前に文字列化するので、 このようなカスタム例外オブジェクトの文字列化をオーバーロードしたいと 思うかもしれません。 これに関する詳細は overload を参照してください。 文字列化されたメッセージは、文字列例外の扱いに合わせるために、 空ではなく、末尾は改行であるべきです。 また、例外オブジェクトリファレンスはそれを破壊することなく 文字列化することができないので、Perl はリファレンス例外に位置や その他の情報を追加しようとしません。 複雑な例外オブジェクトに位置情報が欲しい場合、 オブジェクト自身に位置情報を設定するように用意する必要があります。

Because $@ is a global variable, be careful that analyzing an exception caught by eval doesn't replace the reference in the global variable. It's easiest to make a local copy of the reference before any manipulations. Here's an example:

$@ はグローバル変数なので、 eval により補足された例外の解析はグローバル変数の リファレンスを置き換えないことに注意を払わなければなりません。 他の操作をする前にリファレンスのローカルコピーを 作るのが一番簡単です。 以下に例を示します:

    use Scalar::Util "blessed";

    eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) };
    if (my $ev_err = $@) {
        if (blessed($ev_err)
            && $ev_err->isa("Some::Module::Exception")) {
            # handle Some::Module::Exception
        }
        else {
            # handle all other possible exceptions
        }
    }

If an uncaught exception results in interpreter exit, the exit code is determined from the values of $! and $? with this pseudocode:

例外が捕捉されないとインタプリタは終了し、終了コードは以下の 擬似コードのように、$!$? の値から 決定されます:

    exit $! if $!;              # errno
    exit $? >> 8 if $? >> 8;    # child exit status
    exit 255;                   # last resort

As with exit, $? is set prior to unwinding the call stack; any DESTROY or END handlers can then alter this value, and thus Perl's exit code.

exit と同様に、コールスタックを巻き戻す前に $? が設定されます; DESTROYEND のハンドラが それからこの値を変更して、これが Perl の終了コードになります。

The intent is to squeeze as much possible information about the likely cause into the limited space of the system exit code. However, as $! is the value of C's errno, which can be set by any system call, this means that the value of the exit code used by die can be non-predictable, so should not be relied upon, other than to be non-zero.

この意図は、できるだけ多くの似たような原因に関する情報を、システム終了 コードという限られた領域に圧縮することです。 しかし、$! はシステムコールによって設定される可能性がある C の errno の値であり、die によって使われる終了コードの値は 予測不能であることを意味するので、非 0 ということ以上にこの値に 依存するべきではありません。

You can arrange for a callback to be run just before the die does its deed, by setting the $SIG{__DIE__} hook. The associated handler is called with the exception as an argument, and can change the exception, if it sees fit, by calling die again. See "%SIG" in perlvar for details on setting %SIG entries, and eval for some examples. Although this feature was to be run only right before your program was to exit, this is not currently so: the $SIG{__DIE__} hook is currently called even inside evaled blocks/strings! If one wants the hook to do nothing in such situations, put

$SIG{__DIE__} フックをセットすることで、 die がその行動を行う 直前に実行されるコールバックを設定できます。 結び付けられたハンドラは例外を引数として呼び出され、 必要なら再び die を呼び出すことで例外を変更できます。 %SIG のエントリをセットする詳細については、 "%SIG" in perlvar を、例については eval を参照してください。 この機能はプログラムが終了しようとする前に 1 回だけ実行していましたが、 現在ではそうではありません: $SIG{__DIE__} フックは eval された ブロック/文字列の中でも呼ばれるのです! もしそのような状況で何もしなくない時は:

    die @_ if $^S;

as the first line of the handler (see "$^S" in perlvar). Because this promotes strange action at a distance, this counterintuitive behavior may be fixed in a future release.

をハンドラの最初の行に置いてください("$^S" in perlvar を参照してください)。 これは離れたところで不思議な行動を引き起こすので、 この直感的でない振る舞いは将来のリリースで修正されるかもしれません。

See also exit, warn, and the Carp module.

exitwarnCarp モジュールも 参照してください。