Make Mouse::Util::load_class return the argument class name, which can remove several...
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
CommitLineData
3a63a2e7 1package Mouse::Meta::Module;
637d4f17 2use Mouse::Util qw/:meta get_code_package get_code_ref not_supported/; # enables strict and warnings
3a63a2e7 3
b397e301 4use Carp ();
5use Scalar::Util ();
fce211ae 6
fe1221ad 7my %METAS;
8536d351 8
029463f4 9if(Mouse::Util::MOUSE_XS){
fd168725 10 # register meta storage for performance
11 Mouse::Util::__register_metaclass_storage(\%METAS, 0);
12
13 # ensure thread safety
14 *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) };
15}
16
deb9a0f3 17sub _metaclass_cache { # DEPRECATED
739525d0 18 my($class, $name) = @_;
8b0573c6 19 Carp::cluck('_metaclass_cache() has been deprecated. Use Mouse::Util::get_metaclass_by_name() instead');
fe1221ad 20 return $METAS{$name};
739525d0 21}
8536d351 22
739525d0 23sub initialize {
24 my($class, $package_name, @args) = @_;
8536d351 25
739525d0 26 ($package_name && !ref($package_name))
27 || $class->throw_error("You must pass a package name and it cannot be blessed");
8536d351 28
fe1221ad 29 return $METAS{$package_name}
739525d0 30 ||= $class->_construct_meta(package => $package_name, @args);
31}
8536d351 32
542f20ad 33sub reinitialize {
34 my($class, $package_name, @args) = @_;
35
f87debb9 36 $package_name = $package_name->name if ref $package_name;
37
542f20ad 38 ($package_name && !ref($package_name))
39 || $class->throw_error("You must pass a package name and it cannot be blessed");
40
41 delete $METAS{$package_name};
42 return $class->initialize($package_name, @args);
43}
44
45sub _class_of{
739525d0 46 my($class_or_instance) = @_;
47 return undef unless defined $class_or_instance;
23274b88 48 return $METAS{ ref($class_or_instance) || $class_or_instance };
8536d351 49}
50
739525d0 51# Means of accessing all the metaclasses that have
52# been initialized thus far
542f20ad 53#sub _get_all_metaclasses { %METAS }
54sub _get_all_metaclass_instances { values %METAS }
55sub _get_all_metaclass_names { keys %METAS }
56sub _get_metaclass_by_name { $METAS{$_[0]} }
57#sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
58#sub _weaken_metaclass { weaken($METAS{$_[0]}) }
59#sub _does_metaclass_exist { defined $METAS{$_[0]} }
60#sub _remove_metaclass_by_name { delete $METAS{$_[0]} }
739525d0 61
43165725 62sub name;
3a63a2e7 63
2591e962 64sub namespace;
65
739525d0 66# The followings are Class::MOP specific methods
67
68#sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
69#sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
70#sub identifier {
71# my $self = shift;
72# return join '-' => (
73# $self->name,
74# ($self->version || ()),
75# ($self->authority || ()),
76# );
77#}
23264b5b 78
8536d351 79# add_attribute is an abstract method
80
deb9a0f3 81sub get_attribute_map { # DEPRECATED
8b0573c6 82 Carp::cluck('get_attribute_map() has been deprecated. Use get_attribute_list() and get_attribute() instead');
3a683350 83 return $_[0]->{attributes};
84}
85
8536d351 86sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
87sub get_attribute { $_[0]->{attributes}->{$_[1]} }
6cfa1e5e 88sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
23264b5b 89
047d7af0 90sub get_attribute_list{ keys %{$_[0]->{attributes}} }
91
01afd8ff 92# XXX: for backward compatibility
93my %foreign = map{ $_ => undef } qw(
94 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
81bc0675 95 Carp Scalar::Util List::Util
01afd8ff 96);
97sub _code_is_mine{
81bc0675 98# my($self, $code) = @_;
8e64d0fa 99
81bc0675 100 return !exists $foreign{ get_code_package($_[1]) };
3a63a2e7 101}
102
3e44140b 103sub add_method;
104
3a63a2e7 105sub has_method {
106 my($self, $method_name) = @_;
107
78dc5ed1 108 defined($method_name)
109 or $self->throw_error('You must define a method name');
110
81bc0675 111 return defined($self->{methods}{$method_name}) || do{
112 my $code = get_code_ref($self->{package}, $method_name);
113 $code && $self->_code_is_mine($code);
114 };
3a63a2e7 115}
116
81bc0675 117sub get_method_body {
71e7b544 118 my($self, $method_name) = @_;
119
120 defined($method_name)
121 or $self->throw_error('You must define a method name');
122
123 return $self->{methods}{$method_name} ||= do{
7d96ae4d 124 my $code = get_code_ref($self->{package}, $method_name);
81bc0675 125 $code && $self->_code_is_mine($code) ? $code : undef;
71e7b544 126 };
127}
128
8536d351 129sub get_method{
6cfa1e5e 130 my($self, $method_name) = @_;
131
81bc0675 132 if(my $code = $self->get_method_body($method_name)){
637d4f17 133 return Mouse::Util::load_class($self->method_metaclass)->wrap(
81bc0675 134 body => $code,
9010458d 135 name => $method_name,
60e446e8 136 package => $self->name,
9010458d 137 associated_metaclass => $self,
6cfa1e5e 138 );
139 }
140
141 return undef;
8536d351 142}
3a63a2e7 143
8e64d0fa 144sub get_method_list {
3a63a2e7 145 my($self) = @_;
8e64d0fa 146
147 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 148}
149
013ee5f0 150sub _collect_methods { # Mouse specific
151 my($meta, @args) = @_;
152
153 my @methods;
154 foreach my $arg(@args){
155 if(my $type = ref $arg){
156 if($type eq 'Regexp'){
157 push @methods, grep { $_ =~ $arg } $meta->get_all_method_names;
158 }
159 elsif($type eq 'ARRAY'){
160 push @methods, @{$arg};
161 }
162 else{
163 my $subname = ( caller(1) )[3];
164 $meta->throw_error(
165 sprintf(
166 'Methods passed to %s must be provided as a list, ArrayRef or regular expression, not %s',
167 $subname,
168 $type,
169 )
170 );
171 }
172 }
173 else{
174 push @methods, $arg;
175 }
176 }
177 return @methods;
178}
179
180
7a50b450 181{
182 my $ANON_SERIAL = 0;
7a50b450 183
184 my %IMMORTALS;
185
186 sub create {
7eb3a8d5 187 my($self, $package_name, %options) = @_;
7a50b450 188
7eb3a8d5 189 my $class = ref($self) || $self;
190 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 191
8f40866c 192 my $superclasses;
7a50b450 193 if(exists $options{superclasses}){
f48920c1 194 if(Mouse::Util::is_a_metarole($self)){
7a50b450 195 delete $options{superclasses};
196 }
8f40866c 197 else{
198 $superclasses = delete $options{superclasses};
199 (ref $superclasses eq 'ARRAY')
7eb3a8d5 200 || $self->throw_error("You must pass an ARRAY ref of superclasses");
8f40866c 201 }
7a50b450 202 }
203
8f40866c 204 my $attributes = delete $options{attributes};
205 if(defined $attributes){
206 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
7eb3a8d5 207 || $self->throw_error("You must pass an ARRAY ref of attributes");
8f40866c 208 }
209 my $methods = delete $options{methods};
210 if(defined $methods){
211 (ref $methods eq 'HASH')
7eb3a8d5 212 || $self->throw_error("You must pass a HASH ref of methods");
8f40866c 213 }
214 my $roles = delete $options{roles};
215 if(defined $roles){
216 (ref $roles eq 'ARRAY')
7eb3a8d5 217 || $self->throw_error("You must pass an ARRAY ref of roles");
8f40866c 218 }
7a50b450 219 my $mortal;
220 my $cache_key;
221
222 if(!defined $package_name){ # anonymous
223 $mortal = !$options{cache};
224
225 # anonymous but immortal
226 if(!$mortal){
8e64d0fa 227 # something like Super::Class|Super::Class::2=Role|Role::1
228 $cache_key = join '=' => (
8f40866c 229 join('|', @{$superclasses || []}),
230 join('|', sort @{$roles || []}),
7a50b450 231 );
232 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
233 }
8f40866c 234 $options{anon_serial_id} = ++$ANON_SERIAL;
235 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
7a50b450 236 }
237
238 # instantiate a module
239 {
240 no strict 'refs';
241 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
242 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
243 }
244
7eb3a8d5 245 my $meta = $self->initialize( $package_name, %options);
7a50b450 246
b397e301 247 Scalar::Util::weaken $METAS{$package_name}
7a50b450 248 if $mortal;
249
7eb3a8d5 250 $meta->add_method(meta => sub{
251 $self->initialize(ref($_[0]) || $_[0]);
7a50b450 252 });
253
8f40866c 254 $meta->superclasses(@{$superclasses})
255 if defined $superclasses;
7a50b450 256
257 # NOTE:
258 # process attributes first, so that they can
259 # install accessors, but locally defined methods
260 # can then overwrite them. It is maybe a little odd, but
261 # I think this should be the order of things.
262 if (defined $attributes) {
263 if(ref($attributes) eq 'ARRAY'){
8f40866c 264 # array of Mouse::Meta::Attribute
7a50b450 265 foreach my $attr (@{$attributes}) {
8f40866c 266 $meta->add_attribute($attr);
7a50b450 267 }
268 }
269 else{
8f40866c 270 # hash map of name and attribute spec pairs
7a50b450 271 while(my($name, $attr) = each %{$attributes}){
272 $meta->add_attribute($name => $attr);
273 }
274 }
275 }
8f40866c 276 if (defined $methods) {
277 while(my($method_name, $method_body) = each %{$methods}){
278 $meta->add_method($method_name, $method_body);
7a50b450 279 }
280 }
8f40866c 281 if (defined $roles){
282 Mouse::Util::apply_all_roles($package_name, @{$roles});
7a50b450 283 }
284
8f40866c 285 if($cache_key){
7a50b450 286 $IMMORTALS{$cache_key} = $meta;
287 }
288
289 return $meta;
290 }
291
292 sub DESTROY{
293 my($self) = @_;
294
39a8df63 295 return if $Mouse::Util::in_global_destruction;
296
7a50b450 297 my $serial_id = $self->{anon_serial_id};
298
299 return if !$serial_id;
300
e75f21db 301 # @ISA is a magical variable, so we clear it manually.
302 @{$self->{superclasses}} = () if exists $self->{superclasses};
303
304 # Then, clear the symbol table hash
305 %{$self->namespace} = ();
7a50b450 306
8f40866c 307 my $name = $self->name;
308 delete $METAS{$name};
309
e75f21db 310 $name =~ s/ $serial_id \z//xms;
7a50b450 311
312 no strict 'refs';
8f40866c 313 delete ${$name}{ $serial_id . '::' };
7a50b450 314
315 return;
316 }
317}
318
8536d351 319sub throw_error{
320 my($class, $message, %args) = @_;
321
fce211ae 322 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
8e64d0fa 323 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
3a63a2e7 324
8536d351 325 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
326 Carp::croak($message);
327 }
328 else{
329 Carp::confess($message);
330 }
331}
3a63a2e7 332
3331;
3a63a2e7 334__END__
335
336=head1 NAME
337
1820fffe 338Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 339
a25ca8d6 340=head1 VERSION
341
4bc73e47 342This document describes Mouse version 0.50_03
a25ca8d6 343
b995be6a 344=head1 SEE ALSO
345
31c5194b 346L<Class::MOP::Class>
347
348L<Class::MOP::Module>
349
350L<Class::MOP::Package>
b995be6a 351
3a63a2e7 352=cut
2cea7a5f 353