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