I have a Perl web app and would like to log all uncaught exceptions (uneval’ed die’s). My first thought is to do something like this:
use Carp 'cluck';
sub main {
my $logfile ="/some/path/logfile.txt";
open STDERR, ">>$logfile";
# main logic
...
}
sub eval_main {
eval {
main;
};
if ($@) {
cluck $@;
close STDERR;
# redirect to "friendly error page"
....
}
}
eval_main;
Is there a better way than this?
EDIT: Added redirect
Just use a
$SIG{__DIE__}handler. See %SIG in perlvar: