bump version to 1.19
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
index 98121fc..4cc8129 100644 (file)
@@ -4,9 +4,11 @@ package Moose::Meta::Method::Destructor;
 use strict;
 use warnings;
 
+use Devel::GlobalDestruction ();
 use Scalar::Util 'blessed', 'weaken';
+use Try::Tiny ();
 
-our $VERSION   = '0.87';
+our $VERSION   = '1.19';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -79,21 +81,31 @@ sub _initialize_body {
     my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
 
     my $source;
+    $source  = 'sub {' . "\n";
+    $source .= 'my $self = shift;' . "\n";
+    $source .= 'return $self->Moose::Object::DESTROY(@_)' . "\n";
+    $source .= '    if Scalar::Util::blessed($self) ne ';
+    $source .= "'" . $self->associated_metaclass->name . "'";
+    $source .= ';' . "\n";
+
     if ( @DEMOLISH_methods ) {
-        $source = 'sub {';
+        $source .= 'local $?;' . "\n";
+
+        $source .= 'my $in_global_destruction = Devel::GlobalDestruction::in_global_destruction;' . "\n";
+
+        $source .= 'Try::Tiny::try {' . "\n";
 
-        my @DEMOLISH_calls;
-        foreach my $method (@DEMOLISH_methods) {
-            push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
-        }
+        $source .= '$self->' . $_->{class} . '::DEMOLISH($in_global_destruction);' . "\n"
+            for @DEMOLISH_methods;
 
-        $source .= join ";\n" => @DEMOLISH_calls;
+        $source .= '}';
+        $source .= q[ Try::Tiny::catch { no warnings 'misc'; die $_ };] . "\n";
+        $source .= 'return;' . "\n";
 
-        $source .= ";\n" . '}';
-    } else {
-        $source = 'sub { }';
     }
 
+    $source .= '}';
+
     warn $source if $self->options->{debug};
 
     my ( $code, $e ) = $self->_compile_code(
@@ -137,7 +149,7 @@ L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
 
 =over 4
 
-=item B<< Moose::Meta;:Method::Destructor->new(%options) >>
+=item B<< Moose::Meta::Method::Destructor->new(%options) >>
 
 This constructs a new object. It accepts the following options:
 
@@ -168,13 +180,17 @@ of its parents defines a C<DEMOLISH> method, it needs a destructor.
 
 =back
 
+=head1 BUGS
+
+See L<Moose/BUGS> for details on reporting bugs.
+
 =head1 AUTHORS
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>