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

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.

Replies are listed 'Best First'.
Re: Where to put long BEGIN blocks used by many programs...
by Aristotle (Chancellor) on Jan 11, 2005 at 01:05 UTC

    Make it a module. use will automatically run the code at compilation time. You can even leave out the explicit BEGIN block.

    Also, pull those use clauses out of your function. use is unconditionally executed at compile time, so putting them in there is just misleading.

    PS.: please take the time to learn about PerlMonks linking syntax and use it. Since I read PM using www.perlmonks.org, my cookie is set for that host, so following your link sends me to a page where I'm not logged in. As a bonus, using [id://420722] would have inserted the title of your thread into the text automagically.

    Makeshifts last the longest.

      right on all counts. thanks.

      As for the linking syntax... I was curious how others had done it, so I looked it up and found out... but it was after I already posted. I was going to update my posting with the new link sytnax, but then got sidetracked by the fact that "update" doesn't give a "preview" button like normal posting does... so, I hesitated (cuz I didn't want to make a mistake, and I wanted to actually see it work), so I opted to ask folks in the chatterbox why there was no Preview button... this started a discussion that had me forgetting what I was going in the first place.

      sigh...