[Mpls-pm] Code I whiteboarded tonight

Dave Rolsky autarch at urth.org
Wed Sep 14 21:09:01 PDT 2005


So I said I'd send this out.

The discussion was related to overriding $SIG{__DIE__} and how you can do 
that more or less safely.

So here's the $SIG{__DIE__} handler used by Mason:

   sub rethrow_exception {
       my ($err) = @_;
       return unless $err;

       if ( UNIVERSAL::can($err, 'rethrow') ) {
           $err->rethrow;
       }
       elsif ( ref $err ) {
           die $err;
       }
       HTML::Mason::Exception->throw(error => $err);
   }

The key here is that it's careful not to mess with refs/objects, so they 
are just passed through as is.  The rethrow() method is provided via 
Exception::Class::Base.

It's also important to note that Exception::Class::Base objects carry a 
stack trace with them, so that even after they're rethrown the stack trace 
still leads you back to where the original error occurred, not to this 
error handling code.

Also note that our idiom for overriding $SIG{__DIE__} looks more or less 
like this:

   sub foo {
       local $SIG{__DIE__} = \&rethrow_exception;

       eval {
           thing();
           other_thing();
           whatever();
       };

   }

So the override is not happening everywhere, nor does it happen simply by 
_loading_ Mason's modules.


-dave

/*===================================================
VegGuide.Org                        www.BookIRead.com
Your guide to all that's veg.       My book blog
===================================================*/


More information about the Mpls-pm mailing list