X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FMeta%2FMethod%2FDestructor.pm;h=45bc3ee7a605599c40bbb76bb8b216e1a42371b8;hb=efa728b4984ddf1611bc9931fbc209438459652c;hp=499ba6b1e1c4b857f563913f6d1c01d2500f892c;hpb=d03bd989b97597428b460d7f9a021e2931893fa0;p=gitmo%2FMoose.git diff --git a/lib/Moose/Meta/Method/Destructor.pm b/lib/Moose/Meta/Method/Destructor.pm index 499ba6b..45bc3ee 100644 --- a/lib/Moose/Meta/Method/Destructor.pm +++ b/lib/Moose/Meta/Method/Destructor.pm @@ -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.75_01'; +our $VERSION = '1.15'; $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,43 @@ sub _initialize_body { my @DEMOLISH_methods = $self->associated_metaclass->find_all_methods_by_name('DEMOLISH'); - return unless @DEMOLISH_methods; + 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"; - my $source = 'sub {'; + 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 @DEMOLISH_calls; - foreach my $method (@DEMOLISH_methods) { - push @DEMOLISH_calls => '$_[0]->' . $method->{class} . '::DEMOLISH()'; } - $source .= join ";\n" => @DEMOLISH_calls; + $source .= '}'; - $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; } @@ -128,7 +149,7 @@ L I L. =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: @@ -159,13 +180,17 @@ of its parents defines a C method, it needs a destructor. =back +=head1 BUGS + +See L for details on reporting bugs. + =head1 AUTHORS Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE -Copyright 2006-2009 by Infinity Interactive, Inc. +Copyright 2006-2010 by Infinity Interactive, Inc. L