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 ? :)

Random entries

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

Recent Assets

  • tegen-par-2024-2nd-place.jpeg
  • stanford-reunion.png
  • kiff.png
  • hoid.png
  • Dad-in-front-of-log-cabin.png
  • mistborn-trilogy.png
  • 2024-03-Heren1-27h.png
  • three-body-problem.png
  • 10CC.png
  • minds-and-machines.jpeg
  • puglia.png
  • 2023-09-24-jong-tegen-oud-1.jpg

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 2518 entries and as many as 1877 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.

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

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

My father passed away 10-20-2000 ago.

My mother passed away 3-27-2018 ago.

Started Gishtech 04-25-2016 ago.