1 package Mouse::Meta::Module;
6 use Scalar::Util qw/blessed weaken/;
8 use Mouse::Util qw/:meta get_code_package not_supported load_class/;
13 # because Mouse doesn't introspect existing classes, we're forced to
14 # only pay attention to other Mouse classes
15 sub _metaclass_cache {
16 my($class, $name) = @_;
21 my($class, $package_name, @args) = @_;
23 ($package_name && !ref($package_name))
24 || $class->throw_error("You must pass a package name and it cannot be blessed");
26 return $METAS{$package_name}
27 ||= $class->_construct_meta(package => $package_name, @args);
31 my($class_or_instance) = @_;
32 return undef unless defined $class_or_instance;
33 return $METAS{ ref($class_or_instance) || $class_or_instance };
36 # Means of accessing all the metaclasses that have
37 # been initialized thus far
38 #sub get_all_metaclasses { %METAS }
39 sub get_all_metaclass_instances { values %METAS }
40 sub get_all_metaclass_names { keys %METAS }
41 sub get_metaclass_by_name { $METAS{$_[0]} }
42 #sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
43 #sub weaken_metaclass { weaken($METAS{$_[0]}) }
44 #sub does_metaclass_exist { defined $METAS{$_[0]} }
45 #sub remove_metaclass_by_name { delete $METAS{$_[0]} }
49 sub name { $_[0]->{package} }
51 # The followings are Class::MOP specific methods
53 #sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
54 #sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
57 # return join '-' => (
59 # ($self->version || ()),
60 # ($self->authority || ()),
64 # add_attribute is an abstract method
66 sub get_attribute_map {
67 Carp::cluck('get_attribute_map() has been deprecated');
68 return $_[0]->{attributes};
71 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
72 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
73 sub get_attribute_list{ keys %{$_[0]->{attributes}} }
74 sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
77 my $name = $_[0]->{package};
79 return \%{ $name . '::' };
83 my($self, $name, $code) = @_;
86 $self->throw_error('You must pass a defined name');
89 $self->throw_error('You must pass a defined code');
92 if(ref($code) ne 'CODE'){
93 not_supported 'add_method for a method object';
96 $self->{methods}->{$name}++; # Moose stores meta object here.
98 my $pkg = $self->name;
100 no warnings 'redefine';
101 *{ $pkg . '::' . $name } = $code;
104 # XXX: for backward compatibility
105 my %foreign = map{ $_ => undef } qw(
106 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
110 my($self, $code) = @_;
112 my $package = get_code_package($code);
114 return !exists $foreign{$package};
118 my($self, $method_name) = @_;
120 return 1 if $self->{methods}->{$method_name};
122 my $code = do{ no strict 'refs'; *{$self->{package} . '::' . $method_name}{CODE} };
124 return $code && $self->_code_is_mine($code);
128 my($self, $method_name) = @_;
130 if($self->has_method($method_name)){
131 my $method_metaclass = $self->method_metaclass;
132 load_class($method_metaclass);
134 my $package = $self->name;
135 return $method_metaclass->new(
136 body => $package->can($method_name),
137 name => $method_name,
145 sub get_method_list {
148 return grep { $self->has_method($_) } keys %{ $self->namespace };
157 my($class, $package_name, %options) = @_;
159 $class->throw_error('You must pass a package name') if @_ < 2;
162 if(exists $options{superclasses}){
163 if($class->isa('Mouse::Meta::Role')){
164 delete $options{superclasses};
167 $superclasses = delete $options{superclasses};
168 (ref $superclasses eq 'ARRAY')
169 || $class->throw_error("You must pass an ARRAY ref of superclasses");
173 my $attributes = delete $options{attributes};
174 if(defined $attributes){
175 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
176 || $class->throw_error("You must pass an ARRAY ref of attributes");
178 my $methods = delete $options{methods};
179 if(defined $methods){
180 (ref $methods eq 'HASH')
181 || $class->throw_error("You must pass a HASH ref of methods");
183 my $roles = delete $options{roles};
185 (ref $roles eq 'ARRAY')
186 || $class->throw_error("You must pass an ARRAY ref of roles");
191 if(!defined $package_name){ # anonymous
192 $mortal = !$options{cache};
194 # anonymous but immortal
196 # something like Super::Class|Super::Class::2=Role|Role::1
197 $cache_key = join '=' => (
198 join('|', @{$superclasses || []}),
199 join('|', sort @{$roles || []}),
201 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
203 $options{anon_serial_id} = ++$ANON_SERIAL;
204 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
207 # instantiate a module
210 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
211 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
214 my $meta = $class->initialize( $package_name, %options);
216 weaken $METAS{$package_name}
220 $meta->add_method('meta' => sub {
221 $class->initialize(ref($_[0]) || $_[0]);
224 $meta->superclasses(@{$superclasses})
225 if defined $superclasses;
228 # process attributes first, so that they can
229 # install accessors, but locally defined methods
230 # can then overwrite them. It is maybe a little odd, but
231 # I think this should be the order of things.
232 if (defined $attributes) {
233 if(ref($attributes) eq 'ARRAY'){
234 # array of Mouse::Meta::Attribute
235 foreach my $attr (@{$attributes}) {
236 $meta->add_attribute($attr);
240 # hash map of name and attribute spec pairs
241 while(my($name, $attr) = each %{$attributes}){
242 $meta->add_attribute($name => $attr);
246 if (defined $methods) {
247 while(my($method_name, $method_body) = each %{$methods}){
248 $meta->add_method($method_name, $method_body);
252 Mouse::Util::apply_all_roles($package_name, @{$roles});
256 $IMMORTALS{$cache_key} = $meta;
265 my $serial_id = $self->{anon_serial_id};
267 return if !$serial_id;
269 @{$self->{superclasses}} = (); # clear @ISA
270 %{$self->namespace} = (); # clear the stash
272 my $name = $self->name;
273 delete $METAS{$name};
279 delete ${$name}{ $serial_id . '::' };
286 my($class, $message, %args) = @_;
288 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
289 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
291 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
292 Carp::croak($message);
295 Carp::confess($message);
305 Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
311 L<Class::MOP::Module>
313 L<Class::MOP::Package>