Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: different between die and exit

by ELISHEVA (Prior)
on Feb 23, 2009 at 11:53 UTC ( [id://745754] : note . print w/replies, xml ) Need Help??


in reply to different between die and exit

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

Replies are listed 'Best First'.
Re^2: different between die and exit
by Fletch (Bishop) on Feb 23, 2009 at 17:29 UTC

    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.