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