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

I've written a CGI script that does some file manipulation. I do some error checking in case the file isn't there, if it does error out I get a blank web page. What module do I need to use for those errors to be printed on the page? What is the particular syntax?

Thanks in advance,
am

Replies are listed 'Best First'.
RE: CGI Errors in HTML
by Ovid (Cardinal) on Jun 16, 2000 at 23:08 UTC
    CGI::Carp is always the first step. However, "use CGI::Carp qw(fatalsToBrowser);" occasionally does not provide everything you need to debug your script. If you have access to your servers error log, typing (in Linux, for example) tail error.log (or whatever your error log filename is, assuming you're in the log directory) will print out the last few lines of that log and may give you additional information.

    If you are using the CGI module, you can try instantiating a CGI object and running the script from the command line.

    #!/usr/bin/perl -wT use strict; use CGI; my $query = new CGI;
    When running the script from the command line, it will as you to enter name/value pairs in the format name=value. Enter all appropriate name/value pairs and hit cntl-d (cntl-z on Win32 systems) and the script will continue execution. Any errors will be directed to your terminal window.

    Cheers, Curtis

Re: CGI Errors in HTML
by btrott (Parson) on Jun 16, 2000 at 22:43 UTC
    Use CGI::Carp. Fatal errors (die) will then be sent to your browser:
    use CGI::Carp qw(fatalsToBrowser); die "Fatal error messages are now sent to browser";
    Also, make sure that you print out the correct HTTP headers.
      Don't do this on a production site, though. It reveals too much in the case of errors. My current Linux Magazine column shows how to send email on errors, instead of to the browser.

      -- Randal L. Schwartz, Perl hacker

Re: CGI Errors in HTML
by cleen (Pilgrim) on Jun 16, 2000 at 22:32 UTC
    Are you printing out the correct HTTP headers?

    It wouldnt be an "error" persay if the page is just coming up blank, try checking your error log file for your web server, that can give out massive amounts of good information :), also try putting some debugging options in your script (it has nothing to do with a module), simply use pre tags (to make sure its not getting mixed up with other html) and spit out various variables and stuff..or at least thats what I would do.

    -cleen
use CGI::Carp qw (carpout);
by Q*bert (Sexton) on Jun 17, 2000 at 11:34 UTC
    The foregoing suggestions are good ones. I also recommend directing your errors to a separate log using carpout. That way, you don't clutter up your error log too much, and you don't have to grep for the program name (in the case that other programs are writing to the error log at the same time).

    If you will be invoking multiple instances of the program at the same time, don't forget to do file locking on the carpout log! There's a recipe in <cite>The Perl Cookbook</cite> about how to do this; in short, you want to use Fcntl qw (:flock); and then use the flock function, which closely resembles flock(2) from the Unix kernels.

    TMTOWTDI, but ITYWLTW (I think you will like this way). ;) Cheers.

RE: CGI Errors in HTML
by cds (Sexton) on Jun 17, 2000 at 11:07 UTC

    Sometimes you need to do a few more checks too. If you're using Netscape, then if you have a table open when the script dies then that table and anything in it wont show up. If you're using tables and Netscape and don't get content when you expect some, view the HTML source.

    I don't use CGI.pm much (when I use Perl/Apache I like to write modules with mod_perl), so I'm not sure if things like CGI::Carp will automatically close tables for you.

    Colin Scott
    If you build it, they will be dumb...