Checking in changes prior to tagging of version 0.40_09. Changelog diff is:
[gitmo/Mouse.git] / lib / Mouse / Meta / Module.pm
CommitLineData
3a63a2e7 1package Mouse::Meta::Module;
7d96ae4d 2use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings
3a63a2e7 3
b397e301 4use Carp ();
5use Scalar::Util ();
fce211ae 6
fe1221ad 7my %METAS;
8536d351 8
8aba926d 9# XXX: work around a warning "useless use of a constant in void context" in 5.6.2
ecd4a125 10if(&Mouse::Util::MOUSE_XS){
fd168725 11 # register meta storage for performance
12 Mouse::Util::__register_metaclass_storage(\%METAS, 0);
13
14 # ensure thread safety
15 *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) };
16}
17
deb9a0f3 18sub _metaclass_cache { # DEPRECATED
739525d0 19 my($class, $name) = @_;
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
3a683350 82 Carp::cluck('get_attribute_map() has been deprecated');
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
92
01afd8ff 93# XXX: for backward compatibility
94my %foreign = map{ $_ => undef } qw(
95 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
81bc0675 96 Carp Scalar::Util List::Util
01afd8ff 97);
98sub _code_is_mine{
81bc0675 99# my($self, $code) = @_;
8e64d0fa 100
81bc0675 101 return !exists $foreign{ get_code_package($_[1]) };
3a63a2e7 102}
103
3e44140b 104sub add_method;
105
3a63a2e7 106sub has_method {
107 my($self, $method_name) = @_;
108
78dc5ed1 109 defined($method_name)
110 or $self->throw_error('You must define a method name');
111
81bc0675 112 return defined($self->{methods}{$method_name}) || do{
113 my $code = get_code_ref($self->{package}, $method_name);
114 $code && $self->_code_is_mine($code);
115 };
3a63a2e7 116}
117
81bc0675 118sub get_method_body {
71e7b544 119 my($self, $method_name) = @_;
120
121 defined($method_name)
122 or $self->throw_error('You must define a method name');
123
124 return $self->{methods}{$method_name} ||= do{
7d96ae4d 125 my $code = get_code_ref($self->{package}, $method_name);
81bc0675 126 $code && $self->_code_is_mine($code) ? $code : undef;
71e7b544 127 };
128}
129
8536d351 130sub get_method{
6cfa1e5e 131 my($self, $method_name) = @_;
132
81bc0675 133 if(my $code = $self->get_method_body($method_name)){
6cfa1e5e 134 my $method_metaclass = $self->method_metaclass;
135 load_class($method_metaclass);
136
9010458d 137 return $method_metaclass->wrap(
81bc0675 138 body => $code,
9010458d 139 name => $method_name,
60e446e8 140 package => $self->name,
9010458d 141 associated_metaclass => $self,
6cfa1e5e 142 );
143 }
144
145 return undef;
8536d351 146}
3a63a2e7 147
8e64d0fa 148sub get_method_list {
3a63a2e7 149 my($self) = @_;
8e64d0fa 150
151 return grep { $self->has_method($_) } keys %{ $self->namespace };
3a63a2e7 152}
153
7a50b450 154{
155 my $ANON_SERIAL = 0;
7a50b450 156
157 my %IMMORTALS;
158
159 sub create {
7eb3a8d5 160 my($self, $package_name, %options) = @_;
7a50b450 161
7eb3a8d5 162 my $class = ref($self) || $self;
163 $self->throw_error('You must pass a package name') if @_ < 2;
7a50b450 164
8f40866c 165 my $superclasses;
7a50b450 166 if(exists $options{superclasses}){
f48920c1 167 if(Mouse::Util::is_a_metarole($self)){
7a50b450 168 delete $options{superclasses};
169 }
8f40866c 170 else{
171 $superclasses = delete $options{superclasses};
172 (ref $superclasses eq 'ARRAY')
7eb3a8d5 173 || $self->throw_error("You must pass an ARRAY ref of superclasses");
8f40866c 174 }
7a50b450 175 }
176
8f40866c 177 my $attributes = delete $options{attributes};
178 if(defined $attributes){
179 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
7eb3a8d5 180 || $self->throw_error("You must pass an ARRAY ref of attributes");
8f40866c 181 }
182 my $methods = delete $options{methods};
183 if(defined $methods){
184 (ref $methods eq 'HASH')
7eb3a8d5 185 || $self->throw_error("You must pass a HASH ref of methods");
8f40866c 186 }
187 my $roles = delete $options{roles};
188 if(defined $roles){
189 (ref $roles eq 'ARRAY')
7eb3a8d5 190 || $self->throw_error("You must pass an ARRAY ref of roles");
8f40866c 191 }
7a50b450 192 my $mortal;
193 my $cache_key;
194
195 if(!defined $package_name){ # anonymous
196 $mortal = !$options{cache};
197
198 # anonymous but immortal
199 if(!$mortal){
8e64d0fa 200 # something like Super::Class|Super::Class::2=Role|Role::1
201 $cache_key = join '=' => (
8f40866c 202 join('|', @{$superclasses || []}),
203 join('|', sort @{$roles || []}),
7a50b450 204 );
205 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
206 }
8f40866c 207 $options{anon_serial_id} = ++$ANON_SERIAL;
208 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
7a50b450 209 }
210
211 # instantiate a module
212 {
213 no strict 'refs';
214 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
215 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
216 }
217
7eb3a8d5 218 my $meta = $self->initialize( $package_name, %options);
7a50b450 219
b397e301 220 Scalar::Util::weaken $METAS{$package_name}
7a50b450 221 if $mortal;
222
7eb3a8d5 223 $meta->add_method(meta => sub{
224 $self->initialize(ref($_[0]) || $_[0]);
7a50b450 225 });
226
8f40866c 227 $meta->superclasses(@{$superclasses})
228 if defined $superclasses;
7a50b450 229
230 # NOTE:
231 # process attributes first, so that they can
232 # install accessors, but locally defined methods
233 # can then overwrite them. It is maybe a little odd, but
234 # I think this should be the order of things.
235 if (defined $attributes) {
236 if(ref($attributes) eq 'ARRAY'){
8f40866c 237 # array of Mouse::Meta::Attribute
7a50b450 238 foreach my $attr (@{$attributes}) {
8f40866c 239 $meta->add_attribute($attr);
7a50b450 240 }
241 }
242 else{
8f40866c 243 # hash map of name and attribute spec pairs
7a50b450 244 while(my($name, $attr) = each %{$attributes}){
245 $meta->add_attribute($name => $attr);
246 }
247 }
248 }
8f40866c 249 if (defined $methods) {
250 while(my($method_name, $method_body) = each %{$methods}){
251 $meta->add_method($method_name, $method_body);
7a50b450 252 }
253 }
8f40866c 254 if (defined $roles){
255 Mouse::Util::apply_all_roles($package_name, @{$roles});
7a50b450 256 }
257
8f40866c 258 if($cache_key){
7a50b450 259 $IMMORTALS{$cache_key} = $meta;
260 }
261
262 return $meta;
263 }
264
265 sub DESTROY{
266 my($self) = @_;
267
39a8df63 268 return if $Mouse::Util::in_global_destruction;
269
7a50b450 270 my $serial_id = $self->{anon_serial_id};
271
272 return if !$serial_id;
273
e75f21db 274 # @ISA is a magical variable, so we clear it manually.
275 @{$self->{superclasses}} = () if exists $self->{superclasses};
276
277 # Then, clear the symbol table hash
278 %{$self->namespace} = ();
7a50b450 279
8f40866c 280 my $name = $self->name;
281 delete $METAS{$name};
282
e75f21db 283 $name =~ s/ $serial_id \z//xms;
7a50b450 284
285 no strict 'refs';
8f40866c 286 delete ${$name}{ $serial_id . '::' };
7a50b450 287
288 return;
289 }
290}
291
8536d351 292sub throw_error{
293 my($class, $message, %args) = @_;
294
fce211ae 295 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
8e64d0fa 296 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
3a63a2e7 297
8536d351 298 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
299 Carp::croak($message);
300 }
301 else{
302 Carp::confess($message);
303 }
304}
3a63a2e7 305
3061;
307
308__END__
309
310=head1 NAME
311
1820fffe 312Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
3a63a2e7 313
a25ca8d6 314=head1 VERSION
315
506fb74d 316This document describes Mouse version 0.40_09
a25ca8d6 317
b995be6a 318=head1 SEE ALSO
319
31c5194b 320L<Class::MOP::Class>
321
322L<Class::MOP::Module>
323
324L<Class::MOP::Package>
b995be6a 325
3a63a2e7 326=cut
2cea7a5f 327