inline Devel::GlobalDestruction pure perl code temporarily
[gitmo/Moo.git] / lib / Method / Generate / DemolishAll.pm
index 0126680..5d45656 100644 (file)
@@ -2,6 +2,7 @@ package Method::Generate::DemolishAll;
 
 use strictures 1;
 use base qw(Moo::Object);
+use Devel::GlobalDestruction ();
 use Sub::Quote;
 use Moo::_Utils;
 use B qw(perlstring);
@@ -13,6 +14,21 @@ sub generate_method {
     qq{    my \$self = shift;\n},
     $self->demolishall_body_for($into, '$self', '@_'),
     qq{    return \$self\n};
+  quote_sub "${into}::DESTROY", join '',
+    q!    my $self = shift;
+    my $e = do {
+      local $?;
+      local $@;
+      require Moo::_Utils;
+      eval {
+        $self->DEMOLISHALL(Moo::_Utils::_in_global_destruction);
+      };
+      $@;
+    };
+  
+    no warnings 'misc';
+    die $e if $e; # rethrow
+  !;
 }
 
 sub demolishall_body_for {