If you're trying to debug a CGI script, CGI::Carp could be your best friend. It makes getting error messages from CGI scripts with a browser practically as easy as from other scripts at the command line.

My favorite feature is also the easiest to use. Import the special symbol fatalsToBrowser like this:         use CGI::Carp qw(fatalsToBrowser);

Now, if have a syntax error in your script that would abort execution due to compilation errors, say, instead of getting a server error from your HTTP server that isn't very informative, you get a message telling you that there are compilation errors. If I used perl -c more often, I probably wouldn't be so attached to this feature.

On the downside, information about what line the error is at doesn't show up in your browser. It would be nice if it did. However, you can retrieve that information from your HTTP server's error log.

Once you get past syntax errors, anything that you want to die() or croak() about will go to the browser, too, if you've imported fatalsToBrowser. If you want warnings to go to the browser as well, import the carpout() function and use it to redirect STDERR.

You can also use carpout() to redirect STDERR to your own log file, instead of the server's error log. Here's the example from perldoc CGI::Carp:

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

These conveniences make CGI programming a lot easier, and they make CGI::Carp one of my favorite modules.


In reply to CGI::Carp by redcloud

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.