From: Jesse Luehrs Date: Sun, 26 Jul 2009 02:17:06 +0000 (-0500) Subject: clone and rebless when wrapping a method object X-Git-Tag: 0.92~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6db5c459d895adc8cea8f285f26aca1ea3c3cd14;p=gitmo%2FClass-MOP.git clone and rebless when wrapping a method object --- diff --git a/lib/Class/MOP/Method.pm b/lib/Class/MOP/Method.pm index 58971e6..8041994 100644 --- a/lib/Class/MOP/Method.pm +++ b/lib/Class/MOP/Method.pm @@ -5,7 +5,7 @@ use strict; use warnings; use Carp 'confess'; -use Scalar::Util 'weaken', 'reftype'; +use Scalar::Util 'weaken', 'reftype', 'blessed'; our $VERSION = '0.90'; $VERSION = eval $VERSION; @@ -28,8 +28,15 @@ sub wrap { my %params = @args; my $code = $params{body}; - (ref $code && 'CODE' eq reftype($code)) - || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")"; + if (blessed($code) && $code->isa(__PACKAGE__)) { + my $method = $code->clone; + delete $params{body}; + Class::MOP::class_of($class)->rebless_instance($method, %params); + return $method; + } + elsif (!ref $code || 'CODE' ne reftype($code)) { + confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")"; + } ($params{package_name} && $params{name}) || confess "You must supply the package_name and name parameters";