This is a great question, and I look forward to others replies to it. The big problem is that there is no one size fits all answer to this question. If your perl is a CGI script you don't want to die before you've printed out some error message. In other cases die is just fine. If you've got a database connection open you may want to clean up so an eval block is crucial. For the purposes of simple CGI's I've used the following general purpose "bomb" routine:
#---------------------------------------------------
# subroutine bomb
# a somewhat ungracefull way to end the execution of the cgi
sub bomb
{
my $error_text = shift;
print "\n<B>Ho Boy! A nasty error has occured:</B> ";
#extra \n to close off the HTTP header
print $error_text;
print "\n";
my @mail;
if ($MAIL_BOMB_LIST ne '') {
&printbomb(\@mail,$error_text);
@BombRecipients = split (/,/, $MAIL_BOMB_LIST);
my $submission = join '',@mail;
$submission = "\n\n".$submission;
$sent = &send_mail ($BombSMTPServer, $submission, 'CGI_Bomber@
+acme.com', "CGI $ENV{'SCRIPT_NAME'} bombed.", $BombReplyto, @BombReci
+pients);
}
exit;
}
# load error message and backtrace into @$array
sub printbomb
{
my $array = shift;
my $message = shift;
my($syserror) = $!;
push @$array, <<"EOM";
The CGI script $ENV{'SCRIPT_NAME'} seems to be having trouble!
Error Message: $message
Sys Error is: $syserror
EOM
my $i = 2; # start back 2 so as not to print bomb & printbomb s
+tack frames.
push @$array, "Backtrace:\n";
push @$array, " Subroutine name | Line |
+ File \n";
push @$array, "----------------------|------|---------------------
+----------------------------\n";
while(($pack,$file,$line,$subname) = caller($i++)) {
push @$array, sprintf("%21s |%5s |%50s\n",$subname,$line,$file
+);
}
}
This code is obviously not the neatest, but it's very nice for debugging. It simply prints out the error message, but then e-mails the stack backtrace to the emails in the global $MAIL_BOMB_LIST
-I went outside... and then I came back in!!!!
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, 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, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|