in reply to Customizing the 'die' funtion

Rather than redefining die, you can suppress the script name and the line number in output simply by ending your error string with a new line, a la:

die "No extra info\n";

This also works for warn and is documented in die.

You can literally redefine die (which is appropriate in other contexts) by putting an appropriate hook in %SIG, or you can shift in which package Perl considers the error to have occurred with carp and modifying the @CARP_NOT array appropriately.

Replies are listed 'Best First'.
Re^2: Customizing the 'die' funtion
by educated_foo (Vicar) on Apr 26, 2010 at 21:48 UTC
    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.
      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.