How can I troubleshoot my Perl CGI script?

前端 未结 8 1091
深忆病人
深忆病人 2020-11-22 02:43

I have a Perl script that isn\'t working and I don\'t know how to start narrowing down the problem. What can I do?


Note: I\'m adding the question because I rea

8条回答
  •  长情又很酷
    2020-11-22 02:59

    Are you using an error handler while you are debugging?

    die statements and other fatal run-time and compile-time errors get printed to STDERR, which can be hard to find and may be conflated with messages from other web pages at your site. While you're debugging your script, it's a good idea to get the fatal error messages to display in your browser somehow.

    One way to do this is to call

       use CGI::Carp qw(fatalsToBrowser);
    

    at the top of your script. That call will install a $SIG{__DIE__} handler (see perlvar)display fatal errors in your browser, prepending it with a valid header if necessary. Another CGI debugging trick that I used before I ever heard of CGI::Carp was to use eval with the DATA and __END__ facilities on the script to catch compile-time errors:

       #!/usr/bin/perl
       eval join'', ;
       if ($@) { print "Content-type: text/plain:\n\nError in the script:\n$@\n; }
       __DATA__
       # ... actual CGI script starts here
    

    This more verbose technique has a slight advantage over CGI::Carp in that it will catch more compile-time errors.

    Update: I've never used it, but it looks like CGI::Debug, as Mikael S suggested, is also a very useful and configurable tool for this purpose.

提交回复
热议问题