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