Is &DB::DB defined?

| Perl | 1 Comment

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.

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

Random entries

Here are some random entries that you might be interested in:

Recent Assets

  • Tenerife-flight-path.png
  • listen-to-me-marlon.jpg
  • sneeuwval.jpg
  • frozen-crystals.png
  • winter-is-coming.png
  • donald-trump.png
  • Rex-indian-headdress.png
  • Liam-future-famous-golfer-2016-10.jpg
  • number-one.png
  • Mom-and-me-2016.png
  • 636101580191153661-Kiffen-0003675-02.jpg
  • the-haskell-road-ro-logic.png

Recent Comments

Golf Handicap

Information

This personal weblog was started way back on July 21, 2001 which means that it is 7-21-2001 old.

So far this blog contains no less than 2279 entries and as many as 1881 comments.

Important events

Graduated from Stanford 6-5-1979 ago.

Kiffin Rockwell was shot down and killed 9-23-1916 ago.

Believe it or not but I am 10-11-1957 young.

Began well-balanced and healthy life style 1-8-2013 ago.

My father passed away 10-20-2000 ago.

First met Thea in Balestrand, Norway 6-14-1980 ago.