in reply to Improving the Error Handling of my Scripts

I use $SIG{__DIE__} to send me a mail:

$SIG{__DIE__} = sub { my $err = $@; local $@; mail($@); };

That way, whenever the script dies, the error gets mailed to me.

Replies are listed 'Best First'.
Re^2: Improving the Error Handling of my Scripts
by almut (Canon) on Mar 11, 2010 at 00:00 UTC

    I think the die-message is available in $@ only in case the die happens within an eval{} block (in which case I personally wouldn't want to be mailed).

    If the die happens outside of an eval, however (i.e. when the script atually dies), the message is only available in @_.   (Also, I suppose your code should be mail($err), as the localized $@ would always be empty...)

    In other words, I'd use something like

    #!/usr/bin/perl use strict; use warnings; use Carp; $SIG{__DIE__} = sub { die @_ if $^S; # skip handler for eval{} blocks mail(@_); }; sub mail { chomp(my $msg = shift); print qq(mailing "$msg"...\n\n); } sub test { eval { croak "bar"; }; # doesn't send mail croak "foo"; # sends mail (and dies) } test(); __END__ $ ./827866.pl mailing "foo at ./827866.pl line 19 main::test() called at ./827866.pl line 22"... foo at ./827866.pl line 19 main::test() called at ./827866.pl line 22