http://qs321.pair.com?node_id=745698

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

What is the different between die and exit? I know that die will print the error message what we give before exit from the program, any other difference is there?

Replies are listed 'Best First'.
Re: different between die and exit
by ikegami (Patriarch) on Feb 23, 2009 at 04:17 UTC

    die is used to throw an exception (catchable using eval).
    exit is used to exit the process.

    die will set the error code based on $! or $? if the exception is uncaught.
    exit will set the error code based on its argument.

    And of course,
    die outputs a message
    exit does not.

    Update: Added "catchable" bit by request of ysth.

      One important similarity:
      die and exit both unwind the stack, calling destructors and END blocks when necessary.

Re: different between die and exit
by ELISHEVA (Prior) on Feb 23, 2009 at 11:53 UTC
    To clarify a bit what irah said:
    • You can keep die from terminating the program if you surround it (or a sub that calls it) with eval{...}
    • exit always exits, even if you surround exit with eval.

    Example:

    use strict; use warnings; eval { die }; print "You can see I'm still alive!\n"; eval { exit }; print "You should never see this print\n";

    Best, beth

      ITYM "almost always exits (unless someone's done some really evil monkeying around behind your back)" . . .

      use strict; use warnings; BEGIN { *CORE::GLOBAL::exit = sub { print "You'll never take me alive, + copper!\n"; }; } eval { die }; print "You can see I'm still alive!\n"; eval { exit }; print "You should never see this print\n"; eval { CORE::exit }; print "Now this you won't get to see\n";

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Well, if you're going to be that picky, exit isn't the only function that can be overriden.

        use strict; use warnings; BEGIN { *CORE::GLOBAL::die = sub { print(STDERR @_); CORE::exit($! || 255); }; } eval { die }; print "You can see I'm still alive!\n"; # Never reached

        All bets are off when you start overriding functions, so your nit is pointless.

Re: different between die and exit
by irah (Pilgrim) on Feb 23, 2009 at 04:38 UTC

    If you want to trap whatever error happened use die function, to capture the error message. The exit does not.

Re: different between die and exit
by sundialsvc4 (Abbot) on Feb 24, 2009 at 22:03 UTC
    In my somehow-not-forgotten mainframe days, we'd say that die represents an ABEND:   an “abnormal end of program.” Whether or not it is caught, it plainly declares itself to be abnormal.
Re: different between die and exit
by djp (Hermit) on Feb 24, 2009 at 06:19 UTC
    Also, die (by default unless caught) returns a non-zero exit status to the operating system, whereas exit (by default) returns a zero exit status.

    Update: corrected as per ikegami's comment.

      It makes no sense to talk of "by default" for die. There's no argument to specify the exit code, so it can't be omitted to use the default.

      Also, die always returns non-zero ($! || ($?>>8) || 255).

Re: different between die and exit
by mr_mischief (Monsignor) on Feb 26, 2009 at 00:30 UTC

    I'll defer to ikegami's (amended) reply for the differences between those two items. For completeness, I'll also mention another more rarely used option and explain their relative usefulness.

    One may also wish to compare and contrast POSIX::_exit which exits the program immediately. It does not run destructors or END blocks and doesn't flush buffered I/O streams. Both die and exit do those things before they cause the program to exit to the OS.

    The behavior usually wanted is that of die(). You might not be catching the condition now, but you may want to later.

    Sometimes exit() is preferred. You might want to exit in a way that specifically always exits the program and ends the process. You might want to write your error condition somewhere other than STDERR then return a specific error code to the parent program. I've written some network server code that uses print() and exit() to do just that.

    Only quite rarely will POSIX::_exit() be needed. Most people will probably never find a good reason to use it, but it's available in case it is needed.