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