From: Shawn M Moore Date: Sat, 28 Mar 2009 19:58:55 +0000 (-0400) Subject: rebless_instance fixes and no-metaing X-Git-Tag: 0.80~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d79475639b77744b12611f47de2eecfbf3f87e20;p=gitmo%2FClass-MOP.git rebless_instance fixes and no-metaing --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 0801cb7..482797e 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -405,22 +405,16 @@ sub clone_instance { sub rebless_instance { my ($self, $instance, %params) = @_; - my $old_metaclass; - if ($instance->can('meta')) { - ($instance->meta->isa('Class::MOP::Class')) - || confess 'Cannot rebless instance if ->meta is not an instance of Class::MOP::Class'; - $old_metaclass = $instance->meta; - } - else { - $old_metaclass = $self->initialize(blessed($instance)); - } + my $old_metaclass = Class::MOP::class_of($instance); - $old_metaclass->rebless_instance_away($instance, $self, %params); + my $old_class = $old_metaclass ? $old_metaclass->name : blessed($instance); + $self->name->isa($old_class) + || confess "You may rebless only into a subclass of ($old_class), of which (". $self->name .") isn't."; - my $meta_instance = $self->get_meta_instance(); + $old_metaclass->rebless_instance_away($instance, $self, %params) + if $old_metaclass; - $self->name->isa($old_metaclass->name) - || confess "You may rebless only into a subclass of (". $old_metaclass->name ."), of which (". $self->name .") isn't."; + my $meta_instance = $self->get_meta_instance(); # rebless! # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8