X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMoose%2FObject.pm;h=77026113a43d8ce8b6530f2de5135adbc92a4c1c;hb=01fd70ed5d41bcfa561c198b172c594bc3256760;hp=191e7cb0ea53c593d3958c1828d1ee68cff2d13e;hpb=75d2da3428d70afc00efb0f295fd0656a05d6b9f;p=gitmo%2FMoose.git diff --git a/lib/Moose/Object.pm b/lib/Moose/Object.pm index 191e7cb..7702611 100644 --- a/lib/Moose/Object.pm +++ b/lib/Moose/Object.pm @@ -4,23 +4,26 @@ package Moose::Object; use strict; use warnings; -use Scalar::Util; use Devel::GlobalDestruction qw(in_global_destruction); +use MRO::Compat; +use Scalar::Util; +use Try::Tiny; use if ( not our $__mx_is_compiled ), 'Moose::Meta::Class'; use if ( not our $__mx_is_compiled ), metaclass => 'Moose::Meta::Class'; -our $VERSION = '0.77'; +our $VERSION = '0.89_02'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; sub new { my $class = shift; + Carp::cluck 'Calling new() on an instance is deprecated,' + . ' please use (blessed $obj)->new' if blessed $class; + my $params = $class->BUILDARGS(@_); - # We want to support passing $self->new, but initialize - # takes only an unblessed class name my $real_class = Scalar::Util::blessed($class) || $class; my $self = Class::MOP::Class->initialize($real_class)->new_object($params); @@ -64,30 +67,42 @@ sub DEMOLISHALL { # extra meta level calls return unless $self->can('DEMOLISH'); - # This is a hack, because Moose::Meta::Class may not be the right - # metaclass, but class_of may return undef during global - # destruction, if the metaclass object has already been cleaned - # up. - my $meta = Class::MOP::class_of($self) - || Moose::Meta::Class->initialize( ref $self ); + my @isa; + if ( my $meta = Class::MOP::class_of($self ) ) { + @isa = $meta->linearized_isa; + } else { + # We cannot count on being able to retrieve a previously made + # metaclass, _or_ being able to make a new one during global + # destruction. However, we should still be able to use mro at + # that time (at least tests suggest so ;) + my $class_name = ref $self; + @isa = @{ mro::get_linear_isa($class_name) } + } - foreach my $method ( $meta->find_all_methods_by_name('DEMOLISH') ) { - $method->{code}->execute($self, $in_global_destruction); + foreach my $class (@isa) { + no strict 'refs'; + my $demolish = *{"${class}::DEMOLISH"}{CODE}; + $self->$demolish($in_global_destruction) + if defined $demolish; } } sub DESTROY { - # if we have an exception here ... - if ($@) { - # localize the $@ ... - local $@; - # run DEMOLISHALL ourselves, ... - $_[0]->DEMOLISHALL(in_global_destruction); - # and return ... - return; + my $self = shift; + + local $?; + + try { + $self->DEMOLISHALL(in_global_destruction); } - # otherwise it is normal destruction - $_[0]->DEMOLISHALL(in_global_destruction); + catch { + # Without this, Perl will warn "\t(in cleanup)$@" because of some + # bizarre fucked-up logic deep in the internals. + no warnings 'misc'; + die $_; + }; + + return; } # support for UNIVERSAL::DOES ...