We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / lib / Moose / Meta / Method / Destructor.pm
index b310a72..e06a13d 100644 (file)
@@ -6,11 +6,7 @@ use warnings;
 
 use Devel::GlobalDestruction ();
 use Scalar::Util 'blessed', 'weaken';
-use Try::Tiny ();
-
-our $VERSION   = '0.99';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
+use Try::Tiny;
 
 use base 'Moose::Meta::Method',
          'Class::MOP::Method::Inlined';
@@ -32,6 +28,7 @@ sub new {
         'name'                 => $options{name},
         # ...
         'options'              => $options{options},
+        'definition_context'   => $options{definition_context},
         'associated_metaclass' => $options{metaclass},
     } => $class;
 
@@ -78,78 +75,86 @@ sub _initialize_body {
     # requires some adaption on the part of
     # the author, after all, nothing is free)
 
-    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 .= 'local $?;' . "\n";
-
-        $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 .= '}';
-        $source .= q[ Try::Tiny::catch { no warnings 'misc'; die $_ };] . "\n";
-        $source .= 'return;' . "\n";
+    my $class = $self->associated_metaclass->name;
+    my @source = (
+        'sub {',
+            'my $self = shift;',
+            'return ' . $self->_generate_fallback_destructor('$self'),
+                'if Scalar::Util::blessed($self) ne \'' . $class . '\';',
+            $self->_generate_DEMOLISHALL('$self'),
+            'return;',
+        '}',
+    );
+    warn join("\n", @source) if $self->options->{debug};
 
+    my $code = try {
+        $self->_compile_code(source => \@source);
     }
+    catch {
+        my $source = join("\n", @source);
+        $self->throw_error(
+            "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$_",
+            error => $_,
+            data  => $source,
+        );
+    };
 
-    $source .= '}';
-
-    warn $source if $self->options->{debug};
+    $self->{'body'} = $code;
+}
 
-    my ( $code, $e ) = $self->_compile_code(
-        environment => {},
-        code => $source,
-    );
+sub _generate_fallback_destructor {
+    my $self = shift;
+    my ($inv) = @_;
 
-    $self->throw_error(
-        "Could not eval the destructor :\n\n$source\n\nbecause :\n\n$e",
-        error => $e, data => $source )
-        if $e;
+    return $inv . '->Moose::Object::DESTROY(@_)';
+}
 
-    $self->{'body'} = $code;
+sub _generate_DEMOLISHALL {
+    my $self = shift;
+    my ($inv) = @_;
+
+    my @methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH');
+    return unless @methods;
+
+    return (
+        'local $?;',
+        'my $igd = Devel::GlobalDestruction::in_global_destruction;',
+        'Try::Tiny::try {',
+            (map { $inv . '->' . $_->{class} . '::DEMOLISH($igd);' } @methods),
+        '}',
+        'Try::Tiny::catch {',
+            'die $_;',
+        '};',
+    );
 }
 
 
 1;
 
+# ABSTRACT: Method Meta Object for destructors
+
 __END__
 
 =pod
 
-=head1 NAME
-
-Moose::Meta::Method::Destructor - Method Meta Object for destructors
-
 =head1 DESCRIPTION
 
-This class is a subclass of L<Class::MOP::Class::Generated> that
+This class is a subclass of L<Class::MOP::Method::Inlined> that
 provides Moose-specific functionality for inlining destructors.
 
 To understand this class, you should read the the
-L<Class::MOP::Class::Generated> documentation as well.
+L<Class::MOP::Method::Inlined> documentation as well.
 
 =head1 INHERITANCE
 
 C<Moose::Meta::Method::Destructor> is a subclass of
-L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Generated>.
+L<Moose::Meta::Method> I<and> L<Class::MOP::Method::Inlined>.
 
 =head1 METHODS
 
 =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:
 
@@ -184,18 +189,5 @@ of its parents defines a C<DEMOLISH> method, it needs a destructor.
 
 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-2010 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
-
 =cut