2

I really enjoy using Firebug for debugging my JavaScript code, HTML DOM, and network loading, that is: everything client-side about web development.

The situation is not so bright on the server side. The application which I'm working on uses Perl with the obligatory standard modules like CGI and DBI. I know the standard troubleshooting techniques, but there seems to be no debugging tool that traces e.g. database queries or function calls and gives me nice output.

The output must especially not just be yanked on STDOUT (imagine Content-Type: application/json) or STDERR (grepping through error logs is quite uncomfortable), and the tool should not require me to manually monkey-patch stuff into core modules or sprinkle use Data::Dumper; print '<pre>',Dumper($foo),'</pre>' through-out the code.

Long story short: Is there a tool that makes debugging the server-side of Perl/CGI as fun as Firebug does on the client-side?

3 Answers 3

7

Yes there are, but they rely on Plack. Plack is a layer which sits between web frameworks and web servers. Plack smooths over server differences and offers a place to plug in all sorts of debugging tools. These include database profile and trace information, and you can write your own debugging panels. There is also an interactive debugger, though I haven't used it myself.

Many Perl web frameworks these days (Catalyst and Dancer for example) use Plack, but if you're using straight Perl and CGI you're not going to get it. Fortunately, it's pretty easy to make a CGI application run on Plack. Either replace CGI with a subclass that uses Plack, or wrap your CGI code in an emulation layer.

Converting to Plack has a lot of advantages beyond the debugging tools, it's well worth the effort.

3
  • +1, because PSGI/Plack is the modern way. And I really like Enbugger which allows the loading of the debugger at runtime. Aug 6, 2012 at 3:05
  • Plack::Middleware::Debug looks nice, but still hsa the problem that it works only with Content-Type: text/html since it manipulates "STDOUT". Aug 6, 2012 at 13:43
  • @StefanMajewsky Yeah, you'd have to temporarily change the content type. Otherwise it would have to be a browser plugin, and I don't know of any to debug server-side code.
    – Schwern
    Aug 7, 2012 at 1:12
6

No, not really.

Things you can do are:

Sprinkle your code with debug code.

use constant DEBUG => 1;
...;
debug_function($var) if DEBUG;

Once you unset the DEBUG constant for deployment, you won't have any extra runtime costs (the statements are optimized away), and you can re-activate them whenever neccessary.

You can even say evil things like

BEGIN {require Data::Dumper if DEBUG}

to conditionally load debugging modules.

If you want to override Core functions, why not add debug handlers? That's not evil patching, thats augmenting…

BEGIN {
   if (DEBUG) {
      my $oldfunction = \&CORE::function;
      *CORE::function = sub {  # add prototypes if you like them
         debug_handler(@_);
         &$oldfunction;
      };
   }
}

Use this technique to log your calls to your database.

You can re-open STDERR to some special file (or pipe, or terminal, or…)

BEGIN {
   if (DEBUG) {
      close STDERR or die "STDERR hates me and doesn't want to be closed.";
      open STDERR, '>', "/path/to/my/error/log.file" or die "couldn't open error log";
   }
}

You can define your DIE and WARN handlers to do something you want:

$SIG{__WARN__} = sub {
    print MYERRORS "There was a warning: $!";
    # do additional error handling unless you're paranoid
}

The warnings pragma can help you to make important warnings fatal:

use warnings qw(FATAL importantWarning);

or to create your own warnings, just see the documentation

And to get call stack traces, look into the numerous possibilities of the caller function. Essentially, you might want to build your personal interpretation of the Carp module as CGI::Carp may not meet your needs.

2
1

In addition to the above, check Devel::LTrace, which can tell you when named subroutines are called, and Devel::CallTrace, which can tell what you what subroutines are called when and where.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.