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