1 package Mouse::Meta::Module;
2 use Mouse::Util qw/:meta get_code_package get_code_ref load_class not_supported/; # enables strict and warnings
4 use Mouse::Util::TypeConstraints ();
11 sub _metaclass_cache { # DEPRECATED
12 my($class, $name) = @_;
17 my($class, $package_name, @args) = @_;
19 ($package_name && !ref($package_name))
20 || $class->throw_error("You must pass a package name and it cannot be blessed");
22 return $METAS{$package_name}
23 ||= $class->_construct_meta(package => $package_name, @args);
27 my($class_or_instance) = @_;
28 return undef unless defined $class_or_instance;
29 return $METAS{ ref($class_or_instance) || $class_or_instance };
32 # Means of accessing all the metaclasses that have
33 # been initialized thus far
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]} }
47 # The followings are Class::MOP specific methods
49 #sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
50 #sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
53 # return join '-' => (
55 # ($self->version || ()),
56 # ($self->authority || ()),
60 # add_attribute is an abstract method
62 sub get_attribute_map { # DEPRECATED
63 Carp::cluck('get_attribute_map() has been deprecated');
64 return $_[0]->{attributes};
67 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
68 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
69 sub get_attribute_list{ keys %{$_[0]->{attributes}} }
70 sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
72 # XXX: for backward compatibility
73 my %foreign = map{ $_ => undef } qw(
74 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
75 Carp Scalar::Util List::Util
78 # my($self, $code) = @_;
80 return !exists $foreign{ get_code_package($_[1]) };
86 my($self, $method_name) = @_;
89 or $self->throw_error('You must define a method name');
91 return defined($self->{methods}{$method_name}) || do{
92 my $code = get_code_ref($self->{package}, $method_name);
93 $code && $self->_code_is_mine($code);
98 my($self, $method_name) = @_;
100 defined($method_name)
101 or $self->throw_error('You must define a method name');
103 return $self->{methods}{$method_name} ||= do{
104 my $code = get_code_ref($self->{package}, $method_name);
105 $code && $self->_code_is_mine($code) ? $code : undef;
110 my($self, $method_name) = @_;
112 if(my $code = $self->get_method_body($method_name)){
113 my $method_metaclass = $self->method_metaclass;
114 load_class($method_metaclass);
116 return $method_metaclass->wrap(
118 name => $method_name,
119 package => $self->name,
120 associated_metaclass => $self,
127 sub get_method_list {
130 return grep { $self->has_method($_) } keys %{ $self->namespace };
139 my($self, $package_name, %options) = @_;
141 my $class = ref($self) || $self;
142 $self->throw_error('You must pass a package name') if @_ < 2;
145 if(exists $options{superclasses}){
146 if(Mouse::Util::is_a_metarole($self)){
147 delete $options{superclasses};
150 $superclasses = delete $options{superclasses};
151 (ref $superclasses eq 'ARRAY')
152 || $self->throw_error("You must pass an ARRAY ref of superclasses");
156 my $attributes = delete $options{attributes};
157 if(defined $attributes){
158 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
159 || $self->throw_error("You must pass an ARRAY ref of attributes");
161 my $methods = delete $options{methods};
162 if(defined $methods){
163 (ref $methods eq 'HASH')
164 || $self->throw_error("You must pass a HASH ref of methods");
166 my $roles = delete $options{roles};
168 (ref $roles eq 'ARRAY')
169 || $self->throw_error("You must pass an ARRAY ref of roles");
174 if(!defined $package_name){ # anonymous
175 $mortal = !$options{cache};
177 # anonymous but immortal
179 # something like Super::Class|Super::Class::2=Role|Role::1
180 $cache_key = join '=' => (
181 join('|', @{$superclasses || []}),
182 join('|', sort @{$roles || []}),
184 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
186 $options{anon_serial_id} = ++$ANON_SERIAL;
187 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
190 # instantiate a module
193 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
194 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
197 my $meta = $self->initialize( $package_name, %options);
199 Scalar::Util::weaken $METAS{$package_name}
202 $meta->add_method(meta => sub{
203 $self->initialize(ref($_[0]) || $_[0]);
206 $meta->superclasses(@{$superclasses})
207 if defined $superclasses;
210 # process attributes first, so that they can
211 # install accessors, but locally defined methods
212 # can then overwrite them. It is maybe a little odd, but
213 # I think this should be the order of things.
214 if (defined $attributes) {
215 if(ref($attributes) eq 'ARRAY'){
216 # array of Mouse::Meta::Attribute
217 foreach my $attr (@{$attributes}) {
218 $meta->add_attribute($attr);
222 # hash map of name and attribute spec pairs
223 while(my($name, $attr) = each %{$attributes}){
224 $meta->add_attribute($name => $attr);
228 if (defined $methods) {
229 while(my($method_name, $method_body) = each %{$methods}){
230 $meta->add_method($method_name, $method_body);
234 Mouse::Util::apply_all_roles($package_name, @{$roles});
238 $IMMORTALS{$cache_key} = $meta;
247 my $serial_id = $self->{anon_serial_id};
249 return if !$serial_id;
251 # @ISA is a magical variable, so we clear it manually.
252 @{$self->{superclasses}} = () if exists $self->{superclasses};
254 # Then, clear the symbol table hash
255 %{$self->namespace} = ();
257 my $name = $self->name;
258 delete $METAS{$name};
260 $name =~ s/ $serial_id \z//xms;
263 delete ${$name}{ $serial_id . '::' };
270 my($class, $message, %args) = @_;
272 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
273 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
275 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
276 Carp::croak($message);
279 Carp::confess($message);
289 Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
293 This document describes Mouse version 0.40_03
299 L<Class::MOP::Module>
301 L<Class::MOP::Package>