bump version to 0.92
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
index 19a35b9..c714b2b 100644 (file)
@@ -4,14 +4,16 @@ package Moose::Meta::Method::Destructor;
 use strict;
 use warnings;
 
+use Devel::GlobalDestruction ();
 use Scalar::Util 'blessed', 'weaken';
+use Try::Tiny ();
 
-our $VERSION   = '0.78';
+our $VERSION   = '0.92';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
 use base 'Moose::Meta::Method',
-         'Class::MOP::Method::Generated';
+         'Class::MOP::Method::Inlined';
 
 sub new {
     my $class   = shift;
@@ -57,7 +59,7 @@ sub is_needed {
         || $self->throw_error(
         "The is_needed method expected a metaclass object as its arugment");
 
-    return $metaclass->find_method_by_name('DEMOLISH');
+    return $metaclass->find_method_by_name("DEMOLISHALL");
 }
 
 sub initialize_body {
@@ -78,24 +80,40 @@ sub _initialize_body {
 
     my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
 
-    return unless @DEMOLISH_methods;
+    my $source;
+    if ( @DEMOLISH_methods ) {
+        $source = 'sub {';
+        $source .= 'my $self = shift;' . "\n";
 
-    my $source = 'sub {';
+        $source .= 'local $?;' . "\n";
 
-    my @DEMOLISH_calls;
-    foreach my $method (@DEMOLISH_methods) {
-        push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()';
-    }
+        $source .= 'my $in_global_destruction = Devel::GlobalDestruction::in_global_destruction;' . "\n";
+
+        $source .= 'Try::Tiny::try {' . "\n";
+
+        $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 .= '}';
+    } else {
+        $source = 'sub { }';
+    }
 
-    $source .= ";\n" . '}';
     warn $source if $self->options->{debug};
 
-    my $code = $self->_compile_code(
+    my ( $code, $e ) = $self->_compile_code(
         environment => {},
         code => $source,
-    ) or $self->throw_error("Could not eval the destructor :\n\n$source\n\nbecause :\n\n$@", error => $@, data => $source);
+    );
+
+    $self->throw_error(
+        "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$e",
+        error => $e, data => $source )
+        if $e;
 
     $self->{'body'} = $code;
 }