Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

die

Perl 5 version 10.1 documentation
Recently read

die

  • die LIST

    Outside an eval, prints the value of LIST to STDERR and exits with the current value of $! (errno). If $! is 0 , exits with the value of ($?>> 8) (backtick `command` status). If ($?>> 8) is 0 , exits with 255 . Inside an eval(), the error message is stuffed into $@ and the eval is terminated with the undefined value. This makes die the way to raise an exception.

    Equivalent examples:

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

    If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied. 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.

    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".

    1. die "/etc/games is no good";
    2. die "/etc/games is no good, stopped";

    produce, respectively

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

    See also exit(), warn(), and the Carp module.

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

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

    If LIST is empty 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.

    If $@ is empty then the string "Died" is used.

    die() can also be called with a reference argument. If this happens to be trapped within an eval(), $@ contains the reference. This behavior permits a more elaborate exception handling implementation using objects that maintain arbitrary state about the nature of the exception. Such a scheme is sometimes preferable to matching particular string values of $@ using regular expressions. Because $@ is a global variable, and eval() may be used within object implementations, care must be taken that analyzing the error object doesn't replace the reference in the global variable. The easiest solution is to make a local copy of the reference before doing other manipulations. Here's an example:

    1. use Scalar::Util 'blessed';
    2. eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) };
    3. if (my $ev_err = $@) {
    4. if (blessed($ev_err) && $ev_err->isa("Some::Module::Exception")) {
    5. # handle Some::Module::Exception
    6. }
    7. else {
    8. # handle all other possible exceptions
    9. }
    10. }

    Because perl will stringify uncaught exception messages before displaying them, you may want to overload stringification operations on such custom exception objects. See overload for details about that.

    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 will be called with the error text and can change the error message, if it sees fit, by calling die again. See $SIG{expr} in perlvar for details on setting %SIG entries, and eval BLOCK for some examples. Although this feature was to be run only right before your program was to exit, this is not currently the case--the $SIG{__DIE__} hook is currently called even inside eval()ed blocks/strings! If one wants the hook to do nothing in such situations, put

    1. 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.