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 qw/:meta get_code_package get_code_ref not_supported/; # enables strict and warnings
-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) = @_;
+ Carp::cluck('_metaclass_cache() has been deprecated. Use Mouse::Util::get_metaclass_by_name() instead');
return $METAS{$name};
}
||= $class->_construct_meta(package => $package_name, @args);
}
-sub class_of{
+sub reinitialize {
+ my($class, $package_name, @args) = @_;
+
+ $package_name = $package_name->name if ref $package_name;
+
+ ($package_name && !ref($package_name))
+ || $class->throw_error("You must pass a package name and it cannot be blessed");
+
+ delete $METAS{$package_name};
+ return $class->initialize($package_name, @args);
+}
+
+sub _class_of{
my($class_or_instance) = @_;
return undef unless defined $class_or_instance;
return $METAS{ ref($class_or_instance) || $class_or_instance };
# Means of accessing all the metaclasses that have
# been initialized thus far
-#sub get_all_metaclasses { %METAS }
-sub get_all_metaclass_instances { values %METAS }
-sub get_all_metaclass_names { keys %METAS }
-sub get_metaclass_by_name { $METAS{$_[0]} }
-#sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
-#sub weaken_metaclass { weaken($METAS{$_[0]}) }
-#sub does_metaclass_exist { defined $METAS{$_[0]} }
-#sub remove_metaclass_by_name { delete $METAS{$_[0]} }
+#sub _get_all_metaclasses { %METAS }
+sub _get_all_metaclass_instances { values %METAS }
+sub _get_all_metaclass_names { keys %METAS }
+sub _get_metaclass_by_name { $METAS{$_[0]} }
+#sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
+#sub _weaken_metaclass { weaken($METAS{$_[0]}) }
+#sub _does_metaclass_exist { defined $METAS{$_[0]} }
+#sub _remove_metaclass_by_name { delete $METAS{$_[0]} }
sub name;
# add_attribute is an abstract method
sub get_attribute_map { # DEPRECATED
- Carp::cluck('get_attribute_map() has been deprecated');
+ Carp::cluck('get_attribute_map() has been deprecated. Use get_attribute_list() and get_attribute() instead');
return $_[0]->{attributes};
}
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
- }
-
- $self->{methods}->{$name} = $code; # Moose stores meta object here.
-
- my $pkg = $self->name;
- no strict 'refs';
- no warnings 'redefine', 'once';
- *{ $pkg . '::' . $name } = $code;
-}
+sub get_attribute_list{ keys %{$_[0]->{attributes}} }
# 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 $package = get_code_package($code);
+# my($self, $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)
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)){
- my $method_metaclass = $self->method_metaclass;
- load_class($method_metaclass);
-
- return $method_metaclass->wrap(
- body => $self->get_method_body($method_name),
+ if(my $code = $self->get_method_body($method_name)){
+ return Mouse::Util::load_class($self->method_metaclass)->wrap(
+ body => $code,
name => $method_name,
package => $self->name,
associated_metaclass => $self,
return grep { $self->has_method($_) } keys %{ $self->namespace };
}
+sub _collect_methods { # Mouse specific
+ my($meta, @args) = @_;
+
+ my @methods;
+ foreach my $arg(@args){
+ if(my $type = ref $arg){
+ if($type eq 'Regexp'){
+ push @methods, grep { $_ =~ $arg } $meta->get_all_method_names;
+ }
+ elsif($type eq 'ARRAY'){
+ push @methods, @{$arg};
+ }
+ else{
+ my $subname = ( caller(1) )[3];
+ $meta->throw_error(
+ sprintf(
+ 'Methods passed to %s must be provided as a list, ArrayRef or regular expression, not %s',
+ $subname,
+ $type,
+ )
+ );
+ }
+ }
+ else{
+ push @methods, $arg;
+ }
+ }
+ return @methods;
+}
+
+
{
my $ANON_SERIAL = 0;
my $superclasses;
if(exists $options{superclasses}){
- if($self->isa('Mouse::Meta::Role')){
+ if(Mouse::Util::is_a_metarole($self)){
delete $options{superclasses};
}
else{
my $meta = $self->initialize( $package_name, %options);
- weaken $METAS{$package_name}
+ Scalar::Util::weaken $METAS{$package_name}
if $mortal;
$meta->add_method(meta => sub{
sub DESTROY{
my($self) = @_;
+ return if $Mouse::Util::in_global_destruction;
+
my $serial_id = $self->{anon_serial_id};
return if !$serial_id;
}
1;
-
__END__
=head1 NAME
=head1 VERSION
-This document describes Mouse version 0.40
+This document describes Mouse version 0.50_03
=head1 SEE ALSO