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.
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
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 | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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.
| [reply] [d/l] [select] |
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.
| [reply] |
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. | [reply] [d/l] |
Re: different between die and exit
by djp (Hermit) on Feb 24, 2009 at 06:19 UTC
|
| [reply] |
|
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).
| [reply] [d/l] [select] |
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.
| [reply] |