Is &DB::DB defined?

 | Perl | 1 Comment | 0 TrackBacks

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.

No TrackBacks

TrackBack URL: http://www.kiffingish.com/cgi-bin/mt/mt-tb.cgi/374

1 Comment

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

(defined &DB::DB)

feature was documented somewhere and I found in perldoc perlvar:

$^P 
The internal variable for debugging support. 
for $^P == x02  
Line-by-line debugging. Causes DB::DB() subroutine to be called for each statement executed. Also causes saving source code lines (like 0x400).

Also found some info about it in Programming Perl 20.5.1

I'm curious where did you find out about it ? :)

Leave a comment

Recent Entries

No more winter greens
The cool, windy and grayish afternoon calls me onward. It's drizzling on my windshield but I do not care.
Champion of Scotland
I nearly cried my eyes out when I discovered that young Tommy Morris dies in the prime of his
Sticky keyboard
Up until yesterday the Del-, Ins- and Home- keys on the upper right-hand corner were sticking alot, and it's
Perl on steroids
Read this from beginning to end, and you then tell me with a straight face that it hasn't completely
Seventh Dutch Perl Workshop
Going to attend the 7th Dutch Perl Workshop tomorrow in Arnhem. Cannot wait to get up bright and early