[sf-perl] Debugging a CGI script - how

Garth Webb garth.webb at gmail.com
Sat Mar 8 13:21:34 PST 2008

On Sat, Mar 8, 2008 at 12:32 PM, Joe Brenner <doom at kzsu.stanford.edu> wrote:
>  Neil Heller <nheller at silcon.com> wrote:
>  > I've been modifying a CGI script and have run into a problem that bring to
>  > mind a larger question.
>  >
>  > My script uses data from a relational database to pull information about
>  > external files.  The script then displays information from the relational
>  > database and the contents of the external file.  The script then continues
>  > on the next iteration - each iteration equals one entry in the relational
>  > database.
>  >
>  > If there are 3 or fewer entries in the relational database, the script works
>  > well.  At the fourth entry in the relational database, Apache throws an
>  > error and the user (me at this point) receives only a generic-type error.
>  >
>  > Does anybody have any ideas about how to best go about finding the error?
>  Have you checked the apache error log?  Where are you seeing this
>  "generic error message"?
>  Note that if you toss in statements like:
>   print STDERR "The value is: $value at " . __LINE__ . "\n";
>  That output should show up in the apache error log.
>  At a guess, you should look at the data involved more closely: I would
>  guess there's something in your fourth record that's messing you up,
>  and you need to be more careful with quoting or using bind parameter
>  ("?" placeholders) or something like that.
>  Try some runs with different data, organized in different ways.
>  I also might suggest that at some point you shoule spend some time to
>  learn how to use the perl debugger, but that admittedly can be a bit
>  of a pain in a web context.

I would second learning the Perl debugger.  Its invaluable for quickly
getting to the root cause of your problems.  I don't think being in a
web context really changes much.  If its a straight CGI script (not
mod_perl) then you can easily run the script in the debugger and
supply your own parameters:

  $ perl -d your_script.cgi
  DB<1> CGI::param('foo' => 'bar');

Then later in your code, this will work how you expect:

  my $foo = CGI::param('foo')

If you put all your functionality into separate methods, its even
easier because you can just call the method with the args you need,
and you don't even have to run the whole program:

  DB<2> s my_method({arg1 => 'someval', arg2 => [1, 2, 3])

where 's' means 'step into'.  If you really want/need to run the
script directly from a real web request, its possible to start up
apache with the perl debugger running and step through requests as
they happen.

If your using mod_perl, you can still use the debugger, its just a
little more complicated if you want to debug calls that involve
mod_perl itself.  Most of the time though, I don't really need to
debug that code.

At work we run mod_perl and all our code is in separate Perl modules
that I can debug easily and individually outside mod_perl:

  $ perl -d -e 1
  DB<1> use Some::Data::Class
  DB<2> $res = Some::Data::Class->load_results(type => 'example')
  DB<3> x $res

which starts up the perl debugger in an interactive mode, loads a
module, makes a call to a method and then eXamines the results.


>  You might try to cut down your code the smallest example that reproduces
>  the problem, then you could post the code here, and we'll look it
>  over.
>  _______________________________________________
>  SanFrancisco-pm mailing list
>  SanFrancisco-pm at pm.org
>  http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

More information about the SanFrancisco-pm mailing list