adding in the C3 example
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Class;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
0882828e 8use Scalar::Util 'blessed', 'reftype';
8b978dd5 9use Sub::Name 'subname';
10use B 'svref_2object';
a740253a 11use Clone ();
8b978dd5 12
0b8eb325 13our $VERSION = '0.04';
8b978dd5 14
aa448b16 15# Self-introspection
2eb717d5 16
aa448b16 17sub meta { Class::MOP::Class->initialize(blessed($_[0]) || $_[0]) }
2eb717d5 18
8b978dd5 19# Creation
20
bfe4d0fc 21{
22 # Metaclasses are singletons, so we cache them here.
23 # there is no need to worry about destruction though
24 # because they should die only when the program dies.
25 # After all, do package definitions even get reaped?
651955fb 26 my %METAS;
27
bfe4d0fc 28 sub initialize {
351bd7d4 29 my $class = shift;
30 my $package_name = shift;
22286063 31 (defined $package_name && $package_name && !blessed($package_name))
32 || confess "You must pass a package name and it cannot be blessed";
651955fb 33 $class->construct_class_instance(':package' => $package_name, @_);
727919c5 34 }
35
36 # NOTE: (meta-circularity)
37 # this is a special form of &construct_instance
38 # (see below), which is used to construct class
1a7ebbb3 39 # meta-object instances for any Class::MOP::*
40 # class. All other classes will use the more
41 # normal &construct_instance.
727919c5 42 sub construct_class_instance {
351bd7d4 43 my $class = shift;
651955fb 44 my %options = @_;
45 my $package_name = $options{':package'};
727919c5 46 (defined $package_name && $package_name)
651955fb 47 || confess "You must pass a package name";
48 return $METAS{$package_name} if exists $METAS{$package_name};
1a7ebbb3 49 $class = blessed($class) || $class;
550d56db 50 # now create the metaclass
51 my $meta;
1a7ebbb3 52 if ($class =~ /^Class::MOP::/) {
550d56db 53 $meta = bless {
351bd7d4 54 '$:package' => $package_name,
55 '%:attributes' => {},
550d56db 56 '$:attribute_metaclass' => $options{':attribute_metaclass'} || 'Class::MOP::Attribute',
57 '$:method_metaclass' => $options{':method_metaclass'} || 'Class::MOP::Method',
1a7ebbb3 58 } => $class;
59 }
60 else {
5f3c057a 61 # NOTE:
62 # it is safe to use meta here because
63 # class will always be a subclass of
64 # Class::MOP::Class, which defines meta
550d56db 65 $meta = bless $class->meta->construct_instance(%options) => $class
1a7ebbb3 66 }
550d56db 67 # and check the metaclass compatibility
68 $meta->check_metaclass_compatability();
69 $METAS{$package_name} = $meta;
70 }
71
72 sub check_metaclass_compatability {
73 my $self = shift;
74
75 # this is always okay ...
76 return if blessed($self) eq 'Class::MOP::Class';
77
78 my @class_list = $self->class_precedence_list;
79 shift @class_list; # shift off $self->name
80
81 foreach my $class_name (@class_list) {
550d56db 82 my $meta = $METAS{$class_name};
83 ($self->isa(blessed($meta)))
84 || confess $self->name . "->meta => (" . (blessed($self)) . ")" .
85 " is not compatible with the " .
86 $class_name . "->meta => (" . (blessed($meta)) . ")";
87 }
bfe4d0fc 88 }
8b978dd5 89}
90
91sub create {
92 my ($class, $package_name, $package_version, %options) = @_;
bfe4d0fc 93 (defined $package_name && $package_name)
8b978dd5 94 || confess "You must pass a package name";
95 my $code = "package $package_name;";
96 $code .= "\$$package_name\:\:VERSION = '$package_version';"
97 if defined $package_version;
98 eval $code;
99 confess "creation of $package_name failed : $@" if $@;
bfe4d0fc 100 my $meta = $class->initialize($package_name);
aa448b16 101
102 $meta->add_method('meta' => sub {
103 Class::MOP::Class->initialize(blessed($_[0]) || $_[0]);
104 });
105
8b978dd5 106 $meta->superclasses(@{$options{superclasses}})
107 if exists $options{superclasses};
2eb717d5 108 # NOTE:
109 # process attributes first, so that they can
110 # install accessors, but locally defined methods
111 # can then overwrite them. It is maybe a little odd, but
112 # I think this should be the order of things.
113 if (exists $options{attributes}) {
cbd9f942 114 foreach my $attr (@{$options{attributes}}) {
115 $meta->add_attribute($attr);
2eb717d5 116 }
117 }
bfe4d0fc 118 if (exists $options{methods}) {
119 foreach my $method_name (keys %{$options{methods}}) {
120 $meta->add_method($method_name, $options{methods}->{$method_name});
121 }
2eb717d5 122 }
8b978dd5 123 return $meta;
124}
125
7b31baf4 126## Attribute readers
127
128# NOTE:
129# all these attribute readers will be bootstrapped
130# away in the Class::MOP bootstrap section
131
132sub name { $_[0]->{'$:package'} }
133sub get_attribute_map { $_[0]->{'%:attributes'} }
134sub attribute_metaclass { $_[0]->{'$:attribute_metaclass'} }
135sub method_metaclass { $_[0]->{'$:method_metaclass'} }
136
c9e77dbb 137# Instance Construction & Cloning
138
5f3c057a 139sub new_object {
140 my $class = shift;
651955fb 141 # NOTE:
142 # we need to protect the integrity of the
143 # Class::MOP::Class singletons here, so we
144 # delegate this to &construct_class_instance
145 # which will deal with the singletons
146 return $class->construct_class_instance(@_)
147 if $class->name->isa('Class::MOP::Class');
5f3c057a 148 bless $class->construct_instance(@_) => $class->name;
149}
e16da3e6 150
151sub construct_instance {
cbd9f942 152 my ($class, %params) = @_;
153 my $instance = {};
c9e77dbb 154 foreach my $attr ($class->compute_all_applicable_attributes()) {
651955fb 155 my $init_arg = $attr->init_arg();
cbd9f942 156 # try to fetch the init arg from the %params ...
157 my $val;
158 $val = $params{$init_arg} if exists $params{$init_arg};
159 # if nothing was in the %params, we can use the
160 # attribute's default value (if it has one)
c9e77dbb 161 $val ||= $attr->default($instance) if $attr->has_default();
cbd9f942 162 $instance->{$attr->name} = $val;
163 }
164 return $instance;
e16da3e6 165}
166
5f3c057a 167sub clone_object {
168 my $class = shift;
7b31baf4 169 my $instance = shift;
651955fb 170 (blessed($instance) && $instance->isa($class->name))
171 || confess "You must pass an instance ($instance) of the metaclass (" . $class->name . ")";
172 # NOTE:
173 # we need to protect the integrity of the
174 # Class::MOP::Class singletons here, they
a740253a 175 # should not be cloned.
651955fb 176 return $instance if $instance->isa('Class::MOP::Class');
177 bless $class->clone_instance($instance, @_) => blessed($instance);
5f3c057a 178}
179
c9e77dbb 180sub clone_instance {
651955fb 181 my ($class, $instance, %params) = @_;
182 (blessed($instance))
c9e77dbb 183 || confess "You can only clone instances, \$self is not a blessed instance";
184 # NOTE:
a740253a 185 # This will deep clone, which might
186 # not be what you always want. So
187 # the best thing is to write a more
188 # controled &clone method locally
189 # in the class (see Class::MOP)
190 my $clone = Clone::clone($instance);
c9e77dbb 191 foreach my $attr ($class->compute_all_applicable_attributes()) {
651955fb 192 my $init_arg = $attr->init_arg();
c9e77dbb 193 # try to fetch the init arg from the %params ...
194 $clone->{$attr->name} = $params{$init_arg}
195 if exists $params{$init_arg};
196 }
197 return $clone;
198}
199
8b978dd5 200# Informational
201
7b31baf4 202# &name should be here too, but it is above
203# because it gets bootstrapped away
8b978dd5 204
205sub version {
206 my $self = shift;
207 no strict 'refs';
208 ${$self->name . '::VERSION'};
209}
210
211# Inheritance
212
213sub superclasses {
214 my $self = shift;
215 no strict 'refs';
216 if (@_) {
217 my @supers = @_;
218 @{$self->name . '::ISA'} = @supers;
219 }
220 @{$self->name . '::ISA'};
221}
222
223sub class_precedence_list {
224 my $self = shift;
bfe4d0fc 225 # NOTE:
226 # We need to check for ciruclar inheirtance here.
227 # This will do nothing if all is well, and blow
228 # up otherwise. Yes, it's an ugly hack, better
229 # suggestions are welcome.
230 { $self->name->isa('This is a test for circular inheritance') }
231 # ... and no back to our regularly scheduled program
8b978dd5 232 (
233 $self->name,
234 map {
bfe4d0fc 235 $self->initialize($_)->class_precedence_list()
8b978dd5 236 } $self->superclasses()
237 );
238}
239
0882828e 240## Methods
241
242sub add_method {
243 my ($self, $method_name, $method) = @_;
244 (defined $method_name && $method_name)
245 || confess "You must define a method name";
a5eca695 246 # use reftype here to allow for blessed subs ...
0882828e 247 (reftype($method) && reftype($method) eq 'CODE')
248 || confess "Your code block must be a CODE reference";
249 my $full_method_name = ($self->name . '::' . $method_name);
250
251 no strict 'refs';
c9b8b7f9 252 no warnings 'redefine';
22286063 253 *{$full_method_name} = subname $full_method_name => $method;
0882828e 254}
255
663f8198 256sub alias_method {
257 my ($self, $method_name, $method) = @_;
258 (defined $method_name && $method_name)
259 || confess "You must define a method name";
260 # use reftype here to allow for blessed subs ...
261 (reftype($method) && reftype($method) eq 'CODE')
262 || confess "Your code block must be a CODE reference";
263 my $full_method_name = ($self->name . '::' . $method_name);
264
265 no strict 'refs';
266 no warnings 'redefine';
267 *{$full_method_name} = $method;
268}
269
bfe4d0fc 270{
271
272 ## private utility functions for has_method
2eb717d5 273 my $_find_subroutine_package_name = sub { eval { svref_2object($_[0])->GV->STASH->NAME } || '' };
274 my $_find_subroutine_name = sub { eval { svref_2object($_[0])->GV->NAME } || '' };
bfe4d0fc 275
276 sub has_method {
c9b8b7f9 277 my ($self, $method_name) = @_;
bfe4d0fc 278 (defined $method_name && $method_name)
279 || confess "You must define a method name";
0882828e 280
bfe4d0fc 281 my $sub_name = ($self->name . '::' . $method_name);
0882828e 282
bfe4d0fc 283 no strict 'refs';
284 return 0 if !defined(&{$sub_name});
285 return 0 if $_find_subroutine_package_name->(\&{$sub_name}) ne $self->name &&
286 $_find_subroutine_name->(\&{$sub_name}) ne '__ANON__';
287 return 1;
288 }
289
0882828e 290}
291
292sub get_method {
c9b8b7f9 293 my ($self, $method_name) = @_;
0882828e 294 (defined $method_name && $method_name)
295 || confess "You must define a method name";
296
297 no strict 'refs';
298 return \&{$self->name . '::' . $method_name}
bfe4d0fc 299 if $self->has_method($method_name);
c9b8b7f9 300 return; # <- make sure to return undef
301}
302
303sub remove_method {
304 my ($self, $method_name) = @_;
305 (defined $method_name && $method_name)
306 || confess "You must define a method name";
307
308 my $removed_method = $self->get_method($method_name);
309
310 no strict 'refs';
311 delete ${$self->name . '::'}{$method_name}
312 if defined $removed_method;
313
314 return $removed_method;
315}
316
317sub get_method_list {
318 my $self = shift;
319 no strict 'refs';
a5eca695 320 grep { $self->has_method($_) } %{$self->name . '::'};
321}
322
323sub compute_all_applicable_methods {
324 my $self = shift;
325 my @methods;
326 # keep a record of what we have seen
327 # here, this will handle all the
328 # inheritence issues because we are
329 # using the &class_precedence_list
330 my (%seen_class, %seen_method);
331 foreach my $class ($self->class_precedence_list()) {
332 next if $seen_class{$class};
333 $seen_class{$class}++;
334 # fetch the meta-class ...
335 my $meta = $self->initialize($class);
336 foreach my $method_name ($meta->get_method_list()) {
337 next if exists $seen_method{$method_name};
338 $seen_method{$method_name}++;
339 push @methods => {
340 name => $method_name,
341 class => $class,
342 code => $meta->get_method($method_name)
343 };
344 }
345 }
346 return @methods;
347}
348
a5eca695 349sub find_all_methods_by_name {
350 my ($self, $method_name) = @_;
351 (defined $method_name && $method_name)
352 || confess "You must define a method name to find";
353 my @methods;
354 # keep a record of what we have seen
355 # here, this will handle all the
356 # inheritence issues because we are
357 # using the &class_precedence_list
358 my %seen_class;
359 foreach my $class ($self->class_precedence_list()) {
360 next if $seen_class{$class};
361 $seen_class{$class}++;
362 # fetch the meta-class ...
aa448b16 363 my $meta = $self->initialize($class);;
a5eca695 364 push @methods => {
365 name => $method_name,
366 class => $class,
367 code => $meta->get_method($method_name)
368 } if $meta->has_method($method_name);
369 }
370 return @methods;
371
8b978dd5 372}
373
552e3d24 374## Attributes
375
e16da3e6 376sub add_attribute {
2e41896e 377 my $self = shift;
378 # either we have an attribute object already
379 # or we need to create one from the args provided
380 my $attribute = blessed($_[0]) ? $_[0] : $self->attribute_metaclass->new(@_);
381 # make sure it is derived from the correct type though
382 ($attribute->isa('Class::MOP::Attribute'))
383 || confess "Your attribute must be an instance of Class::MOP::Attribute (or a subclass)";
9ec169fe 384 $attribute->attach_to_class($self);
385 $attribute->install_accessors();
7b31baf4 386 $self->get_attribute_map->{$attribute->name} = $attribute;
e16da3e6 387}
388
389sub has_attribute {
390 my ($self, $attribute_name) = @_;
391 (defined $attribute_name && $attribute_name)
392 || confess "You must define an attribute name";
7b31baf4 393 exists $self->get_attribute_map->{$attribute_name} ? 1 : 0;
e16da3e6 394}
395
396sub get_attribute {
397 my ($self, $attribute_name) = @_;
398 (defined $attribute_name && $attribute_name)
399 || confess "You must define an attribute name";
7b31baf4 400 return $self->get_attribute_map->{$attribute_name}
22286063 401 if $self->has_attribute($attribute_name);
402 return;
e16da3e6 403}
404
405sub remove_attribute {
406 my ($self, $attribute_name) = @_;
407 (defined $attribute_name && $attribute_name)
408 || confess "You must define an attribute name";
7b31baf4 409 my $removed_attribute = $self->get_attribute_map->{$attribute_name};
22286063 410 return unless defined $removed_attribute;
411 delete $self->get_attribute_map->{$attribute_name};
9ec169fe 412 $removed_attribute->remove_accessors();
413 $removed_attribute->detach_from_class();
e16da3e6 414 return $removed_attribute;
415}
416
417sub get_attribute_list {
418 my $self = shift;
7b31baf4 419 keys %{$self->get_attribute_map};
e16da3e6 420}
421
422sub compute_all_applicable_attributes {
423 my $self = shift;
424 my @attrs;
425 # keep a record of what we have seen
426 # here, this will handle all the
427 # inheritence issues because we are
428 # using the &class_precedence_list
429 my (%seen_class, %seen_attr);
430 foreach my $class ($self->class_precedence_list()) {
431 next if $seen_class{$class};
432 $seen_class{$class}++;
433 # fetch the meta-class ...
434 my $meta = $self->initialize($class);
435 foreach my $attr_name ($meta->get_attribute_list()) {
436 next if exists $seen_attr{$attr_name};
437 $seen_attr{$attr_name}++;
c9e77dbb 438 push @attrs => $meta->get_attribute($attr_name);
e16da3e6 439 }
440 }
441 return @attrs;
442}
2eb717d5 443
52e8a34c 444# Class attributes
445
446sub add_package_variable {
447 my ($self, $variable, $initial_value) = @_;
448 (defined $variable && $variable =~ /^[\$\@\%]/)
449 || confess "variable name does not have a sigil";
450
451 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
452 if (defined $initial_value) {
453 no strict 'refs';
454 *{$self->name . '::' . $name} = $initial_value;
455 }
456 else {
457 eval $sigil . $self->name . '::' . $name;
458 confess "Could not create package variable ($variable) because : $@" if $@;
459 }
460}
461
462sub has_package_variable {
463 my ($self, $variable) = @_;
464 (defined $variable && $variable =~ /^[\$\@\%]/)
465 || confess "variable name does not have a sigil";
466 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
467 no strict 'refs';
468 defined ${$self->name . '::'}{$name} ? 1 : 0;
469}
470
471sub get_package_variable {
472 my ($self, $variable) = @_;
473 (defined $variable && $variable =~ /^[\$\@\%]/)
474 || confess "variable name does not have a sigil";
475 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
476 no strict 'refs';
477 # try to fetch it first,.. see what happens
478 eval '\\' . $sigil . $self->name . '::' . $name;
479 confess "Could not get the package variable ($variable) because : $@" if $@;
480 # if we didn't die, then we can return it
481 # NOTE:
482 # this is not ideal, better suggestions are welcome
483 eval '\\' . $sigil . $self->name . '::' . $name;
484}
485
486sub remove_package_variable {
487 my ($self, $variable) = @_;
488 (defined $variable && $variable =~ /^[\$\@\%]/)
489 || confess "variable name does not have a sigil";
490 my ($sigil, $name) = ($variable =~ /^(.)(.*)$/);
491 no strict 'refs';
492 delete ${$self->name . '::'}{$name};
493}
494
8b978dd5 4951;
496
497__END__
498
499=pod
500
501=head1 NAME
502
503Class::MOP::Class - Class Meta Object
504
505=head1 SYNOPSIS
506
fe122940 507 # use this for introspection ...
508
fe122940 509 # add a method to Foo ...
510 Foo->meta->add_method('bar' => sub { ... })
511
512 # get a list of all the classes searched
513 # the method dispatcher in the correct order
514 Foo->meta->class_precedence_list()
515
516 # remove a method from Foo
517 Foo->meta->remove_method('bar');
518
519 # or use this to actually create classes ...
520
521 Class::MOP::Class->create('Bar' => '0.01' => (
522 superclasses => [ 'Foo' ],
523 attributes => [
524 Class::MOP:::Attribute->new('$bar'),
525 Class::MOP:::Attribute->new('$baz'),
526 ],
527 methods => {
528 calculate_bar => sub { ... },
529 construct_baz => sub { ... }
530 }
531 ));
532
8b978dd5 533=head1 DESCRIPTION
534
fe122940 535This is the largest and currently most complex part of the Perl 5
536meta-object protocol. It controls the introspection and
537manipulation of Perl 5 classes (and it can create them too). The
538best way to understand what this module can do, is to read the
539documentation for each of it's methods.
540
552e3d24 541=head1 METHODS
542
2eb717d5 543=head2 Self Introspection
544
545=over 4
546
547=item B<meta>
548
fe122940 549This will return a B<Class::MOP::Class> instance which is related
550to this class. Thereby allowing B<Class::MOP::Class> to actually
551introspect itself.
552
553As with B<Class::MOP::Attribute>, B<Class::MOP> will actually
554bootstrap this module by installing a number of attribute meta-objects
555into it's metaclass. This will allow this class to reap all the benifits
556of the MOP when subclassing it.
2eb717d5 557
558=back
559
552e3d24 560=head2 Class construction
561
a2e85e6c 562These methods will handle creating B<Class::MOP::Class> objects,
563which can be used to both create new classes, and analyze
564pre-existing classes.
552e3d24 565
566This module will internally store references to all the instances
567you create with these methods, so that they do not need to be
568created any more than nessecary. Basically, they are singletons.
569
570=over 4
571
572=item B<create ($package_name, ?$package_version,
a2e85e6c 573 superclasses =E<gt> ?@superclasses,
574 methods =E<gt> ?%methods,
575 attributes =E<gt> ?%attributes)>
552e3d24 576
a2e85e6c 577This returns a B<Class::MOP::Class> object, bringing the specified
552e3d24 578C<$package_name> into existence and adding any of the
579C<$package_version>, C<@superclasses>, C<%methods> and C<%attributes>
580to it.
581
582=item B<initialize ($package_name)>
583
a2e85e6c 584This initializes and returns returns a B<Class::MOP::Class> object
585for a given a C<$package_name>.
586
651955fb 587=item B<construct_class_instance (%options)>
a2e85e6c 588
589This will construct an instance of B<Class::MOP::Class>, it is
590here so that we can actually "tie the knot" for B<Class::MOP::Class>
591to use C<construct_instance> once all the bootstrapping is done. This
592method is used internally by C<initialize> and should never be called
593from outside of that method really.
552e3d24 594
550d56db 595=item B<check_metaclass_compatability>
596
597This method is called as the very last thing in the
598C<construct_class_instance> method. This will check that the
599metaclass you are creating is compatible with the metaclasses of all
600your ancestors. For more inforamtion about metaclass compatibility
601see the C<About Metaclass compatibility> section in L<Class::MOP>.
602
552e3d24 603=back
604
c9e77dbb 605=head2 Object instance construction and cloning
a2e85e6c 606
c9e77dbb 607These methods are B<entirely optional>, it is up to you whether you want
608to use them or not.
552e3d24 609
610=over 4
611
5f3c057a 612=item B<new_object (%params)>
613
614This is a convience method for creating a new object of the class, and
615blessing it into the appropriate package as well. Ideally your class
616would call a C<new> this method like so:
617
618 sub MyClass::new {
619 my ($class, %param) = @_;
620 $class->meta->new_object(%params);
621 }
622
623Of course the ideal place for this would actually be in C<UNIVERSAL::>
624but that is considered bad style, so we do not do that.
625
cbd9f942 626=item B<construct_instance (%params)>
552e3d24 627
c9e77dbb 628This method is used to construct an instace structure suitable for
629C<bless>-ing into your package of choice. It works in conjunction
630with the Attribute protocol to collect all applicable attributes.
631
cbd9f942 632This will construct and instance using a HASH ref as storage
552e3d24 633(currently only HASH references are supported). This will collect all
a2e85e6c 634the applicable attributes and layout out the fields in the HASH ref,
635it will then initialize them using either use the corresponding key
636in C<%params> or any default value or initializer found in the
637attribute meta-object.
727919c5 638
5f3c057a 639=item B<clone_object ($instance, %params)>
640
641This is a convience method for cloning an object instance, then
642blessing it into the appropriate package. Ideally your class
643would call a C<clone> this method like so:
644
645 sub MyClass::clone {
646 my ($self, %param) = @_;
647 $self->meta->clone_object($self, %params);
648 }
649
650Of course the ideal place for this would actually be in C<UNIVERSAL::>
651but that is considered bad style, so we do not do that.
652
c9e77dbb 653=item B<clone_instance($instance, %params)>
654
655This method is a compliment of C<construct_instance> (which means if
656you override C<construct_instance>, you need to override this one too).
657
658This method will clone the C<$instance> structure created by the
659C<construct_instance> method, and apply any C<%params> passed to it
660to change the attribute values. The structure returned is (like with
661C<construct_instance>) an unC<bless>ed HASH reference, it is your
662responsibility to then bless this cloned structure into the right
663class.
664
552e3d24 665=back
666
667=head2 Informational
668
669=over 4
670
671=item B<name>
672
a2e85e6c 673This is a read-only attribute which returns the package name for the
674given B<Class::MOP::Class> instance.
552e3d24 675
676=item B<version>
677
678This is a read-only attribute which returns the C<$VERSION> of the
a2e85e6c 679package for the given B<Class::MOP::Class> instance.
552e3d24 680
681=back
682
683=head2 Inheritance Relationships
684
685=over 4
686
687=item B<superclasses (?@superclasses)>
688
689This is a read-write attribute which represents the superclass
a2e85e6c 690relationships of the class the B<Class::MOP::Class> instance is
691associated with. Basically, it can get and set the C<@ISA> for you.
552e3d24 692
343203ee 693B<NOTE:>
694Perl will occasionally perform some C<@ISA> and method caching, if
695you decide to change your superclass relationship at runtime (which
696is quite insane and very much not recommened), then you should be
697aware of this and the fact that this module does not make any
698attempt to address this issue.
699
552e3d24 700=item B<class_precedence_list>
701
a2e85e6c 702This computes the a list of all the class's ancestors in the same order
703in which method dispatch will be done. This is similair to
704what B<Class::ISA::super_path> does, but we don't remove duplicate names.
552e3d24 705
706=back
707
708=head2 Methods
709
710=over 4
711
2e41896e 712=item B<method_metaclass>
713
552e3d24 714=item B<add_method ($method_name, $method)>
715
716This will take a C<$method_name> and CODE reference to that
a2e85e6c 717C<$method> and install it into the class's package.
552e3d24 718
a2e85e6c 719B<NOTE>:
720This does absolutely nothing special to C<$method>
552e3d24 721other than use B<Sub::Name> to make sure it is tagged with the
722correct name, and therefore show up correctly in stack traces and
723such.
724
663f8198 725=item B<alias_method ($method_name, $method)>
726
727This will take a C<$method_name> and CODE reference to that
728C<$method> and alias the method into the class's package.
729
730B<NOTE>:
731Unlike C<add_method>, this will B<not> try to name the
732C<$method> using B<Sub::Name>, it only aliases the method in
733the class's package.
734
552e3d24 735=item B<has_method ($method_name)>
736
a2e85e6c 737This just provides a simple way to check if the class implements
552e3d24 738a specific C<$method_name>. It will I<not> however, attempt to check
a2e85e6c 739if the class inherits the method (use C<UNIVERSAL::can> for that).
552e3d24 740
741This will correctly handle functions defined outside of the package
742that use a fully qualified name (C<sub Package::name { ... }>).
743
744This will correctly handle functions renamed with B<Sub::Name> and
745installed using the symbol tables. However, if you are naming the
746subroutine outside of the package scope, you must use the fully
747qualified name, including the package name, for C<has_method> to
748correctly identify it.
749
750This will attempt to correctly ignore functions imported from other
751packages using B<Exporter>. It breaks down if the function imported
752is an C<__ANON__> sub (such as with C<use constant>), which very well
753may be a valid method being applied to the class.
754
755In short, this method cannot always be trusted to determine if the
756C<$method_name> is actually a method. However, it will DWIM about
a2e85e6c 75790% of the time, so it's a small trade off I think.
552e3d24 758
759=item B<get_method ($method_name)>
760
761This will return a CODE reference of the specified C<$method_name>,
762or return undef if that method does not exist.
763
764=item B<remove_method ($method_name)>
765
a2e85e6c 766This will attempt to remove a given C<$method_name> from the class.
552e3d24 767It will return the CODE reference that it has removed, and will
768attempt to use B<Sub::Name> to clear the methods associated name.
769
770=item B<get_method_list>
771
772This will return a list of method names for all I<locally> defined
773methods. It does B<not> provide a list of all applicable methods,
774including any inherited ones. If you want a list of all applicable
775methods, use the C<compute_all_applicable_methods> method.
776
777=item B<compute_all_applicable_methods>
778
a2e85e6c 779This will return a list of all the methods names this class will
780respond to, taking into account inheritance. The list will be a list of
552e3d24 781HASH references, each one containing the following information; method
782name, the name of the class in which the method lives and a CODE
783reference for the actual method.
784
785=item B<find_all_methods_by_name ($method_name)>
786
787This will traverse the inheritence hierarchy and locate all methods
788with a given C<$method_name>. Similar to
789C<compute_all_applicable_methods> it returns a list of HASH references
790with the following information; method name (which will always be the
791same as C<$method_name>), the name of the class in which the method
792lives and a CODE reference for the actual method.
793
794The list of methods produced is a distinct list, meaning there are no
795duplicates in it. This is especially useful for things like object
796initialization and destruction where you only want the method called
797once, and in the correct order.
798
799=back
800
801=head2 Attributes
802
803It should be noted that since there is no one consistent way to define
804the attributes of a class in Perl 5. These methods can only work with
805the information given, and can not easily discover information on
a2e85e6c 806their own. See L<Class::MOP::Attribute> for more details.
552e3d24 807
808=over 4
809
2e41896e 810=item B<attribute_metaclass>
811
7b31baf4 812=item B<get_attribute_map>
813
552e3d24 814=item B<add_attribute ($attribute_name, $attribute_meta_object)>
815
a2e85e6c 816This stores a C<$attribute_meta_object> in the B<Class::MOP::Class>
817instance associated with the given class, and associates it with
818the C<$attribute_name>. Unlike methods, attributes within the MOP
819are stored as meta-information only. They will be used later to
820construct instances from (see C<construct_instance> above).
552e3d24 821More details about the attribute meta-objects can be found in the
a2e85e6c 822L<Class::MOP::Attribute> or the L<Class::MOP/The Attribute protocol>
823section.
824
825It should be noted that any accessor, reader/writer or predicate
826methods which the C<$attribute_meta_object> has will be installed
827into the class at this time.
552e3d24 828
829=item B<has_attribute ($attribute_name)>
830
a2e85e6c 831Checks to see if this class has an attribute by the name of
552e3d24 832C<$attribute_name> and returns a boolean.
833
834=item B<get_attribute ($attribute_name)>
835
836Returns the attribute meta-object associated with C<$attribute_name>,
837if none is found, it will return undef.
838
839=item B<remove_attribute ($attribute_name)>
840
841This will remove the attribute meta-object stored at
842C<$attribute_name>, then return the removed attribute meta-object.
843
a2e85e6c 844B<NOTE:>
845Removing an attribute will only affect future instances of
552e3d24 846the class, it will not make any attempt to remove the attribute from
847any existing instances of the class.
848
a2e85e6c 849It should be noted that any accessor, reader/writer or predicate
850methods which the attribute meta-object stored at C<$attribute_name>
851has will be removed from the class at this time. This B<will> make
852these attributes somewhat inaccessable in previously created
853instances. But if you are crazy enough to do this at runtime, then
854you are crazy enough to deal with something like this :).
855
552e3d24 856=item B<get_attribute_list>
857
858This returns a list of attribute names which are defined in the local
859class. If you want a list of all applicable attributes for a class,
860use the C<compute_all_applicable_attributes> method.
861
862=item B<compute_all_applicable_attributes>
863
c9e77dbb 864This will traverse the inheritance heirachy and return a list of all
865the applicable attributes for this class. It does not construct a
866HASH reference like C<compute_all_applicable_methods> because all
867that same information is discoverable through the attribute
868meta-object itself.
552e3d24 869
870=back
871
52e8a34c 872=head2 Package Variables
873
874Since Perl's classes are built atop the Perl package system, it is
875fairly common to use package scoped variables for things like static
876class variables. The following methods are convience methods for
877the creation and inspection of package scoped variables.
878
879=over 4
880
881=item B<add_package_variable ($variable_name, ?$initial_value)>
882
883Given a C<$variable_name>, which must contain a leading sigil, this
884method will create that variable within the package which houses the
885class. It also takes an optional C<$initial_value>, which must be a
886reference of the same type as the sigil of the C<$variable_name>
887implies.
888
889=item B<get_package_variable ($variable_name)>
890
891This will return a reference to the package variable in
892C<$variable_name>.
893
894=item B<has_package_variable ($variable_name)>
895
896Returns true (C<1>) if there is a package variable defined for
897C<$variable_name>, and false (C<0>) otherwise.
898
899=item B<remove_package_variable ($variable_name)>
900
901This will attempt to remove the package variable at C<$variable_name>.
902
903=back
904
8b978dd5 905=head1 AUTHOR
906
a2e85e6c 907Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 908
909=head1 COPYRIGHT AND LICENSE
910
911Copyright 2006 by Infinity Interactive, Inc.
912
913L<http://www.iinteractive.com>
914
915This library is free software; you can redistribute it and/or modify
916it under the same terms as Perl itself.
917
918=cut