Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Exceptions and Return Codes

by Henri Icarus (Beadle)
on Jun 29, 2001 at 18:03 UTC ( [id://92616]=note: print w/replies, xml ) Need Help??


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:
#--------------------------------------------------- # 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!!!!

Replies are listed 'Best First'.
Re: Exceptions and Return Codes
by Abigail (Deacon) on Jun 29, 2001 at 18:13 UTC
    The disadvantage of using a bomb function is that it only performs the intended task if you use bomb. No CPAN module will do so. If you use a __DIE__ handler, you will catch all dies, including those from modules.

    And it has the added benefit that if someone else has to maintain the code, she won't be wondering what this bomb function is for.

    -- Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://92616]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-26 03:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found