1 package Mouse::Meta::Module;
6 use Scalar::Util qw/blessed weaken/;
8 use Mouse::Util qw/get_code_info not_supported load_class/;
13 # because Mouse doesn't introspect existing classes, we're forced to
14 # only pay attention to other Mouse classes
15 sub _metaclass_cache {
16 my($class, $name) = @_;
17 return $METACLASS_CACHE{$name};
21 my($class, $package_name, @args) = @_;
23 ($package_name && !ref($package_name))
24 || $class->throw_error("You must pass a package name and it cannot be blessed");
26 return $METACLASS_CACHE{$package_name}
27 ||= $class->_construct_meta(package => $package_name, @args);
31 my($class_or_instance) = @_;
32 return undef unless defined $class_or_instance;
33 return $METACLASS_CACHE{ blessed($class_or_instance) || $class_or_instance };
36 # Means of accessing all the metaclasses that have
37 # been initialized thus far
38 sub get_all_metaclasses { %METACLASS_CACHE }
39 sub get_all_metaclass_instances { values %METACLASS_CACHE }
40 sub get_all_metaclass_names { keys %METACLASS_CACHE }
41 sub get_metaclass_by_name { $METACLASS_CACHE{$_[0]} }
42 sub store_metaclass_by_name { $METACLASS_CACHE{$_[0]} = $_[1] }
43 sub weaken_metaclass { weaken($METACLASS_CACHE{$_[0]}) }
44 sub does_metaclass_exist { defined $METACLASS_CACHE{$_[0]} }
45 sub remove_metaclass_by_name { delete $METACLASS_CACHE{$_[0]} }
49 sub meta{ Mouse::Meta::Class->initialize(ref $_[0] || $_[0]) }
51 sub _new{ Carp::croak("Mouse::Meta::Module is an abstract class") }
53 sub name { $_[0]->{package} }
55 sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
56 sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
61 ($self->version || ()),
62 ($self->authority || ()),
66 # add_attribute is an abstract method
68 sub get_attribute_map { $_[0]->{attributes} }
69 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
70 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
71 sub get_attribute_list{ keys %{$_[0]->{attributes}} }
72 sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
75 my $name = $_[0]->{package};
77 return \%{ $name . '::' };
81 my($self, $name, $code) = @_;
84 $self->throw_error("You must pass a defined name");
86 if(ref($code) ne 'CODE'){
87 not_supported 'add_method for a method object';
90 $self->{methods}->{$name}++; # Moose stores meta object here.
92 my $pkg = $self->name;
94 no warnings 'redefine';
95 *{ $pkg . '::' . $name } = $code;
98 sub _code_is_mine { # taken from Class::MOP::Class
99 my ( $self, $code ) = @_;
101 my ( $code_package, $code_name ) = get_code_info($code);
103 return $code_package && $code_package eq $self->name
104 || ( $code_package eq 'constant' && $code_name eq '__ANON__' );
108 my($self, $method_name) = @_;
110 return 1 if $self->{methods}->{$method_name};
111 my $code = $self->name->can($method_name);
113 return $code && $self->_code_is_mine($code);
117 my($self, $method_name) = @_;
119 if($self->has_method($method_name)){
120 my $method_metaclass = $self->method_metaclass;
121 load_class($method_metaclass);
123 my $package = $self->name;
124 return $method_metaclass->new(
125 body => $package->can($method_name),
126 name => $method_name,
134 sub get_method_list {
137 return grep { $self->has_method($_) } keys %{ $self->namespace };
142 my $ANON_PREFIX = 'Mouse::Meta::Module::__ANON__::';
147 my ($class, $package_name, %options) = @_;
149 $class->throw_error('You must pass a package name') if @_ == 1;
152 if(exists $options{superclasses}){
153 if($class->isa('Mouse::Meta::Class')){
154 (ref $options{superclasses} eq 'ARRAY')
155 || $class->throw_error("You must pass an ARRAY ref of superclasses");
158 delete $options{superclasses};
163 if(exists $options{attributes}){
164 $attributes = delete $options{attributes};
165 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
166 || $class->throw_error("You must pass an ARRAY ref of attributes")
169 (ref $options{methods} eq 'HASH')
170 || $class->throw_error("You must pass a HASH ref of methods")
171 if exists $options{methods};
173 (ref $options{roles} eq 'ARRAY')
174 || $class->throw_error("You must pass an ARRAY ref of roles")
175 if exists $options{roles};
182 if(!defined $package_name){ # anonymous
183 $mortal = !$options{cache};
185 # anonymous but immortal
187 # something like Super::Class|Super::Class::2=Role|Role::1
188 $cache_key = join '=' => (
189 join('|', @{$options{superclasses} || []}),
190 join('|', sort @{$options{roles} || []}),
192 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
194 $package_name = $ANON_PREFIX . ++$ANON_SERIAL;
196 push @extra_options, (anon_serial_id => $ANON_SERIAL);
199 # instantiate a module
202 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
203 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
206 my %initialize_options = %options;
207 delete @initialize_options{qw(
214 my $meta = $class->initialize( $package_name, %initialize_options, @extra_options);
216 Mouse::Meta::Module::weaken_metaclass($package_name)
220 $meta->add_method('meta' => sub {
221 $class->initialize(ref($_[0]) || $_[0]);
224 $meta->superclasses(@{$options{superclasses}})
225 if exists $options{superclasses};
228 # process attributes first, so that they can
229 # install accessors, but locally defined methods
230 # can then overwrite them. It is maybe a little odd, but
231 # I think this should be the order of things.
232 if (defined $attributes) {
233 if(ref($attributes) eq 'ARRAY'){
234 foreach my $attr (@{$attributes}) {
235 $meta->add_attribute($attr->{name} => $attr);
239 while(my($name, $attr) = each %{$attributes}){
240 $meta->add_attribute($name => $attr);
244 if (exists $options{methods}) {
245 foreach my $method_name (keys %{$options{methods}}) {
246 $meta->add_method($method_name, $options{methods}->{$method_name});
249 if (exists $options{roles}){
250 Mouse::Util::apply_all_roles($package_name, @{$options{roles}});
253 if(!$mortal && exists $meta->{anon_serial_id}){
254 $IMMORTALS{$cache_key} = $meta;
263 my $serial_id = $self->{anon_serial_id};
265 return if !$serial_id;
267 my $stash = $self->namespace;
269 @{$self->{superclasses}} = () if exists $self->{superclasses};
271 Mouse::Meta::Module::remove_metaclass_by_name($self->name);
274 delete ${$ANON_PREFIX}{ $serial_id . '::' };
281 my($class, $message, %args) = @_;
283 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
284 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
286 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
287 Carp::croak($message);
290 Carp::confess($message);
300 Mouse::Meta::Module - Common base class for Mouse::Meta::Class and Mouse::Meta::Role