The Perl module CGI::Carp
allows you to define a different error handler than the boring "A Software Error Occurred"
one by using set_message()
like this:
use CGI::Carp qw(fatalsToBrowser set_message);
...
set_message(\&handle_error);
Now all calls to die()
are caught and sent to the user-defined subroutine handle_error()
which will present the user with a nicer HTML page rather than the boring default one generated by CGI::Carp
. Pretty nifty.
The only problem with this is when you are debugging. The contents of the HTML page are barfed up and scattered across stdout
. This makes it hard to search through all of the HTML and find exactly where the error description, error code and line number is.
So what you need to do is define a simpler user-defined error handler which generates a plain-vanilla printf
message.
How can the script detect whether or not it is in the debugger? Simple, just check if &DB::DB
is defined. If it is defined, then call set_message()
and pass it the simpler error handler like this:
set_message(\&handle_error_printf) if (defined &DB::DB);
...
sub handle_error_printf {
my $msg = shift;
print("$msg\n");
}
You're on your way to a happy and productive life as a Perl debugger kind of person.
Haven't used the Perl debugger in a while ( I'm using "print" and "say" for debugging and writing tests in order to avoid the debugger ).
I was interested to find if this
feature was documented somewhere and I found in perldoc perlvar:
Also found some info about it in Programming Perl 20.5.1
I'm curious where did you find out about it ? :)