How to debug the perl via cgi?

Posted on 2005-05-17
Last Modified: 2010-03-05
I am calling perl program from a web. When there is a problem, the error returns page cannot be displayed.
How i can generate meaning error and see for debug?
Question by:StanleyLMW
    LVL 19

    Assisted Solution

    If you add the following to your code, you will get more detailed error messages appearing in the browser
    use CGI::Carp 'fatalsToBrowser';
    LVL 16

    Expert Comment

    ...or you can check your webserver error logs to see the details.
    LVL 28

    Expert Comment

    use warnings;
    use diagnostics;
    use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

    warningsToBrowser(1);   # this goes after you print the header
    LVL 84

    Expert Comment

    You might also try calling the program from the command line using the same parameters that cause the error, so you can use the Perl source debugger.

    Author Comment


    teraplane solution is perfect.

    FishMonger, i got errors simply using use warnings. not sure why.
    use diagnostics my perl cannot reconize.
    LVL 84

    Expert Comment

    if you're not sure why you get warnings, that could indicate a problem.
    You should determine he cause of the warnings and try to eliminate them.
    If you don't understand the error messages, post them here along with the code,
    and we may be able to help you interpret them.

    Author Comment


    use CGI::Carp 'fatalsToBrowser'
    this seems could capture the syntax error for me.
    However, if there is run time error, it cannot give me clue.

    e.g. i have database call with incorrect sql. It just gives me the page not found.
    LVL 16

    Expert Comment

    Does the runtime error occur everytime, or for selected user-inputted values??
    If the answer to the above is "selected values", you should be putting a lot of checks to first make sure that the database call you are making is valid. Putting user-supplied fields straight into your sql makes your code prone to sql injection attacks.

    If the error occur always, follow the advice given in http:#14024929

    LVL 1

    Accepted Solution

    Hello there.
    I have found the following very useful in debbugging CGI scripts. Put it at the top of cgi programme before anything else.

    #!/usr/local/bin/perl -T

    BEGIN {
         use CGI::Carp qw(carpout);
         open(LOG, ">/usr/path_to_directory_that_webserver_has_permissions_to_write_to/mycgi.$$.log") or
           die("Unable to open mynewcgi-log: $!\n");

    Remember to:
    1) change the path and filename to suit your system
    2) Once you have finished debbugging your cgi and your happy with it. Remove the BEGGIN statement.

    I think this should catch all errors or has all of mine in the past.

    Hope this helps.


    Author Comment


    Hi lovesnakeyjakey , it works! so great!
    In my case, i cannot use -T, i dont know why. But without -T still work. What does -T means?

    LVL 84

    Assisted Solution

    perldoc perlrun
           -T   forces "taint" checks to be turned on so you can test them.  Ordi-
                narily these checks are done only when running setuid or setgid.
                It's a good idea to turn them on explicitly for programs that run
                on behalf of someone else whom you might not necessarily trust,
                such as CGI programs or any internet servers you might write in
                Perl.  See perlsec for details.  For security reasons, this option
                must be seen by Perl quite early; usually this means it must
                appear early on the command line or in the #! line for systems
                which support that construct.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now