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