From: Yuval Kogman Date: Wed, 13 Aug 2008 21:18:57 +0000 (+0000) Subject: immutable constructor for method X-Git-Tag: 0_64_01~28 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=71b98d4fae80b55fd5b03142fae05b625b03d328;p=gitmo%2FClass-MOP.git immutable constructor for method --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 20ca98e..46a7e2c 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -516,25 +516,6 @@ Class::MOP::Method->meta->add_attribute( )) ); -# FIMXE prime candidate for immutablization -Class::MOP::Method->meta->add_method('wrap' => sub { - my ( $class, @args ) = @_; - - unshift @args, 'body' if @args % 2 == 1; - - my %options = @args; - my $code = $options{body}; - - ('CODE' eq ref($code)) - || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")"; - - ($options{package_name} && $options{name}) - || confess "You must supply the package_name and name parameters"; - - # return the new object - $class->meta->new_object(%options); -}); - Class::MOP::Method->meta->add_method('clone' => sub { my $self = shift; $self->meta->clone_object($self, @_); diff --git a/lib/Class/MOP/Method.pm b/lib/Class/MOP/Method.pm index 8a2dc88..ab56ffc 100644 --- a/lib/Class/MOP/Method.pm +++ b/lib/Class/MOP/Method.pm @@ -42,18 +42,24 @@ sub wrap { ($params{package_name} && $params{name}) || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT"; - my $self = bless { - 'body' => $code, - 'associated_metaclass' => $params{associated_metaclass}, - 'package_name' => $params{package_name}, - 'name' => $params{name}, - } => ref($class) || $class; + my $self = (ref($class) || $class)->_new(%params); weaken($self->{associated_metaclass}) if $self->{associated_metaclass}; return $self; } +sub _new { + my ( $class, %params ) = @_; + + my $self = bless { + 'body' => $params{body}, + 'associated_metaclass' => $params{associated_metaclass}, + 'package_name' => $params{package_name}, + 'name' => $params{name}, + } => $class; +} + ## accessors sub body { (shift)->{'body'} }