Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

How to Debug the CGI Codes

by gopalr (Priest)
on Jul 10, 2006 at 06:46 UTC ( [id://560079] : perlquestion . print w/replies, xml ) Need Help??

gopalr has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am new to CGI. I want to know, how to debug the CGI coding. In windows we can use PerlIDE to debug the code like that I want.

Now I am using the following to debug the CGI Code.

print STDERR $var; #(we can show the value of $var in error_log file) +;

Please let me know, is there any other way to debug the CGI Code.

I am using Linux and Apacher Server.

Gopal R.

Replies are listed 'Best First'.
Re: How to Debug the CGI Codes
by McDarren (Abbot) on Jul 10, 2006 at 06:58 UTC
    There are all sorts of methods you can employ for debugging CGI code - here are a couple that I often use:
    use CGI::Carp qw/fatalsToBrowser/;
    This will throw any errors or warnings to your browser. (Never leave this enabled in production code).

    use Data::Dumper::Simple; print q(<pre>); print Dumper(%foo);
    Useful for examining the contents of any data structure.
    my @params = param(); print q(<pre>); for (@params) { print "$_ :: $params[$_]\n"; }
    Useful to find out which CGI parameters were passed and what their values are.

    Hope this helps,
    Darren :)

      For development, I even use:

      use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

      because it also handles warnings

      Update: is wrong, see the posting from reneeb below

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

        To handle warnings, you have to call the warningsToBrowser subroutine with a true argument. See documentation of CGI::Carp.
        Just a note:
        warningsToBrowser(1) must be explicitly called after printing the page header, and will output all warnings as HTML comments. It's its difference from fatalsToBrowser, which catches all fatal errors (even compile-time!) without any additional actions. The CGI::Carp documentation contains additional info.

        UPD: i haven't seen the previous reply while writing this :)

Re: How to Debug the CGI Codes
by davorg (Chancellor) on Jul 10, 2006 at 07:58 UTC

    In addition to the excellent advice you've already got from McDarren it's worth pointing out that CGI::Carp is a very useful module for getting nicely formatted error messages out of a CGI program. In particular, I recommend the "carpout" function which allows you to set up an alternative log file that CGI::Carp messages get written to.

    You should also be aware that a CGI program really isn't that much different to any other Perl program and you don't need to run it on a web server. As long as the required inputs are set up, you can run a CGI program from the command line. I've debugged many a problematic CGI program using Perl's built-in debugger.


    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg