OVERVIEW

DESCRIPTION

This chapter of the tutorial takes a brief look at the primary options available for troubleshooting Catalyst applications.

Source code for the tutorial in included in the /home/catalyst/Final directory of the Tutorial Virtual machine (one subdirectory per chapter). There are also instructions for downloading the code in Catalyst::Manual::Tutorial::01_Intro.

Note that when it comes to debugging and troubleshooting, there are two camps:

Fans of log and print statements embedded in the code.

Fans of interactive debuggers.

Catalyst is able to easily accommodate both styles of debugging.

LOG STATEMENTS

Folks in the former group can use Catalyst's $c->log facility. (See Catalyst::Log for more detail.) For example, if you add the following code to a controller action method:

NOTE: Whether you are a logging fanatic or not, we strongly recommend that you take advantage of Log::Log4perl or Log::Dispatch. It's easy to use Catalyst::Log with either of these and they will provide a huge amount of extra functionality that you will want in virtually every production application you run or support.

RUNNING CATALYST UNDER THE PERL DEBUGGER

Members of the interactive-debugger fan club will also be at home with Catalyst applications. One approach to this style of Perl debugging is to embed breakpoints in your code. For example, open lib/MyApp/Controller/Books.pm in your editor and add the DB::single=1 line as follows inside the list method (I like to "left-justify" my debug statements so I don't forget to remove them, but you can obviously indent them if you prefer):

sub list :Local {
# Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
# 'Context' that's used to 'glue together' the various components
# that make up the application
my ($self, $c) = @_;
$DB::single=1;
# Retrieve all of the book records as book model objects and store in the
# stash where they can be accessed by the TT template
$c->stash->{books} = [$c->model('DB::Book')->all];
# Set the TT template to use. You will almost always want to do this
# in your action methods.
$c->stash->{template} = 'books/list.tt2';
}

This causes the Perl Debugger to enter "single step mode" when this command is encountered (it has no effect when Perl is run without the -d flag).

NOTE: The DB here is the Perl Debugger, not the DB model.

If you haven't done it already, enable SQL logging as before:

$ export DBIC_TRACE=1

To now run the Catalyst development server under the Perl debugger, simply prepend perl -d to the front of script/myapp_server.pl:

$ perl -d script/myapp_server.pl

This will start the interactive debugger and produce output similar to:

Press the c key and hit Enter to continue executing the Catalyst development server under the debugger. Although execution speed will be slightly slower than normal, you should soon see the usual Catalyst startup debug information.

Now point your browser to http://localhost:3000/books/list and log in. Once the breakpoint is encountered in the MyApp::Controller::list method, the console session running the development server will drop to the Perl debugger prompt:

You now have the full Perl debugger at your disposal. First use the next feature by typing n to execute the all method on the Book model (n jumps over method/subroutine calls; you can also use s to single-step into methods/subroutines):

For more information on using the Perl debugger, please see perldebug and perldebtut. For those daring souls out there, you can dive down even deeper into the magical depths of this fine debugger by checking out perldebguts.

You can also type h or h h at the debugger prompt to view the built-in help screens.

For an excellent book covering all aspects of the Perl debugger, we highly recommend reading 'Pro Perl Debugging' by Richard Foley.

Oh yeah, before you forget, be sure to remove the DB::single=1 line you added above in lib/MyApp/Controller/Books.pm.

DEBUGGING MODULES FROM CPAN

Although the techniques discussed above work well for code you are writing, what if you want to use print/log/warn messages or set breakpoints in code that you have installed from CPAN (or in module that ship with Perl)? One helpful approach is to place a copy of the module inside the lib directory of your Catalyst project. When Catalyst loads, it will load from inside your lib directory first, only turning to the global modules if a local copy cannot be found. You can then make modifications such as adding a $DB::single=1 to the local copy of the module without risking the copy in the original location. This can also be a great way to "locally override" bugs in modules while you wait for a fix on CPAN.

Matt Trout has suggested the following shortcut to create a local copy of an installed module:

mkdir -p lib/Module; cp `perldoc -l Module::Name` lib/Module/

Note: If you are following along in Debian 6 or Ubuntu, you will need to install the perl-doc package to use the perldoc command. Use sudo aptitude install perl-doc to do that.

If the method exists, the Perl can method returns a coderef. Otherwise, it returns undef and nothing will be printed.

TT DEBUGGING

If you run into issues during the rendering of your template, it might be helpful to enable TT DEBUG options. You can do this in a Catalyst environment by adding a DEBUG line to the __PACKAGE__-config> declaration in lib/MyApp/View/HTML.pm:

There are a variety of options you can use, such as 'undef', 'all', 'service', 'context', 'parser' and 'provider'. See Template::Constants for more information (remove the DEBUG_ portion of the name shown in the TT docs and convert to lower case for use inside Catalyst).

NOTE:Please be sure to disable TT debug options before continuing with the tutorial (especially the 'undef' option -- leaving this enabled will conflict with several of the conventions used by this tutorial to leave some variables undefined on purpose).

As a valued partner and proud supporter of MetaCPAN, StickerYou is
happy to offer a 10% discount on all Custom Stickers,
Business Labels, Roll Labels,
Vinyl Lettering or Custom Decals. StickerYou.com
is your one-stop shop to make your business stick.
Use code METACPAN10 at checkout to apply your discount.