X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FModule.pm;h=ab2fd6a92c2e4ca832989a69a0e375f6bc744bf6;hp=9dbe85dd197806cba8742b825687f25903c862ee;hb=1e5823974a557bd35b4aa4a5c1d1aecf1e5483d2;hpb=745220df2da2256a9bd2692ac585f39b35ed19df diff --git a/lib/Mouse/Meta/Module.pm b/lib/Mouse/Meta/Module.pm index 9dbe85d..ab2fd6a 100755 --- a/lib/Mouse/Meta/Module.pm +++ b/lib/Mouse/Meta/Module.pm @@ -1,13 +1,19 @@ package Mouse::Meta::Module; use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings -use Mouse::Util::TypeConstraints (); - -use Carp (); -use Scalar::Util qw/blessed weaken/; +use Carp (); +use Scalar::Util (); my %METAS; +if(Mouse::Util::_MOUSE_XS){ + # register meta storage for performance + Mouse::Util::__register_metaclass_storage(\%METAS, 0); + + # ensure thread safety + *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) }; +} + sub _metaclass_cache { # DEPRECATED my($class, $name) = @_; return $METAS{$name}; @@ -66,58 +72,37 @@ sub get_attribute_map { # DEPRECATED sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } sub get_attribute { $_[0]->{attributes}->{$_[1]} } -sub get_attribute_list{ keys %{$_[0]->{attributes}} } sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} } -sub add_method { - my($self, $name, $code) = @_; - - if(!defined $name){ - $self->throw_error('You must pass a defined name'); - } - if(!defined $code){ - $self->throw_error('You must pass a defined code'); - } - - if(ref($code) ne 'CODE'){ - $code = \&{$code}; # coerce - } +sub get_attribute_list{ keys %{$_[0]->{attributes}} } - $self->{methods}->{$name} = $code; # Moose stores meta object here. - - my $pkg = $self->name; - no strict 'refs'; - no warnings 'redefine', 'once'; - *{ $pkg . '::' . $name } = $code; -} # XXX: for backward compatibility my %foreign = map{ $_ => undef } qw( Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints - Carp Scalar::Util + Carp Scalar::Util List::Util ); sub _code_is_mine{ - my($self, $code) = @_; +# my($self, $code) = @_; - my $package = get_code_package($code); - - return !exists $foreign{$package}; + return !exists $foreign{ get_code_package($_[1]) }; } +sub add_method; + sub has_method { my($self, $method_name) = @_; defined($method_name) or $self->throw_error('You must define a method name'); - return 1 if $self->{methods}{$method_name}; - - my $code = get_code_ref($self->{package}, $method_name); - - return $code && $self->_code_is_mine($code); + return defined($self->{methods}{$method_name}) || do{ + my $code = get_code_ref($self->{package}, $method_name); + $code && $self->_code_is_mine($code); + }; } -sub get_method_body{ +sub get_method_body { my($self, $method_name) = @_; defined($method_name) @@ -125,19 +110,19 @@ sub get_method_body{ return $self->{methods}{$method_name} ||= do{ my $code = get_code_ref($self->{package}, $method_name); - ($code && $self->_code_is_mine($code)) ? $code : undef; + $code && $self->_code_is_mine($code) ? $code : undef; }; } sub get_method{ my($self, $method_name) = @_; - if($self->has_method($method_name)){ + if(my $code = $self->get_method_body($method_name)){ my $method_metaclass = $self->method_metaclass; load_class($method_metaclass); return $method_metaclass->wrap( - body => $self->get_method_body($method_name), + body => $code, name => $method_name, package => $self->name, associated_metaclass => $self, @@ -166,7 +151,7 @@ sub get_method_list { my $superclasses; if(exists $options{superclasses}){ - if(Mouse::Util::TypeConstraints::_is_a_metarole($self)){ + if(Mouse::Util::is_a_metarole($self)){ delete $options{superclasses}; } else{ @@ -219,7 +204,7 @@ sub get_method_list { my $meta = $self->initialize( $package_name, %options); - weaken $METAS{$package_name} + Scalar::Util::weaken $METAS{$package_name} if $mortal; $meta->add_method(meta => sub{ @@ -313,7 +298,7 @@ Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Rol =head1 VERSION -This document describes Mouse version 0.40_01 +This document describes Mouse version 0.40_06 =head1 SEE ALSO