1 package Mouse::Meta::Module;
2 use Mouse::Util qw/:meta get_code_package load_class not_supported/; # enables strict and warnings
5 use Scalar::Util qw/blessed weaken/;
9 sub _metaclass_cache { # DEPRECATED
10 my($class, $name) = @_;
15 my($class, $package_name, @args) = @_;
17 ($package_name && !ref($package_name))
18 || $class->throw_error("You must pass a package name and it cannot be blessed");
20 return $METAS{$package_name}
21 ||= $class->_construct_meta(package => $package_name, @args);
25 my($class_or_instance) = @_;
26 return undef unless defined $class_or_instance;
27 return $METAS{ ref($class_or_instance) || $class_or_instance };
30 # Means of accessing all the metaclasses that have
31 # been initialized thus far
32 #sub get_all_metaclasses { %METAS }
33 sub get_all_metaclass_instances { values %METAS }
34 sub get_all_metaclass_names { keys %METAS }
35 sub get_metaclass_by_name { $METAS{$_[0]} }
36 #sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
37 #sub weaken_metaclass { weaken($METAS{$_[0]}) }
38 #sub does_metaclass_exist { defined $METAS{$_[0]} }
39 #sub remove_metaclass_by_name { delete $METAS{$_[0]} }
45 # The followings are Class::MOP specific methods
47 #sub version { no strict 'refs'; ${shift->name.'::VERSION'} }
48 #sub authority { no strict 'refs'; ${shift->name.'::AUTHORITY'} }
51 # return join '-' => (
53 # ($self->version || ()),
54 # ($self->authority || ()),
58 # add_attribute is an abstract method
60 sub get_attribute_map { # DEPRECATED
61 Carp::cluck('get_attribute_map() has been deprecated');
62 return $_[0]->{attributes};
65 sub has_attribute { exists $_[0]->{attributes}->{$_[1]} }
66 sub get_attribute { $_[0]->{attributes}->{$_[1]} }
67 sub get_attribute_list{ keys %{$_[0]->{attributes}} }
68 sub remove_attribute { delete $_[0]->{attributes}->{$_[1]} }
71 my($self, $name, $code) = @_;
74 $self->throw_error('You must pass a defined name');
77 $self->throw_error('You must pass a defined code');
80 if(ref($code) ne 'CODE'){
81 $code = \&{$code}; # coerce
84 $self->{methods}->{$name} = $code; # Moose stores meta object here.
86 my $pkg = $self->name;
88 no warnings 'redefine', 'once';
89 *{ $pkg . '::' . $name } = $code;
92 # XXX: for backward compatibility
93 my %foreign = map{ $_ => undef } qw(
94 Mouse Mouse::Role Mouse::Util Mouse::Util::TypeConstraints
98 my($self, $code) = @_;
100 my $package = get_code_package($code);
102 return !exists $foreign{$package};
106 my($self, $method_name) = @_;
108 defined($method_name)
109 or $self->throw_error('You must define a method name');
111 return 1 if $self->{methods}{$method_name};
116 *{ $self->{package} . '::' . $method_name }{CODE};
119 return $code && $self->_code_is_mine($code);
123 my($self, $method_name) = @_;
125 defined($method_name)
126 or $self->throw_error('You must define a method name');
128 return $self->{methods}{$method_name} ||= do{
132 *{$self->{package} . '::' . $method_name}{CODE};
135 ($code && $self->_code_is_mine($code)) ? $code : undef;
140 my($self, $method_name) = @_;
142 if($self->has_method($method_name)){
143 my $method_metaclass = $self->method_metaclass;
144 load_class($method_metaclass);
146 return $method_metaclass->wrap(
147 body => $self->get_method_body($method_name),
148 name => $method_name,
149 package => $self->name,
150 associated_metaclass => $self,
157 sub get_method_list {
160 return grep { $self->has_method($_) } keys %{ $self->namespace };
169 my($self, $package_name, %options) = @_;
171 my $class = ref($self) || $self;
172 $self->throw_error('You must pass a package name') if @_ < 2;
175 if(exists $options{superclasses}){
176 if($self->isa('Mouse::Meta::Role')){
177 delete $options{superclasses};
180 $superclasses = delete $options{superclasses};
181 (ref $superclasses eq 'ARRAY')
182 || $self->throw_error("You must pass an ARRAY ref of superclasses");
186 my $attributes = delete $options{attributes};
187 if(defined $attributes){
188 (ref $attributes eq 'ARRAY' || ref $attributes eq 'HASH')
189 || $self->throw_error("You must pass an ARRAY ref of attributes");
191 my $methods = delete $options{methods};
192 if(defined $methods){
193 (ref $methods eq 'HASH')
194 || $self->throw_error("You must pass a HASH ref of methods");
196 my $roles = delete $options{roles};
198 (ref $roles eq 'ARRAY')
199 || $self->throw_error("You must pass an ARRAY ref of roles");
204 if(!defined $package_name){ # anonymous
205 $mortal = !$options{cache};
207 # anonymous but immortal
209 # something like Super::Class|Super::Class::2=Role|Role::1
210 $cache_key = join '=' => (
211 join('|', @{$superclasses || []}),
212 join('|', sort @{$roles || []}),
214 return $IMMORTALS{$cache_key} if exists $IMMORTALS{$cache_key};
216 $options{anon_serial_id} = ++$ANON_SERIAL;
217 $package_name = $class . '::__ANON__::' . $ANON_SERIAL;
220 # instantiate a module
223 ${ $package_name . '::VERSION' } = delete $options{version} if exists $options{version};
224 ${ $package_name . '::AUTHORITY' } = delete $options{authority} if exists $options{authority};
227 my $meta = $self->initialize( $package_name, %options);
229 weaken $METAS{$package_name}
232 $meta->add_method(meta => sub{
233 $self->initialize(ref($_[0]) || $_[0]);
236 $meta->superclasses(@{$superclasses})
237 if defined $superclasses;
240 # process attributes first, so that they can
241 # install accessors, but locally defined methods
242 # can then overwrite them. It is maybe a little odd, but
243 # I think this should be the order of things.
244 if (defined $attributes) {
245 if(ref($attributes) eq 'ARRAY'){
246 # array of Mouse::Meta::Attribute
247 foreach my $attr (@{$attributes}) {
248 $meta->add_attribute($attr);
252 # hash map of name and attribute spec pairs
253 while(my($name, $attr) = each %{$attributes}){
254 $meta->add_attribute($name => $attr);
258 if (defined $methods) {
259 while(my($method_name, $method_body) = each %{$methods}){
260 $meta->add_method($method_name, $method_body);
264 Mouse::Util::apply_all_roles($package_name, @{$roles});
268 $IMMORTALS{$cache_key} = $meta;
277 my $serial_id = $self->{anon_serial_id};
279 return if !$serial_id;
281 # @ISA is a magical variable, so we clear it manually.
282 @{$self->{superclasses}} = () if exists $self->{superclasses};
284 # Then, clear the symbol table hash
285 %{$self->namespace} = ();
287 my $name = $self->name;
288 delete $METAS{$name};
290 $name =~ s/ $serial_id \z//xms;
293 delete ${$name}{ $serial_id . '::' };
300 my($class, $message, %args) = @_;
302 local $Carp::CarpLevel = $Carp::CarpLevel + 1 + ($args{depth} || 0);
303 local $Carp::MaxArgNums = 20; # default is 8, usually we use named args which gets messier though
305 if(exists $args{longmess} && !$args{longmess}){ # intentionaly longmess => 0
306 Carp::croak($message);
309 Carp::confess($message);
319 Mouse::Meta::Module - The base class for Mouse::Meta::Class and Mouse::Meta::Role
323 This document describes Mouse version 0.40
329 L<Class::MOP::Module>
331 L<Class::MOP::Package>