in reply to Exceptions and Return Codes
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:
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#--------------------------------------------------- # 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 +); } }
-I went outside... and then I came back in!!!!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Exceptions and Return Codes
by Abigail (Deacon) on Jun 29, 2001 at 18:13 UTC |
In Section
Seekers of Perl Wisdom