=over
=item die LIST
X X X X X X
C raises an exception. Inside an C the error message is stuffed
into C and the C is terminated with the undefined value.
If the exception is outside of all enclosing Cs, then the uncaught
exception prints LIST to C and exits with a non-zero value. If you
need to exit the process with a specific exit code, see L.
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"
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
C. See L and L.
Hint: sometimes appending C to your message will cause it
to make better sense when the string C is appended.
Suppose you are running script "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 the output is empty and C already contains a value (typically from a
previous eval) that value is reused after appending C.
This is useful for propagating exceptions:
eval { ... };
die unless $@ =~ /Expected exception/;
If the output is empty and C contains an object reference that has a
C method, that method will be called with additional file
and line number parameters. The return value replaces the value in
C. i.e., as if CPROPAGATE(__FILE__, __LINE__) }; >>
were called.
If C is empty then the string C is used.
If an uncaught exception results in interpreter exit, the exit code is
determined from the values of C and C with this pseudocode:
exit $! if $!; # errno
exit $? >> 8 if $? >> 8; # child exit status
exit 255; # last resort
The intent is to squeeze as much possible information about the likely cause
into the limited space of the system exit code. However, as C is the value
of C's C, which can be set by any system call, this means that the value
of the exit code used by C can be non-predictable, so should not be relied
upon, other than to be non-zero.
You can also call C with a reference argument, and if this is trapped
within an C, C 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 C with regular expressions. Because C
is a global variable and C may be used within object implementations,
be careful that analyzing the error object 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:
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
}
}
Because Perl stringifies uncaught exception messages before display,
you'll probably want to overload stringification operations on
exception objects. See L for details about that.
You can arrange for a callback to be run just before the C
does its deed, by setting the C hook. The associated
handler is called with the error text and can change the error
message, if it sees fit, by calling C again. See
L for details on setting C entries, and
L for some examples. Although this feature was
to be run only right before your program was to exit, this is not
currently so: the C hook is currently called
even inside eval()ed blocks/strings! If one wants the hook to do
nothing in such situations, put
die @_ if $^S;
as the first line of the handler (see L). Because
this promotes strange action at a distance, this counterintuitive
behavior may be fixed in a future release.
See also exit(), warn(), and the Carp module.
=back