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
9 # XXX: work around a warning "useless use of a constant in void context" in 5.6.2
10 if(&Mouse::Util::MOUSE_XS){
11 # register meta storage for performance
12 Mouse::Util::__register_metaclass_storage(\%METAS, 0);
14 # ensure thread safety
15 *CLONE = sub { Mouse::Util::__register_metaclass_storage(\%METAS, 1) };
18 sub _metaclass_cache { # DEPRECATED
19 my($class, $name) = @_;
24 my($class, $package_name, @args) = @_;
26 ($package_name && !ref($package_name))
27 || $class->throw_error("You must pass a package name and it cannot be blessed");
29 return $METAS{$package_name}
30 ||= $class->_construct_meta(package => $package_name, @args);
34 my($class, $package_name, @args) = @_;
36 $package_name = $package_name->name if ref $package_name;
38 ($package_name && !ref($package_name))
39 || $class->throw_error("You must pass a package name and it cannot be blessed");
41 delete $METAS{$package_name};
42 return $class->initialize($package_name, @args);
46 my($class_or_instance) = @_;
47 return undef unless defined $class_or_instance;
48 return $METAS{ ref($class_or_instance) || $class_or_instance };
51 # Means of accessing all the metaclasses that have
52 # been initialized thus far
53 #sub _get_all_metaclasses { %METAS }
54 sub _get_all_metaclass_instances { values %METAS }
55 sub _get_all_metaclass_names { keys %METAS }
56 sub _get_metaclass_by_name { $METAS{$_[0]} }
57 #sub _store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
58 #sub _weaken_metaclass { weaken($METAS{$_[0]}) }
59 #sub _does_metaclass_exist { defined $METAS{$_[0]} }
60 #sub _remove_metaclass_by_name { delete $METAS{$_[0]} }
66 # The followings are Class::MOP specific methods
68 #sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
69 #sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
72 # return join '-' => (
74 # ($self->version || ()),
75 # ($self->authority || ()),
79 # add_attribute is an abstract method
81 sub get_attribute_map { # DEPRECATED
82 Carp::cluck('get_attribute_map() has been deprecated');
83 return $_[0]->{attributes};
86 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
87 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
88 sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
90 sub get_attribute_list{ keys %{$_[0]->{attributes}} }
92 # XXX: for backward compatibility
93 my %foreign = map{ $_ => undef } qw(
94 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
95 Carp Scalar::Util List::Util
98 # my($self, $code) = @_;
100 return !exists $foreign{ get_code_package($_[1]) };
106 my($self, $method_name) = @_;
108 defined($method_name)
109 or $self->throw_error('You must define a method name');
111 return defined($self->{methods}{$method_name}) || do{
112 my $code = get_code_ref($self->{package}, $method_name);
113 $code && $self->_code_is_mine($code);
117 sub get_method_body {
118 my($self, $method_name) = @_;
120 defined($method_name)
121 or $self->throw_error('You must define a method name');
123 return $self->{methods}{$method_name} ||= do{
124 my $code = get_code_ref($self->{package}, $method_name);
125 $code && $self->_code_is_mine($code) ? $code : undef;
130 my($self, $method_name) = @_;
132 if(my $code = $self->get_method_body($method_name)){
133 my $method_metaclass = $self->method_metaclass;
134 load_class($method_metaclass);
136 return $method_metaclass->wrap(
138 name => $method_name,
139 package => $self->name,
140 associated_metaclass => $self,
147 sub get_method_list {
150 return grep { $self->has_method($_) } keys %{ $self->namespace };
159 my($self, $package_name, %options) = @_;
161 my $class = ref($self) || $self;
162 $self->throw_error('You must pass a package name') if @_ < 2;
165 if(exists $options{superclasses}){
166 if(Mouse::Util::is_a_metarole($self)){
167 delete $options{superclasses};
170 $superclasses = delete $options{superclasses};
171 (ref $superclasses eq 'ARRAY')
172 || $self->throw_error("You must pass an ARRAY ref of superclasses");
176 my $attributes = delete $options{attributes};
177 if(defined $attributes){
178 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
179 || $self->throw_error("You must pass an ARRAY ref of attributes");
181 my $methods = delete $options{methods};
182 if(defined $methods){
183 (ref $methods eq 'HASH')
184 || $self->throw_error("You must pass a HASH ref of methods");
186 my $roles = delete $options{roles};
188 (ref $roles eq 'ARRAY')
189 || $self->throw_error("You must pass an ARRAY ref of roles");
194 if(!defined $package_name){ # anonymous
195 $mortal = !$options{cache};
197 # anonymous but immortal
199 # something like Super::Class|Super::Class::2=Role|Role::1
200 $cache_key = join '=' => (
201 join('|', @{$superclasses || []}),
202 join('|', sort @{$roles || []}),
204 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
206 $options{anon_serial_id} = ++$ANON_SERIAL;
207 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
210 # instantiate a module
213 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
214 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
217 my $meta = $self->initialize( $package_name, %options);
219 Scalar::Util::weaken $METAS{$package_name}
222 $meta->add_method(meta => sub{
223 $self->initialize(ref($_[0]) || $_[0]);
226 $meta->superclasses(@{$superclasses})
227 if defined $superclasses;
230 # process attributes first, so that they can
231 # install accessors, but locally defined methods
232 # can then overwrite them. It is maybe a little odd, but
233 # I think this should be the order of things.
234 if (defined $attributes) {
235 if(ref($attributes) eq 'ARRAY'){
236 # array of Mouse::Meta::Attribute
237 foreach my $attr (@{$attributes}) {
238 $meta->add_attribute($attr);
242 # hash map of name and attribute spec pairs
243 while(my($name, $attr) = each %{$attributes}){
244 $meta->add_attribute($name => $attr);
248 if (defined $methods) {
249 while(my($method_name, $method_body) = each %{$methods}){
250 $meta->add_method($method_name, $method_body);
254 Mouse::Util::apply_all_roles($package_name, @{$roles});
258 $IMMORTALS{$cache_key} = $meta;
267 return if $Mouse::Util::in_global_destruction;
269 my $serial_id = $self->{anon_serial_id};
271 return if !$serial_id;
273 # @ISA is a magical variable, so we clear it manually.
274 @{$self->{superclasses}} = () if exists $self->{superclasses};
276 # Then, clear the symbol table hash
277 %{$self->namespace} = ();
279 my $name = $self->name;
280 delete $METAS{$name};
282 $name =~ s/ $serial_id \z//xms;
285 delete ${$name}{ $serial_id . '::' };
292 my($class, $message, %args) = @_;
294 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
295 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
297 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
298 Carp::croak($message);
301 Carp::confess($message);
310 Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
314 This document describes Mouse version 0.43
320 L<Class::MOP::Module>
322 L<Class::MOP::Package>