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