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