In "Safe" ways for Carp to report errors to browser?, I discussed a BEGIN block where I trap perl compilation and runtime errors using CGI::CARP. The code below is my entire BEGIN block, which illustrates a new question posed afterwards.

This block intercepts perl errors and sets an error handler to determine where to send the output. If the user has a cookie named "debug" and it's value is "true", then errors go to the browser (and stderr is redirected to /dev/null). Otherwise, errors are appended to a file that I can browse later. (The file is uniquely named using the IP address of the browser that triggered the error.)

BEGIN { use CGI::Carp qw(fatalsToBrowser set_message); sub handle_error { use CGI; use CGI::Carp qw(carpout); $query = new CGI; $debug = $query->cookie('debug') || "unknown"; if ($debug eq "true") { carpout(LOG) if open(LOG, ">/dev/null"); $msg = shift; print "<h1>Script Error:</h1>\n\n"; $msg =~ s/\n/<br>/g; } else { if (open FILE, "../web-banner2") { print <FILE>; close FIL +E; } $remote_addr = $ENV{'REMOTE_ADDR'}; if (open FILE, "+>>tmp/$remote_addr") { print FILE "Script Error: " . localtime(time()); print FILE qq( Address: $remote_addr Referrer: $ENV{HTTP_REFERER} ===========\n); carpout(FILE); print FILE "===============\n\n"; close FILE; } else { print "Note: can't open error log: $!<br>\n"; } $msg = qq(<h1>Software Error!</h1> Please <a href="mailto:argv@danheller.com">send me mail</a>.); } print $msg; } set_message(\&handle_error); }

The new question revolves around the length of this block and the best place to put it. I have many perl scripts that run my site, and I don't want to copy this code redundantly at the top of each one. So, I made a module called that I "include" (via use) at the top of each perl script.

The questions are, first, it seems odd to have an entire module that contains nothing but a BEGIN block. Is that an appropriate way to handle all this? Also, would this interfere with other BEGIN blocks in other places in ways not apparent with simple testing?

ps. Obviously, I didn't need to include the entire block to ask the question, but I figured the code would be useful to some... a search for CGI::Carp on this site doesn't yield much.

In reply to Where to put long BEGIN blocks used by many programs... by argv

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.