in reply to Re: Customizing the 'die' funtion
in thread Customizing the 'die' funtion

Actually, you can literally redefine die by... redefining die:
local *CORE::GLOBAL::die = sub { "My version of die." }; # use it...
You almost certainly want to do this instead of overriding $SIG{__DIE__}, since the latter doesn't stop exception handling, but just gives you a chance to do some stuff before die continues going about its business.

Replies are listed 'Best First'.
Re^3: Customizing the 'die' funtion
by kennethk (Abbot) on Apr 26, 2010 at 22:56 UTC
    Yes, you can also literally redefine die using a typeglob in a BEGIN block, but I would disagree that this is generally what one would want. If you want to prevent die from killing your script, I think it is far better to trap potential dies in eval blocks, since you won't violate assumptions made in code you are calling (like having an open fail and still trying to process input). If you are not calling any external code, there is (generally) no reason to be using die in the first place if you don't want to throw exceptions.

    Certainly in the case of the OP, there is no indication that the exception should be prevented.

      Sure, if you just want to catch an error, eval is what you want. My point is that if you're reaching for $SIG{__DIE__}, you're almost certainly doing something wrong. For example, if you want to create resumable exceptions or drop into the debugger before unwinding the stack on a die, CORE::GLOBAL::DIE is what you want.