Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

perlfunc:die

by gods
on Aug 24, 1999 at 22:42 UTC ( #235=perlfunc: print w/ replies, xml ) Need Help??

die

See the current Perl documentation for die.

Here is our local, out-dated (pre-5.6) version:


die - raise an exception or bail out



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:

    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 value of EXPR 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. 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''.

    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.

See also exit() and warn().

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:

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

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

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 perlman:perlvar for details on setting %SIG entries, and eval BLOCK for some examples.

Note that the $SIG{__DIE__} hook is 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 1).


Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2015-07-06 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls