0.58, no really this time
[gitmo/Class-MOP.git] / lib / Class / MOP / Class.pm
CommitLineData
8b978dd5 1
2package Class::MOP::Class;
3
4use strict;
5use warnings;
6
c23184fc 7use Class::MOP::Immutable;
ba38bf08 8use Class::MOP::Instance;
9use Class::MOP::Method::Wrapped;
10
8b978dd5 11use Carp 'confess';
7f63694d 12use Scalar::Util 'blessed', 'reftype', 'weaken';
8b978dd5 13
ae234dc6 14our $VERSION = '0.32';
f0480c45 15our $AUTHORITY = 'cpan:STEVAN';
8b978dd5 16
2243a22b 17use base 'Class::MOP::Module';
18
8b978dd5 19# Creation
0ac992ee 20
be7677c7 21sub initialize {
22 my $class = shift;
23 my $package_name = shift;
24 (defined $package_name && $package_name && !blessed($package_name))
0ac992ee 25 || confess "You must pass a package name and it cannot be blessed";
408ce4ea 26 return Class::MOP::get_metaclass_by_name($package_name)
27 || $class->construct_class_instance('package' => $package_name, @_);
be7677c7 28}
29
30sub reinitialize {
31 my $class = shift;
32 my $package_name = shift;
33 (defined $package_name && $package_name && !blessed($package_name))
0ac992ee 34 || confess "You must pass a package name and it cannot be blessed";
be7677c7 35 Class::MOP::remove_metaclass_by_name($package_name);
c23184fc 36 $class->construct_class_instance('package' => $package_name, @_);
0ac992ee 37}
38
39# NOTE: (meta-circularity)
40# this is a special form of &construct_instance
be7677c7 41# (see below), which is used to construct class
0ac992ee 42# meta-object instances for any Class::MOP::*
43# class. All other classes will use the more
be7677c7 44# normal &construct_instance.
45sub construct_class_instance {
46 my $class = shift;
47 my %options = @_;
c23184fc 48 my $package_name = $options{'package'};
be7677c7 49 (defined $package_name && $package_name)
0ac992ee 50 || confess "You must pass a package name";
be7677c7 51 # NOTE:
0ac992ee 52 # return the metaclass if we have it cached,
53 # and it is still defined (it has not been
54 # reaped by DESTROY yet, which can happen
be7677c7 55 # annoyingly enough during global destruction)
3af3cbbd 56
57 if (defined(my $meta = Class::MOP::get_metaclass_by_name($package_name))) {
58 return $meta;
59 }
be7677c7 60
61 # NOTE:
0ac992ee 62 # we need to deal with the possibility
63 # of class immutability here, and then
be7677c7 64 # get the name of the class appropriately
65 $class = (blessed($class)
66 ? ($class->is_immutable
67 ? $class->get_mutable_metaclass_name()
68 : blessed($class))
69 : $class);
70
be7677c7 71 # now create the metaclass
72 my $meta;
9c6877f4 73 if ($class eq 'Class::MOP::Class') {
0ac992ee 74 no strict 'refs';
75 $meta = bless {
be7677c7 76 # inherited from Class::MOP::Package
0ac992ee 77 '$!package' => $package_name,
78
c4260b45 79 # NOTE:
0ac992ee 80 # since the following attributes will
81 # actually be loaded from the symbol
c4260b45 82 # table, and actually bypass the instance
83 # entirely, we can just leave these things
84 # listed here for reference, because they
0ac992ee 85 # should not actually have a value associated
c4260b45 86 # with the slot.
0ac992ee 87 '%!namespace' => \undef,
be7677c7 88 # inherited from Class::MOP::Module
c23184fc 89 '$!version' => \undef,
90 '$!authority' => \undef,
c4260b45 91 # defined in Class::MOP::Class
c23184fc 92 '@!superclasses' => \undef,
0ac992ee 93
c23184fc 94 '%!methods' => {},
0ac992ee 95 '%!attributes' => {},
c23184fc 96 '$!attribute_metaclass' => $options{'attribute_metaclass'} || 'Class::MOP::Attribute',
97 '$!method_metaclass' => $options{'method_metaclass'} || 'Class::MOP::Method',
98 '$!instance_metaclass' => $options{'instance_metaclass'} || 'Class::MOP::Instance',
e0e4674a 99
100 ## uber-private variables
101 # NOTE:
102 # this starts out as undef so that
103 # we can tell the first time the
104 # methods are fetched
105 # - SL
106 '$!_package_cache_flag' => undef,
be7677c7 107 } => $class;
108 }
109 else {
110 # NOTE:
111 # it is safe to use meta here because
0ac992ee 112 # class will always be a subclass of
be7677c7 113 # Class::MOP::Class, which defines meta
114 $meta = $class->meta->construct_instance(%options)
727919c5 115 }
0ac992ee 116
be7677c7 117 # and check the metaclass compatibility
715adbb7 118 $meta->check_metaclass_compatability();
0ac992ee 119
be7677c7 120 Class::MOP::store_metaclass_by_name($package_name, $meta);
0ac992ee 121
be7677c7 122 # NOTE:
123 # we need to weaken any anon classes
124 # so that they can call DESTROY properly
b9d9fc0b 125 Class::MOP::weaken_metaclass($package_name) if $meta->is_anon_class;
0ac992ee 126
127 $meta;
128}
129
715adbb7 130sub reset_package_cache_flag { (shift)->{'$!_package_cache_flag'} = undef }
131sub update_package_cache_flag {
b1f5f41d 132 my $self = shift;
e0e4674a 133 # NOTE:
134 # we can manually update the cache number
135 # since we are actually adding the method
136 # to our cache as well. This avoids us
137 # having to regenerate the method_map.
138 # - SL
b1f5f41d 139 $self->{'$!_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
e0e4674a 140}
141
be7677c7 142sub check_metaclass_compatability {
143 my $self = shift;
144
145 # this is always okay ...
0ac992ee 146 return if blessed($self) eq 'Class::MOP::Class' &&
be7677c7 147 $self->instance_metaclass eq 'Class::MOP::Instance';
148
b7bdffc3 149 my @class_list = $self->linearized_isa;
be7677c7 150 shift @class_list; # shift off $self->name
373a16ae 151
0ac992ee 152 foreach my $class_name (@class_list) {
be7677c7 153 my $meta = Class::MOP::get_metaclass_by_name($class_name) || next;
0ac992ee 154
373a16ae 155 # NOTE:
0ac992ee 156 # we need to deal with the possibility
157 # of class immutability here, and then
158 # get the name of the class appropriately
be7677c7 159 my $meta_type = ($meta->is_immutable
160 ? $meta->get_mutable_metaclass_name()
0ac992ee 161 : blessed($meta));
162
be7677c7 163 ($self->isa($meta_type))
408ce4ea 164 || confess $self->name . "->meta => (" . (blessed($self)) . ")" .
0ac992ee 165 " is not compatible with the " .
be7677c7 166 $class_name . "->meta => (" . ($meta_type) . ")";
77e5fce4 167 # NOTE:
be7677c7 168 # we also need to check that instance metaclasses
169 # are compatabile in the same the class.
170 ($self->instance_metaclass->isa($meta->instance_metaclass))
408ce4ea 171 || confess $self->name . "->meta => (" . ($self->instance_metaclass) . ")" .
0ac992ee 172 " is not compatible with the " .
173 $class_name . "->meta => (" . ($meta->instance_metaclass) . ")";
174 }
175}
8b978dd5 176
6d5355c3 177## ANON classes
178
179{
180 # NOTE:
0ac992ee 181 # this should be sufficient, if you have a
182 # use case where it is not, write a test and
6d5355c3 183 # I will change it.
184 my $ANON_CLASS_SERIAL = 0;
0ac992ee 185
b9d9fc0b 186 # NOTE:
187 # we need a sufficiently annoying prefix
0ac992ee 188 # this should suffice for now, this is
189 # used in a couple of places below, so
b9d9fc0b 190 # need to put it up here for now.
0ac992ee 191 my $ANON_CLASS_PREFIX = 'Class::MOP::Class::__ANON__::SERIAL::';
b9d9fc0b 192
193 sub is_anon_class {
194 my $self = shift;
a651e249 195 no warnings 'uninitialized';
0ac992ee 196 $self->name =~ /^$ANON_CLASS_PREFIX/ ? 1 : 0;
b9d9fc0b 197 }
6d5355c3 198
199 sub create_anon_class {
0ac992ee 200 my ($class, %options) = @_;
6d5355c3 201 my $package_name = $ANON_CLASS_PREFIX . ++$ANON_CLASS_SERIAL;
88dd563c 202 return $class->create($package_name, %options);
0ac992ee 203 }
6d5355c3 204
b9d9fc0b 205 # NOTE:
0ac992ee 206 # this will only get called for
207 # anon-classes, all other calls
208 # are assumed to occur during
b9d9fc0b 209 # global destruction and so don't
210 # really need to be handled explicitly
211 sub DESTROY {
212 my $self = shift;
0ac992ee 213 no warnings 'uninitialized';
408ce4ea 214 return unless $self->name =~ /^$ANON_CLASS_PREFIX/;
215 my ($serial_id) = ($self->name =~ /^$ANON_CLASS_PREFIX(\d+)/);
0ac992ee 216 no strict 'refs';
b9d9fc0b 217 foreach my $key (keys %{$ANON_CLASS_PREFIX . $serial_id}) {
218 delete ${$ANON_CLASS_PREFIX . $serial_id}{$key};
219 }
0ac992ee 220 delete ${'main::' . $ANON_CLASS_PREFIX}{$serial_id . '::'};
6d5355c3 221 }
b9d9fc0b 222
6d5355c3 223}
224
225# creating classes with MOP ...
226
8b978dd5 227sub create {
88dd563c 228 my $class = shift;
229 my $package_name = shift;
0ac992ee 230
bfe4d0fc 231 (defined $package_name && $package_name)
8b978dd5 232 || confess "You must pass a package name";
88dd563c 233
234 (scalar @_ % 2 == 0)
0ac992ee 235 || confess "You much pass all parameters as name => value pairs " .
88dd563c 236 "(I found an uneven number of params in \@_)";
237
238 my (%options) = @_;
d7b2249e 239
240 (ref $options{superclasses} eq 'ARRAY')
241 || confess "You must pass an ARRAY ref of superclasses"
242 if exists $options{superclasses};
243
244 (ref $options{attributes} eq 'ARRAY')
245 || confess "You must pass an ARRAY ref of attributes"
246 if exists $options{attributes};
247
248 (ref $options{methods} eq 'HASH')
249 || confess "You must pass an HASH ref of methods"
250 if exists $options{methods};
0ac992ee 251
8b978dd5 252 my $code = "package $package_name;";
0ac992ee 253 $code .= "\$$package_name\:\:VERSION = '" . $options{version} . "';"
88dd563c 254 if exists $options{version};
0ac992ee 255 $code .= "\$$package_name\:\:AUTHORITY = '" . $options{authority} . "';"
256 if exists $options{authority};
257
8b978dd5 258 eval $code;
0ac992ee 259 confess "creation of $package_name failed : $@" if $@;
260
bfe4d0fc 261 my $meta = $class->initialize($package_name);
0ac992ee 262
263 $meta->add_method('meta' => sub {
df7b4119 264 $class->initialize(blessed($_[0]) || $_[0]);
aa448b16 265 });
0ac992ee 266
8b978dd5 267 $meta->superclasses(@{$options{superclasses}})
268 if exists $options{superclasses};
2eb717d5 269 # NOTE:
0ac992ee 270 # process attributes first, so that they can
2eb717d5 271 # install accessors, but locally defined methods
272 # can then overwrite them. It is maybe a little odd, but
273 # I think this should be the order of things.
274 if (exists $options{attributes}) {
cbd9f942 275 foreach my $attr (@{$options{attributes}}) {
276 $meta->add_attribute($attr);
2eb717d5 277 }
0ac992ee 278 }
bfe4d0fc 279 if (exists $options{methods}) {
280 foreach my $method_name (keys %{$options{methods}}) {
281 $meta->add_method($method_name, $options{methods}->{$method_name});
282 }
0ac992ee 283 }
8b978dd5 284 return $meta;
285}
286
7b31baf4 287## Attribute readers
288
289# NOTE:
0ac992ee 290# all these attribute readers will be bootstrapped
7b31baf4 291# away in the Class::MOP bootstrap section
292
c23184fc 293sub get_attribute_map { $_[0]->{'%!attributes'} }
294sub attribute_metaclass { $_[0]->{'$!attribute_metaclass'} }
295sub method_metaclass { $_[0]->{'$!method_metaclass'} }
296sub instance_metaclass { $_[0]->{'$!instance_metaclass'} }
7b31baf4 297
0f71bc80 298# FIXME:
299# this is a prime canidate for conversion to XS
0ac992ee 300sub get_method_map {
c4260b45 301 my $self = shift;
e0e4674a 302
303 if (defined $self->{'$!_package_cache_flag'} &&
b1f5f41d 304 $self->{'$!_package_cache_flag'} == Class::MOP::check_package_cache_flag($self->name)) {
e0e4674a 305 return $self->{'%!methods'};
306 }
307
0ac992ee 308 my $map = $self->{'%!methods'};
309
0f71bc80 310 my $class_name = $self->name;
311 my $method_metaclass = $self->method_metaclass;
0ac992ee 312
ae234dc6 313 my %all_code = $self->get_all_package_symbols('CODE');
314
315 foreach my $symbol (keys %all_code) {
316 my $code = $all_code{$symbol};
0ac992ee 317
318 next if exists $map->{$symbol} &&
319 defined $map->{$symbol} &&
320 $map->{$symbol}->body == $code;
321
e0e4674a 322 my ($pkg, $name) = Class::MOP::get_code_info($code);
46b23b44 323
d7d3f3cb 324 # NOTE:
325 # in 5.10 constant.pm the constants show up
326 # as being in the right package, but in pre-5.10
327 # they show up as constant::__ANON__ so we
328 # make an exception here to be sure that things
329 # work as expected in both.
330 # - SL
331 unless ($pkg eq 'constant' && $name eq '__ANON__') {
332 next if ($pkg || '') ne $class_name ||
333 (($name || '') ne '__ANON__' && ($pkg || '') ne $class_name);
334 }
46b23b44 335
b38f3848 336 $map->{$symbol} = $method_metaclass->wrap(
337 $code,
338 package_name => $class_name,
339 name => $symbol,
340 );
7855ddba 341 }
0ac992ee 342
7855ddba 343 return $map;
c4260b45 344}
345
c9e77dbb 346# Instance Construction & Cloning
347
5f3c057a 348sub new_object {
349 my $class = shift;
651955fb 350 # NOTE:
0ac992ee 351 # we need to protect the integrity of the
651955fb 352 # Class::MOP::Class singletons here, so we
353 # delegate this to &construct_class_instance
354 # which will deal with the singletons
355 return $class->construct_class_instance(@_)
356 if $class->name->isa('Class::MOP::Class');
24869f62 357 return $class->construct_instance(@_);
5f3c057a 358}
e16da3e6 359
360sub construct_instance {
cbd9f942 361 my ($class, %params) = @_;
0e76a376 362 my $meta_instance = $class->get_meta_instance();
363 my $instance = $meta_instance->create_instance();
c9e77dbb 364 foreach my $attr ($class->compute_all_applicable_attributes()) {
f892c0f0 365 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
cbd9f942 366 }
0ac992ee 367 # NOTE:
d4ba1677 368 # this will only work for a HASH instance type
369 if ($class->is_anon_class) {
370 (reftype($instance) eq 'HASH')
371 || confess "Currently only HASH based instances are supported with instance of anon-classes";
372 # NOTE:
373 # At some point we should make this official
0ac992ee 374 # as a reserved slot name, but right now I am
d4ba1677 375 # going to keep it here.
376 # my $RESERVED_MOP_SLOT = '__MOP__';
377 $instance->{'__MOP__'} = $class;
378 }
2d711cc8 379 return $instance;
380}
381
382sub get_meta_instance {
383 my $class = shift;
052c2a1a 384 return $class->instance_metaclass->new(
0ac992ee 385 $class,
052c2a1a 386 $class->compute_all_applicable_attributes()
387 );
e16da3e6 388}
389
5f3c057a 390sub clone_object {
391 my $class = shift;
0ac992ee 392 my $instance = shift;
408ce4ea 393 (blessed($instance) && $instance->isa($class->name))
394 || confess "You must pass an instance ($instance) of the metaclass (" . $class->name . ")";
651955fb 395 # NOTE:
0ac992ee 396 # we need to protect the integrity of the
397 # Class::MOP::Class singletons here, they
a740253a 398 # should not be cloned.
0ac992ee 399 return $instance if $instance->isa('Class::MOP::Class');
f7259199 400 $class->clone_instance($instance, @_);
5f3c057a 401}
402
c9e77dbb 403sub clone_instance {
651955fb 404 my ($class, $instance, %params) = @_;
405 (blessed($instance))
c9e77dbb 406 || confess "You can only clone instances, \$self is not a blessed instance";
f7259199 407 my $meta_instance = $class->get_meta_instance();
0ac992ee 408 my $clone = $meta_instance->clone_instance($instance);
c23184fc 409 foreach my $attr ($class->compute_all_applicable_attributes()) {
de55d708 410 if ( defined( my $init_arg = $attr->init_arg ) ) {
411 if (exists $params{$init_arg}) {
412 $attr->set_value($clone, $params{$init_arg});
413 }
c23184fc 414 }
0ac992ee 415 }
416 return $clone;
c9e77dbb 417}
418
3d9e4646 419sub rebless_instance {
214e4bd7 420 my ($self, $instance, %params) = @_;
2a2b8458 421
422 my $old_metaclass;
423 if ($instance->can('meta')) {
424 ($instance->meta->isa('Class::MOP::Class'))
425 || confess 'Cannot rebless instance if ->meta is not an instance of Class::MOP::Class';
426 $old_metaclass = $instance->meta;
427 }
428 else {
429 $old_metaclass = $self->initialize(blessed($instance));
430 }
431
1a720916 432 my $meta_instance = $self->get_meta_instance();
408ce4ea 433
434 $self->name->isa($old_metaclass->name)
435 || confess "You may rebless only into a subclass of (". $old_metaclass->name ."), of which (". $self->name .") isn't.";
9b71b643 436
1a720916 437 # rebless!
3cd40f5e 438 $meta_instance->rebless_instance_structure($instance, $self);
1a720916 439
de55d708 440 foreach my $attr ( $self->compute_all_applicable_attributes ) {
441 if ( $attr->has_value($instance) ) {
e253285d 442 if ( defined( my $init_arg = $attr->init_arg ) ) {
214e4bd7 443 $params{$init_arg} = $attr->get_value($instance)
444 unless exists $params{$init_arg};
445 }
446 else {
b293f28b 447 $attr->set_value($instance, $attr->get_value($instance));
e253285d 448 }
de55d708 449 }
1a720916 450 }
e253285d 451
452 foreach my $attr ($self->compute_all_applicable_attributes) {
453 $attr->initialize_instance_slot($meta_instance, $instance, \%params);
454 }
830b326c 455
456 $instance;
3d9e4646 457}
458
8b978dd5 459# Inheritance
460
461sub superclasses {
462 my $self = shift;
8b978dd5 463 if (@_) {
464 my @supers = @_;
9d6dce77 465 @{$self->get_package_symbol('@ISA')} = @supers;
d82060fe 466 # NOTE:
0ac992ee 467 # we need to check the metaclass
84086365 468 # compatibility here so that we can
0ac992ee 469 # be sure that the superclass is
470 # not potentially creating an issues
d82060fe 471 # we don't know about
472 $self->check_metaclass_compatability();
8b978dd5 473 }
9d6dce77 474 @{$self->get_package_symbol('@ISA')};
8b978dd5 475}
476
7160cad4 477sub subclasses {
478 my $self = shift;
479
480 my $super_class = $self->name;
481 my @derived_classes;
482
483 my $find_derived_classes;
484 $find_derived_classes = sub {
485 my ($outer_class) = @_;
486
487 my $symbol_table_hashref = do { no strict 'refs'; \%{"${outer_class}::"} };
488
c1d5345a 489 SYMBOL:
7160cad4 490 for my $symbol ( keys %$symbol_table_hashref ) {
491 next SYMBOL if $symbol !~ /\A (\w+):: \z/x;
492 my $inner_class = $1;
493
494 next SYMBOL if $inner_class eq 'SUPER'; # skip '*::SUPER'
495
496 my $class =
497 $outer_class
498 ? "${outer_class}::$inner_class"
499 : $inner_class;
500
501 if ( $class->isa($super_class) and $class ne $super_class ) {
502 push @derived_classes, $class;
503 }
504
505 next SYMBOL if $class eq 'main'; # skip 'main::*'
506
507 $find_derived_classes->($class);
508 }
509 };
510
511 my $root_class = q{};
512 $find_derived_classes->($root_class);
513
514 undef $find_derived_classes;
515
516 @derived_classes = sort { $a->isa($b) ? 1 : $b->isa($a) ? -1 : 0 } @derived_classes;
517
518 return @derived_classes;
519}
520
521
b7bdffc3 522sub linearized_isa {
3cf322a0 523 return @{ mro::get_linear_isa( (shift)->name ) };
b7bdffc3 524}
525
8b978dd5 526sub class_precedence_list {
527 my $self = shift;
77a143ba 528 my $name = $self->name;
c1d5345a 529
530 unless (Class::MOP::IS_RUNNING_ON_5_10()) {
531 # NOTE:
532 # We need to check for circular inheritance here
533 # if we are are not on 5.10, cause 5.8 detects it
534 # late. This will do nothing if all is well, and
535 # blow up otherwise. Yes, it's an ugly hack, better
536 # suggestions are welcome.
537 # - SL
77a143ba 538 ($name || return)->isa('This is a test for circular inheritance')
c1d5345a 539 }
0ac992ee 540
77a143ba 541 # if our mro is c3, we can
542 # just grab the linear_isa
543 if (mro::get_mro($name) eq 'c3') {
544 return @{ mro::get_linear_isa($name) }
545 }
546 else {
547 # NOTE:
548 # we can't grab the linear_isa for dfs
549 # since it has all the duplicates
550 # already removed.
551 return (
552 $name,
553 map {
554 $self->initialize($_)->class_precedence_list()
555 } $self->superclasses()
556 );
557 }
8b978dd5 558}
559
0882828e 560## Methods
561
562sub add_method {
563 my ($self, $method_name, $method) = @_;
564 (defined $method_name && $method_name)
565 || confess "You must define a method name";
0ac992ee 566
7855ddba 567 my $body;
7855ddba 568 if (blessed($method)) {
0ac992ee 569 $body = $method->body;
408ce4ea 570 if ($method->package_name ne $self->name &&
4c105333 571 $method->name ne $method_name) {
572 warn "Hello there, got somethig for you."
573 . " Method says " . $method->package_name . " " . $method->name
408ce4ea 574 . " Class says " . $self->name . " " . $method_name;
4c105333 575 $method = $method->clone(
408ce4ea 576 package_name => $self->name,
4c105333 577 name => $method_name
578 ) if $method->can('clone');
579 }
7855ddba 580 }
0ac992ee 581 else {
7855ddba 582 $body = $method;
7855ddba 583 ('CODE' eq (reftype($body) || ''))
0ac992ee 584 || confess "Your code block must be a CODE reference";
4c105333 585 $method = $self->method_metaclass->wrap(
586 $body => (
408ce4ea 587 package_name => $self->name,
4c105333 588 name => $method_name
589 )
590 );
7855ddba 591 }
0f71bc80 592 $self->get_method_map->{$method_name} = $method;
4c105333 593
408ce4ea 594 my $full_method_name = ($self->name . '::' . $method_name);
4c105333 595 $self->add_package_symbol("&${method_name}" =>
596 Class::MOP::subname($full_method_name => $body)
597 );
715adbb7 598 $self->update_package_cache_flag;
0882828e 599}
600
a4258ffd 601{
2d711cc8 602 my $fetch_and_prepare_method = sub {
603 my ($self, $method_name) = @_;
604 # fetch it locally
605 my $method = $self->get_method($method_name);
606 # if we dont have local ...
607 unless ($method) {
195f5bf8 608 # try to find the next method
609 $method = $self->find_next_method_by_name($method_name);
610 # die if it does not exist
611 (defined $method)
804f7d24 612 || confess "The method '$method_name' is not found in the inheritance hierarchy for class " . $self->name;
0ac992ee 613 # and now make sure to wrap it
195f5bf8 614 # even if it is already wrapped
615 # because we need a new sub ref
2d711cc8 616 $method = Class::MOP::Method::Wrapped->wrap($method);
195f5bf8 617 }
618 else {
0ac992ee 619 # now make sure we wrap it properly
195f5bf8 620 $method = Class::MOP::Method::Wrapped->wrap($method)
0ac992ee 621 unless $method->isa('Class::MOP::Method::Wrapped');
622 }
623 $self->add_method($method_name => $method);
2d711cc8 624 return $method;
625 };
626
627 sub add_before_method_modifier {
628 my ($self, $method_name, $method_modifier) = @_;
629 (defined $method_name && $method_name)
0ac992ee 630 || confess "You must pass in a method name";
2d711cc8 631 my $method = $fetch_and_prepare_method->($self, $method_name);
4c105333 632 $method->add_before_modifier(
633 Class::MOP::subname(':before' => $method_modifier)
634 );
2d711cc8 635 }
636
637 sub add_after_method_modifier {
638 my ($self, $method_name, $method_modifier) = @_;
639 (defined $method_name && $method_name)
0ac992ee 640 || confess "You must pass in a method name";
2d711cc8 641 my $method = $fetch_and_prepare_method->($self, $method_name);
4c105333 642 $method->add_after_modifier(
643 Class::MOP::subname(':after' => $method_modifier)
644 );
2d711cc8 645 }
0ac992ee 646
2d711cc8 647 sub add_around_method_modifier {
648 my ($self, $method_name, $method_modifier) = @_;
649 (defined $method_name && $method_name)
650 || confess "You must pass in a method name";
651 my $method = $fetch_and_prepare_method->($self, $method_name);
4c105333 652 $method->add_around_modifier(
653 Class::MOP::subname(':around' => $method_modifier)
654 );
0ac992ee 655 }
a4258ffd 656
0ac992ee 657 # NOTE:
8c936afc 658 # the methods above used to be named like this:
659 # ${pkg}::${method}:(before|after|around)
660 # but this proved problematic when using one modifier
661 # to wrap multiple methods (something which is likely
662 # to happen pretty regularly IMO). So instead of naming
0ac992ee 663 # it like this, I have chosen to just name them purely
8c936afc 664 # with their modifier names, like so:
665 # :(before|after|around)
0ac992ee 666 # The fact is that in a stack trace, it will be fairly
8c936afc 667 # evident from the context what method they are attached
668 # to, and so don't need the fully qualified name.
ee5e71d4 669}
670
663f8198 671sub alias_method {
672 my ($self, $method_name, $method) = @_;
673 (defined $method_name && $method_name)
674 || confess "You must define a method name";
de19f115 675
0f71bc80 676 my $body = (blessed($method) ? $method->body : $method);
677 ('CODE' eq (reftype($body) || ''))
0ac992ee 678 || confess "Your code block must be a CODE reference";
679
7855ddba 680 $self->add_package_symbol("&${method_name}" => $body);
715adbb7 681 $self->update_package_cache_flag;
16e960bd 682}
683
de19f115 684sub has_method {
685 my ($self, $method_name) = @_;
686 (defined $method_name && $method_name)
0ac992ee 687 || confess "You must define a method name";
688
689 return 0 unless exists $self->get_method_map->{$method_name};
de19f115 690 return 1;
0882828e 691}
692
693sub get_method {
c9b8b7f9 694 my ($self, $method_name) = @_;
0882828e 695 (defined $method_name && $method_name)
696 || confess "You must define a method name";
0ac992ee 697
0f71bc80 698 # NOTE:
699 # I don't really need this here, because
0ac992ee 700 # if the method_map is missing a key it
0f71bc80 701 # will just return undef for me now
702 # return unless $self->has_method($method_name);
0ac992ee 703
7855ddba 704 return $self->get_method_map->{$method_name};
c9b8b7f9 705}
706
707sub remove_method {
708 my ($self, $method_name) = @_;
709 (defined $method_name && $method_name)
710 || confess "You must define a method name";
0ac992ee 711
e0e4674a 712 my $removed_method = delete $self->get_method_map->{$method_name};
713
714 $self->remove_package_symbol("&${method_name}");
715
715adbb7 716 $self->update_package_cache_flag;
0ac992ee 717
c9b8b7f9 718 return $removed_method;
719}
720
721sub get_method_list {
722 my $self = shift;
0f71bc80 723 keys %{$self->get_method_map};
7855ddba 724}
725
726sub find_method_by_name {
727 my ($self, $method_name) = @_;
b9575695 728 (defined $method_name && $method_name)
0ac992ee 729 || confess "You must define a method name to find";
b7bdffc3 730 foreach my $class ($self->linearized_isa) {
b9575695 731 # fetch the meta-class ...
732 my $meta = $self->initialize($class);
0ac992ee 733 return $meta->get_method($method_name)
b9575695 734 if $meta->has_method($method_name);
735 }
736 return;
a5eca695 737}
738
739sub compute_all_applicable_methods {
740 my $self = shift;
b7bdffc3 741 my (@methods, %seen_method);
742 foreach my $class ($self->linearized_isa) {
a5eca695 743 # fetch the meta-class ...
744 my $meta = $self->initialize($class);
0ac992ee 745 foreach my $method_name ($meta->get_method_list()) {
a5eca695 746 next if exists $seen_method{$method_name};
747 $seen_method{$method_name}++;
748 push @methods => {
0ac992ee 749 name => $method_name,
a5eca695 750 class => $class,
751 code => $meta->get_method($method_name)
752 };
753 }
754 }
755 return @methods;
756}
757
a5eca695 758sub find_all_methods_by_name {
759 my ($self, $method_name) = @_;
760 (defined $method_name && $method_name)
0ac992ee 761 || confess "You must define a method name to find";
a5eca695 762 my @methods;
b7bdffc3 763 foreach my $class ($self->linearized_isa) {
a5eca695 764 # fetch the meta-class ...
96ceced8 765 my $meta = $self->initialize($class);
a5eca695 766 push @methods => {
0ac992ee 767 name => $method_name,
a5eca695 768 class => $class,
769 code => $meta->get_method($method_name)
770 } if $meta->has_method($method_name);
771 }
772 return @methods;
8b978dd5 773}
774
96ceced8 775sub find_next_method_by_name {
776 my ($self, $method_name) = @_;
777 (defined $method_name && $method_name)
0ac992ee 778 || confess "You must define a method name to find";
b7bdffc3 779 my @cpl = $self->linearized_isa;
2d711cc8 780 shift @cpl; # discard ourselves
96ceced8 781 foreach my $class (@cpl) {
96ceced8 782 # fetch the meta-class ...
783 my $meta = $self->initialize($class);
0ac992ee 784 return $meta->get_method($method_name)
2d711cc8 785 if $meta->has_method($method_name);
96ceced8 786 }
2d711cc8 787 return;
96ceced8 788}
789
552e3d24 790## Attributes
791
e16da3e6 792sub add_attribute {
2e41896e 793 my $self = shift;
794 # either we have an attribute object already
795 # or we need to create one from the args provided
796 my $attribute = blessed($_[0]) ? $_[0] : $self->attribute_metaclass->new(@_);
797 # make sure it is derived from the correct type though
798 ($attribute->isa('Class::MOP::Attribute'))
0ac992ee 799 || confess "Your attribute must be an instance of Class::MOP::Attribute (or a subclass)";
b1897d4d 800
801 # first we attach our new attribute
0ac992ee 802 # because it might need certain information
b1897d4d 803 # about the class which it is attached to
9ec169fe 804 $attribute->attach_to_class($self);
0ac992ee 805
806 # then we remove attributes of a conflicting
807 # name here so that we can properly detach
808 # the old attr object, and remove any
b1897d4d 809 # accessors it would have generated
408ce4ea 810 $self->remove_attribute($attribute->name)
811 if $self->has_attribute($attribute->name);
0ac992ee 812
b1897d4d 813 # then onto installing the new accessors
2d711cc8 814 $attribute->install_accessors();
408ce4ea 815 $self->get_attribute_map->{$attribute->name} = $attribute;
e16da3e6 816}
817
818sub has_attribute {
819 my ($self, $attribute_name) = @_;
820 (defined $attribute_name && $attribute_name)
821 || confess "You must define an attribute name";
0ac992ee 822 exists $self->get_attribute_map->{$attribute_name} ? 1 : 0;
823}
e16da3e6 824
825sub get_attribute {
826 my ($self, $attribute_name) = @_;
827 (defined $attribute_name && $attribute_name)
828 || confess "You must define an attribute name";
0ac992ee 829 return $self->get_attribute_map->{$attribute_name}
b1897d4d 830 # NOTE:
831 # this will return undef anyway, so no need ...
0ac992ee 832 # if $self->has_attribute($attribute_name);
833 #return;
834}
e16da3e6 835
836sub remove_attribute {
837 my ($self, $attribute_name) = @_;
838 (defined $attribute_name && $attribute_name)
839 || confess "You must define an attribute name";
0ac992ee 840 my $removed_attribute = $self->get_attribute_map->{$attribute_name};
22286063 841 return unless defined $removed_attribute;
0ac992ee 842 delete $self->get_attribute_map->{$attribute_name};
843 $removed_attribute->remove_accessors();
2d711cc8 844 $removed_attribute->detach_from_class();
e16da3e6 845 return $removed_attribute;
0ac992ee 846}
e16da3e6 847
848sub get_attribute_list {
849 my $self = shift;
f7259199 850 keys %{$self->get_attribute_map};
0ac992ee 851}
e16da3e6 852
853sub compute_all_applicable_attributes {
854 my $self = shift;
b7bdffc3 855 my (@attrs, %seen_attr);
856 foreach my $class ($self->linearized_isa) {
e16da3e6 857 # fetch the meta-class ...
f7259199 858 my $meta = $self->initialize($class);
0ac992ee 859 foreach my $attr_name ($meta->get_attribute_list()) {
e16da3e6 860 next if exists $seen_attr{$attr_name};
861 $seen_attr{$attr_name}++;
c9e77dbb 862 push @attrs => $meta->get_attribute($attr_name);
e16da3e6 863 }
864 }
0ac992ee 865 return @attrs;
e16da3e6 866}
2eb717d5 867
058c1cf5 868sub find_attribute_by_name {
869 my ($self, $attr_name) = @_;
b7bdffc3 870 foreach my $class ($self->linearized_isa) {
058c1cf5 871 # fetch the meta-class ...
872 my $meta = $self->initialize($class);
873 return $meta->get_attribute($attr_name)
874 if $meta->has_attribute($attr_name);
875 }
876 return;
877}
878
857f87a7 879## Class closing
880
881sub is_mutable { 1 }
882sub is_immutable { 0 }
883
b817e248 884# NOTE:
885# Why I changed this (groditi)
886# - One Metaclass may have many Classes through many Metaclass instances
887# - One Metaclass should only have one Immutable Transformer instance
888# - Each Class may have different Immutabilizing options
889# - Therefore each Metaclass instance may have different Immutabilizing options
890# - We need to store one Immutable Transformer instance per Metaclass
891# - We need to store one set of Immutable Transformer options per Class
892# - Upon make_mutable we may delete the Immutabilizing options
893# - We could clean the immutable Transformer instance when there is no more
894# immutable Classes of that type, but we can also keep it in case
895# another class with this same Metaclass becomes immutable. It is a case
896# of trading of storing an instance to avoid unnecessary instantiations of
897# Immutable Transformers. You may view this as a memory leak, however
898# Because we have few Metaclasses, in practice it seems acceptable
899# - To allow Immutable Transformers instances to be cleaned up we could weaken
900# the reference stored in $IMMUTABLE_TRANSFORMERS{$class} and ||= should DWIM
04dd7510 901
c23184fc 902{
d9586da2 903 my %IMMUTABLE_TRANSFORMERS;
0ac992ee 904 my %IMMUTABLE_OPTIONS;
c23184fc 905 sub make_immutable {
0ac992ee 906 my $self = shift;
04dd7510 907 my %options = @_;
d9586da2 908 my $class = blessed $self || $self;
909
910 $IMMUTABLE_TRANSFORMERS{$class} ||= $self->create_immutable_transformer;
911 my $transformer = $IMMUTABLE_TRANSFORMERS{$class};
912
229910b5 913 $transformer->make_metaclass_immutable($self, \%options);
7f63694d 914 $IMMUTABLE_OPTIONS{$self->name} =
d9586da2 915 { %options, IMMUTABLE_TRANSFORMER => $transformer };
04dd7510 916
917 if( exists $options{debug} && $options{debug} ){
d9586da2 918 print STDERR "# of Metaclass options: ", keys %IMMUTABLE_OPTIONS;
919 print STDERR "# of Immutable transformers: ", keys %IMMUTABLE_TRANSFORMERS;
04dd7510 920 }
09c6e1d2 921
922 1;
c23184fc 923 }
0ac992ee 924
925 sub make_mutable{
926 my $self = shift;
927 return if $self->is_mutable;
7f63694d 928 my $options = delete $IMMUTABLE_OPTIONS{$self->name};
1d68af04 929 confess "unable to find immutabilizing options" unless ref $options;
d9586da2 930 my $transformer = delete $options->{IMMUTABLE_TRANSFORMER};
229910b5 931 $transformer->make_metaclass_mutable($self, $options);
09c6e1d2 932 1;
0ac992ee 933 }
d9586da2 934}
0ac992ee 935
d9586da2 936sub create_immutable_transformer {
937 my $self = shift;
938 my $class = Class::MOP::Immutable->new($self, {
5f3efd66 939 read_only => [qw/superclasses/],
940 cannot_call => [qw/
d9586da2 941 add_method
942 alias_method
943 remove_method
944 add_attribute
945 remove_attribute
d9586da2 946 remove_package_symbol
5f3efd66 947 /],
948 memoize => {
d9586da2 949 class_precedence_list => 'ARRAY',
b7bdffc3 950 linearized_isa => 'ARRAY',
d9586da2 951 compute_all_applicable_attributes => 'ARRAY',
952 get_meta_instance => 'SCALAR',
953 get_method_map => 'SCALAR',
5f3efd66 954 },
955 # NOTE:
956 # this is ugly, but so are typeglobs,
957 # so whattayahgonnadoboutit
958 # - SL
959 wrapped => {
960 add_package_symbol => sub {
961 my $original = shift;
962 confess "Cannot add package symbols to an immutable metaclass"
963 unless (caller(2))[3] eq 'Class::MOP::Package::get_package_symbol';
964 goto $original->body;
965 },
966 },
d9586da2 967 });
968 return $class;
857f87a7 969}
970
8b978dd5 9711;
972
973__END__
974
975=pod
976
0ac992ee 977=head1 NAME
8b978dd5 978
979Class::MOP::Class - Class Meta Object
980
981=head1 SYNOPSIS
982
0ac992ee 983 # assuming that class Foo
8c936afc 984 # has been defined, you can
0ac992ee 985
fe122940 986 # use this for introspection ...
0ac992ee 987
fe122940 988 # add a method to Foo ...
989 Foo->meta->add_method('bar' => sub { ... })
0ac992ee 990
991 # get a list of all the classes searched
992 # the method dispatcher in the correct order
fe122940 993 Foo->meta->class_precedence_list()
0ac992ee 994
fe122940 995 # remove a method from Foo
996 Foo->meta->remove_method('bar');
0ac992ee 997
fe122940 998 # or use this to actually create classes ...
0ac992ee 999
88dd563c 1000 Class::MOP::Class->create('Bar' => (
1001 version => '0.01',
fe122940 1002 superclasses => [ 'Foo' ],
1003 attributes => [
1004 Class::MOP:::Attribute->new('$bar'),
0ac992ee 1005 Class::MOP:::Attribute->new('$baz'),
fe122940 1006 ],
1007 methods => {
1008 calculate_bar => sub { ... },
0ac992ee 1009 construct_baz => sub { ... }
fe122940 1010 }
1011 ));
1012
8b978dd5 1013=head1 DESCRIPTION
1014
0ac992ee 1015This is the largest and currently most complex part of the Perl 5
1016meta-object protocol. It controls the introspection and
1017manipulation of Perl 5 classes (and it can create them too). The
1018best way to understand what this module can do, is to read the
fe122940 1019documentation for each of it's methods.
1020
552e3d24 1021=head1 METHODS
1022
2eb717d5 1023=head2 Self Introspection
1024
1025=over 4
1026
1027=item B<meta>
1028
0ac992ee 1029This will return a B<Class::MOP::Class> instance which is related
1030to this class. Thereby allowing B<Class::MOP::Class> to actually
fe122940 1031introspect itself.
1032
0ac992ee 1033As with B<Class::MOP::Attribute>, B<Class::MOP> will actually
1034bootstrap this module by installing a number of attribute meta-objects
1035into it's metaclass. This will allow this class to reap all the benifits
1036of the MOP when subclassing it.
2eb717d5 1037
1038=back
1039
552e3d24 1040=head2 Class construction
1041
0ac992ee 1042These methods will handle creating B<Class::MOP::Class> objects,
1043which can be used to both create new classes, and analyze
1044pre-existing classes.
552e3d24 1045
0ac992ee 1046This module will internally store references to all the instances
1047you create with these methods, so that they do not need to be
552e3d24 1048created any more than nessecary. Basically, they are singletons.
1049
1050=over 4
1051
0ac992ee 1052=item B<create ($package_name,
1053 version =E<gt> ?$version,
1054 authority =E<gt> ?$authority,
1055 superclasses =E<gt> ?@superclasses,
1056 methods =E<gt> ?%methods,
a2e85e6c 1057 attributes =E<gt> ?%attributes)>
552e3d24 1058
0ac992ee 1059This returns a B<Class::MOP::Class> object, bringing the specified
1060C<$package_name> into existence and adding any of the C<$version>,
1061C<$authority>, C<@superclasses>, C<%methods> and C<%attributes> to
88dd563c 1062it.
552e3d24 1063
0ac992ee 1064=item B<create_anon_class (superclasses =E<gt> ?@superclasses,
1065 methods =E<gt> ?%methods,
587aca23 1066 attributes =E<gt> ?%attributes)>
1067
0ac992ee 1068This will create an anonymous class, it works much like C<create> but
1069it does not need a C<$package_name>. Instead it will create a suitably
587aca23 1070unique package name for you to stash things into.
1071
0ac992ee 1072On very important distinction is that anon classes are destroyed once
1073the metaclass they are attached to goes out of scope. In the DESTROY
1074method, the created package will be removed from the symbol table.
823a5d31 1075
d4ba1677 1076It is also worth noting that any instances created with an anon-class
0ac992ee 1077will keep a special reference to the anon-meta which will prevent the
1078anon-class from going out of scope until all instances of it have also
1079been destroyed. This however only works for HASH based instance types,
1080as we use a special reserved slot (C<__MOP__>) to store this.
d4ba1677 1081
66b3dded 1082=item B<initialize ($package_name, %options)>
552e3d24 1083
0ac992ee 1084This initializes and returns returns a B<Class::MOP::Class> object
a2e85e6c 1085for a given a C<$package_name>.
1086
66b3dded 1087=item B<reinitialize ($package_name, %options)>
1088
1089This removes the old metaclass, and creates a new one in it's place.
0ac992ee 1090Do B<not> use this unless you really know what you are doing, it could
1091very easily make a very large mess of your program.
66b3dded 1092
651955fb 1093=item B<construct_class_instance (%options)>
a2e85e6c 1094
0ac992ee 1095This will construct an instance of B<Class::MOP::Class>, it is
1096here so that we can actually "tie the knot" for B<Class::MOP::Class>
1097to use C<construct_instance> once all the bootstrapping is done. This
a2e85e6c 1098method is used internally by C<initialize> and should never be called
1099from outside of that method really.
552e3d24 1100
550d56db 1101=item B<check_metaclass_compatability>
1102
0ac992ee 1103This method is called as the very last thing in the
1104C<construct_class_instance> method. This will check that the
1105metaclass you are creating is compatible with the metaclasses of all
1106your ancestors. For more inforamtion about metaclass compatibility
550d56db 1107see the C<About Metaclass compatibility> section in L<Class::MOP>.
1108
715adbb7 1109=item B<update_package_cache_flag>
e0e4674a 1110
1111This will reset the package cache flag for this particular metaclass
1112it is basically the value of the C<Class::MOP::get_package_cache_flag>
1113function. This is very rarely needed from outside of C<Class::MOP::Class>
1114but in some cases you might want to use it, so it is here.
1115
715adbb7 1116=item B<reset_package_cache_flag>
1117
127d39a7 1118Clears the package cache flag to announce to the internals that we need
1119to rebuild the method map.
715adbb7 1120
552e3d24 1121=back
1122
c9e77dbb 1123=head2 Object instance construction and cloning
a2e85e6c 1124
0ac992ee 1125These methods are B<entirely optional>, it is up to you whether you want
c9e77dbb 1126to use them or not.
552e3d24 1127
1128=over 4
1129
2bab2be6 1130=item B<instance_metaclass>
1131
127d39a7 1132Returns the class name of the instance metaclass, see L<Class::MOP::Instance>
1133for more information on the instance metaclasses.
1134
2d711cc8 1135=item B<get_meta_instance>
1136
127d39a7 1137Returns an instance of L<Class::MOP::Instance> to be used in the construction
1138of a new instance of the class.
1139
5f3c057a 1140=item B<new_object (%params)>
1141
0ac992ee 1142This is a convience method for creating a new object of the class, and
1143blessing it into the appropriate package as well. Ideally your class
5f3c057a 1144would call a C<new> this method like so:
1145
0ac992ee 1146 sub MyClass::new {
5f3c057a 1147 my ($class, %param) = @_;
1148 $class->meta->new_object(%params);
1149 }
1150
cbd9f942 1151=item B<construct_instance (%params)>
552e3d24 1152
127d39a7 1153This method is used to construct an instance structure suitable for
0ac992ee 1154C<bless>-ing into your package of choice. It works in conjunction
c9e77dbb 1155with the Attribute protocol to collect all applicable attributes.
1156
0ac992ee 1157This will construct and instance using a HASH ref as storage
1158(currently only HASH references are supported). This will collect all
1159the applicable attributes and layout out the fields in the HASH ref,
1160it will then initialize them using either use the corresponding key
1161in C<%params> or any default value or initializer found in the
a2e85e6c 1162attribute meta-object.
727919c5 1163
5f3c057a 1164=item B<clone_object ($instance, %params)>
1165
0ac992ee 1166This is a convience method for cloning an object instance, then
1167blessing it into the appropriate package. This method will call
1168C<clone_instance>, which performs a shallow copy of the object,
1169see that methods documentation for more details. Ideally your
19d4b5b8 1170class would call a C<clone> this method like so:
5f3c057a 1171
1172 sub MyClass::clone {
1173 my ($self, %param) = @_;
1174 $self->meta->clone_object($self, %params);
1175 }
1176
c9e77dbb 1177=item B<clone_instance($instance, %params)>
1178
0ac992ee 1179This method is a compliment of C<construct_instance> (which means if
1180you override C<construct_instance>, you need to override this one too),
19d4b5b8 1181and clones the instance shallowly.
a27ae83f 1182
0ac992ee 1183The cloned structure returned is (like with C<construct_instance>) an
1184unC<bless>ed HASH reference, it is your responsibility to then bless
a27ae83f 1185this cloned structure into the right class (which C<clone_object> will
1186do for you).
c9e77dbb 1187
0ac992ee 1188As of 0.11, this method will clone the C<$instance> structure shallowly,
1189as opposed to the deep cloning implemented in prior versions. After much
1190thought, research and discussion, I have decided that anything but basic
1191shallow cloning is outside the scope of the meta-object protocol. I
1192think Yuval "nothingmuch" Kogman put it best when he said that cloning
19d4b5b8 1193is too I<context-specific> to be part of the MOP.
1194
214e4bd7 1195=item B<rebless_instance($instance, ?%params)>
69663c57 1196
1197This will change the class of C<$instance> to the class of the invoking
1198C<Class::MOP::Class>. You may only rebless the instance to a subclass of
214e4bd7 1199itself. You may pass in optional C<%params> which are like constructor
1200params and will override anything already defined in the instance.
69663c57 1201
552e3d24 1202=back
1203
0ac992ee 1204=head2 Informational
552e3d24 1205
b9d9fc0b 1206These are a few predicate methods for asking information about the class.
552e3d24 1207
b9d9fc0b 1208=over 4
552e3d24 1209
b9d9fc0b 1210=item B<is_anon_class>
552e3d24 1211
96e38ba6 1212This returns true if the class is a C<Class::MOP::Class> created anon class.
1213
b9d9fc0b 1214=item B<is_mutable>
552e3d24 1215
96e38ba6 1216This returns true if the class is still mutable.
1217
b9d9fc0b 1218=item B<is_immutable>
552e3d24 1219
96e38ba6 1220This returns true if the class has been made immutable.
1221
552e3d24 1222=back
1223
1224=head2 Inheritance Relationships
1225
1226=over 4
1227
1228=item B<superclasses (?@superclasses)>
1229
0ac992ee 1230This is a read-write attribute which represents the superclass
a2e85e6c 1231relationships of the class the B<Class::MOP::Class> instance is
1232associated with. Basically, it can get and set the C<@ISA> for you.
552e3d24 1233
1234=item B<class_precedence_list>
1235
0ac992ee 1236This computes the a list of all the class's ancestors in the same order
127d39a7 1237in which method dispatch will be done. This is similair to what
1238B<Class::ISA::super_path> does, but we don't remove duplicate names.
552e3d24 1239
b7bdffc3 1240=item B<linearized_isa>
1241
1242This returns a list based on C<class_precedence_list> but with all
1243duplicates removed.
1244
6c9f390e 1245=item B<subclasses>
1246
127d39a7 1247This returns a list of subclasses for this class.
6c9f390e 1248
552e3d24 1249=back
1250
1251=head2 Methods
1252
1253=over 4
1254
c4260b45 1255=item B<get_method_map>
1256
127d39a7 1257Returns a HASH ref of name to CODE reference mapping for this class.
1258
2e41896e 1259=item B<method_metaclass>
1260
127d39a7 1261Returns the class name of the method metaclass, see L<Class::MOP::Method>
1262for more information on the method metaclasses.
1263
552e3d24 1264=item B<add_method ($method_name, $method)>
1265
0ac992ee 1266This will take a C<$method_name> and CODE reference to that
1267C<$method> and install it into the class's package.
552e3d24 1268
0ac992ee 1269B<NOTE>:
1270This does absolutely nothing special to C<$method>
1271other than use B<Sub::Name> to make sure it is tagged with the
1272correct name, and therefore show up correctly in stack traces and
552e3d24 1273such.
1274
663f8198 1275=item B<alias_method ($method_name, $method)>
1276
0ac992ee 1277This will take a C<$method_name> and CODE reference to that
1278C<$method> and alias the method into the class's package.
663f8198 1279
0ac992ee 1280B<NOTE>:
1281Unlike C<add_method>, this will B<not> try to name the
1282C<$method> using B<Sub::Name>, it only aliases the method in
1283the class's package.
663f8198 1284
552e3d24 1285=item B<has_method ($method_name)>
1286
0ac992ee 1287This just provides a simple way to check if the class implements
1288a specific C<$method_name>. It will I<not> however, attempt to check
a2e85e6c 1289if the class inherits the method (use C<UNIVERSAL::can> for that).
552e3d24 1290
0ac992ee 1291This will correctly handle functions defined outside of the package
552e3d24 1292that use a fully qualified name (C<sub Package::name { ... }>).
1293
0ac992ee 1294This will correctly handle functions renamed with B<Sub::Name> and
1295installed using the symbol tables. However, if you are naming the
1296subroutine outside of the package scope, you must use the fully
1297qualified name, including the package name, for C<has_method> to
1298correctly identify it.
552e3d24 1299
0ac992ee 1300This will attempt to correctly ignore functions imported from other
1301packages using B<Exporter>. It breaks down if the function imported
1302is an C<__ANON__> sub (such as with C<use constant>), which very well
1303may be a valid method being applied to the class.
552e3d24 1304
0ac992ee 1305In short, this method cannot always be trusted to determine if the
1306C<$method_name> is actually a method. However, it will DWIM about
a2e85e6c 130790% of the time, so it's a small trade off I think.
552e3d24 1308
1309=item B<get_method ($method_name)>
1310
0ac992ee 1311This will return a Class::MOP::Method instance related to the specified
86482605 1312C<$method_name>, or return undef if that method does not exist.
1313
0ac992ee 1314The Class::MOP::Method is codifiable, so you can use it like a normal
86482605 1315CODE reference, see L<Class::MOP::Method> for more information.
552e3d24 1316
f9debadc 1317=item B<find_method_by_name ($method_name)>
16e960bd 1318
1319This will return a CODE reference of the specified C<$method_name>,
1320or return undef if that method does not exist.
1321
1322Unlike C<get_method> this will also look in the superclasses.
1323
552e3d24 1324=item B<remove_method ($method_name)>
1325
0ac992ee 1326This will attempt to remove a given C<$method_name> from the class.
1327It will return the CODE reference that it has removed, and will
552e3d24 1328attempt to use B<Sub::Name> to clear the methods associated name.
1329
1330=item B<get_method_list>
1331
0ac992ee 1332This will return a list of method names for all I<locally> defined
1333methods. It does B<not> provide a list of all applicable methods,
1334including any inherited ones. If you want a list of all applicable
552e3d24 1335methods, use the C<compute_all_applicable_methods> method.
1336
1337=item B<compute_all_applicable_methods>
1338
0ac992ee 1339This will return a list of all the methods names this class will
1340respond to, taking into account inheritance. The list will be a list of
1341HASH references, each one containing the following information; method
1342name, the name of the class in which the method lives and a CODE
552e3d24 1343reference for the actual method.
1344
1345=item B<find_all_methods_by_name ($method_name)>
1346
0ac992ee 1347This will traverse the inheritence hierarchy and locate all methods
1348with a given C<$method_name>. Similar to
1349C<compute_all_applicable_methods> it returns a list of HASH references
1350with the following information; method name (which will always be the
1351same as C<$method_name>), the name of the class in which the method
552e3d24 1352lives and a CODE reference for the actual method.
1353
0ac992ee 1354The list of methods produced is a distinct list, meaning there are no
1355duplicates in it. This is especially useful for things like object
1356initialization and destruction where you only want the method called
552e3d24 1357once, and in the correct order.
1358
96ceced8 1359=item B<find_next_method_by_name ($method_name)>
1360
0ac992ee 1361This will return the first method to match a given C<$method_name> in
1362the superclasses, this is basically equivalent to calling
96ceced8 1363C<SUPER::$method_name>, but it can be dispatched at runtime.
1364
552e3d24 1365=back
1366
a4258ffd 1367=head2 Method Modifiers
1368
0ac992ee 1369Method modifiers are a concept borrowed from CLOS, in which a method
1370can be wrapped with I<before>, I<after> and I<around> method modifiers
1371that will be called everytime the method is called.
96ceced8 1372
1373=head3 How method modifiers work?
1374
0ac992ee 1375Method modifiers work by wrapping the original method and then replacing
1376it in the classes symbol table. The wrappers will handle calling all the
1377modifiers in the appropariate orders and preserving the calling context
1378for the original method.
1379
1380Each method modifier serves a particular purpose, which may not be
1381obvious to users of other method wrapping modules. To start with, the
1382return values of I<before> and I<after> modifiers are ignored. This is
1383because thier purpose is B<not> to filter the input and output of the
1384primary method (this is done with an I<around> modifier). This may seem
1385like an odd restriction to some, but doing this allows for simple code
1386to be added at the begining or end of a method call without jeapordizing
1387the normal functioning of the primary method or placing any extra
1388responsibility on the code of the modifier. Of course if you have more
1389complex needs, then use the I<around> modifier, which uses a variation
1390of continutation passing style to allow for a high degree of flexibility.
1391
1392Before and around modifiers are called in last-defined-first-called order,
1393while after modifiers are called in first-defined-first-called order. So
96ceced8 1394the call tree might looks something like this:
0ac992ee 1395
96ceced8 1396 before 2
1397 before 1
1398 around 2
1399 around 1
1400 primary
1401 after 1
1402 after 2
1403
0ac992ee 1404To see examples of using method modifiers, see the following examples
1405included in the distribution; F<InstanceCountingClass>, F<Perl6Attribute>,
1406F<AttributesWithHistory> and F<C3MethodDispatchOrder>. There is also a
96ceced8 1407classic CLOS usage example in the test F<017_add_method_modifier.t>.
1408
1409=head3 What is the performance impact?
1410
0ac992ee 1411Of course there is a performance cost associated with method modifiers,
1412but we have made every effort to make that cost be directly proportional
96ceced8 1413to the amount of modifier features you utilize.
1414
0ac992ee 1415The wrapping method does it's best to B<only> do as much work as it
1416absolutely needs to. In order to do this we have moved some of the
96ceced8 1417performance costs to set-up time, where they are easier to amortize.
1418
1419All this said, my benchmarks have indicated the following:
1420
1421 simple wrapper with no modifiers 100% slower
1422 simple wrapper with simple before modifier 400% slower
1423 simple wrapper with simple after modifier 450% slower
1424 simple wrapper with simple around modifier 500-550% slower
1425 simple wrapper with all 3 modifiers 1100% slower
1426
0ac992ee 1427These numbers may seem daunting, but you must remember, every feature
1428comes with some cost. To put things in perspective, just doing a simple
96ceced8 1429C<AUTOLOAD> which does nothing but extract the name of the method called
0ac992ee 1430and return it costs about 400% over a normal method call.
96ceced8 1431
a4258ffd 1432=over 4
1433
1434=item B<add_before_method_modifier ($method_name, $code)>
1435
0ac992ee 1436This will wrap the method at C<$method_name> and the supplied C<$code>
1437will be passed the C<@_> arguments, and called before the original
1438method is called. As specified above, the return value of the I<before>
1439method modifiers is ignored, and it's ability to modify C<@_> is
1440fairly limited. If you need to do either of these things, use an
96ceced8 1441C<around> method modifier.
1442
a4258ffd 1443=item B<add_after_method_modifier ($method_name, $code)>
1444
0ac992ee 1445This will wrap the method at C<$method_name> so that the original
1446method will be called, it's return values stashed, and then the
96ceced8 1447supplied C<$code> will be passed the C<@_> arguments, and called.
0ac992ee 1448As specified above, the return value of the I<after> method
1449modifiers is ignored, and it cannot modify the return values of
1450the original method. If you need to do either of these things, use an
96ceced8 1451C<around> method modifier.
1452
a4258ffd 1453=item B<add_around_method_modifier ($method_name, $code)>
1454
0ac992ee 1455This will wrap the method at C<$method_name> so that C<$code>
1456will be called and passed the original method as an extra argument
1457at the begining of the C<@_> argument list. This is a variation of
1458continuation passing style, where the function prepended to C<@_>
1459can be considered a continuation. It is up to C<$code> if it calls
1460the original method or not, there is no restriction on what the
96ceced8 1461C<$code> can or cannot do.
1462
a4258ffd 1463=back
1464
552e3d24 1465=head2 Attributes
1466
0ac992ee 1467It should be noted that since there is no one consistent way to define
1468the attributes of a class in Perl 5. These methods can only work with
1469the information given, and can not easily discover information on
a2e85e6c 1470their own. See L<Class::MOP::Attribute> for more details.
552e3d24 1471
1472=over 4
1473
2e41896e 1474=item B<attribute_metaclass>
1475
127d39a7 1476Returns the class name of the attribute metaclass, see L<Class::MOP::Attribute>
1477for more information on the attribute metaclasses.
1478
7b31baf4 1479=item B<get_attribute_map>
1480
127d39a7 1481This returns a HASH ref of name to attribute meta-object mapping.
1482
1483=item B<add_attribute ($attribute_meta_object | ($attribute_name, %attribute_spec))>
552e3d24 1484
8203616d 1485This stores the C<$attribute_meta_object> (or creates one from the
0ac992ee 1486C<$attribute_name> and C<%attribute_spec>) in the B<Class::MOP::Class>
1487instance associated with the given class. Unlike methods, attributes
1488within the MOP are stored as meta-information only. They will be used
8203616d 1489later to construct instances from (see C<construct_instance> above).
0ac992ee 1490More details about the attribute meta-objects can be found in the
a2e85e6c 1491L<Class::MOP::Attribute> or the L<Class::MOP/The Attribute protocol>
1492section.
1493
0ac992ee 1494It should be noted that any accessor, reader/writer or predicate
1495methods which the C<$attribute_meta_object> has will be installed
a2e85e6c 1496into the class at this time.
552e3d24 1497
86482605 1498B<NOTE>
0ac992ee 1499If an attribute already exists for C<$attribute_name>, the old one
1500will be removed (as well as removing all it's accessors), and then
86482605 1501the new one added.
1502
552e3d24 1503=item B<has_attribute ($attribute_name)>
1504
0ac992ee 1505Checks to see if this class has an attribute by the name of
552e3d24 1506C<$attribute_name> and returns a boolean.
1507
1508=item B<get_attribute ($attribute_name)>
1509
0ac992ee 1510Returns the attribute meta-object associated with C<$attribute_name>,
1511if none is found, it will return undef.
552e3d24 1512
1513=item B<remove_attribute ($attribute_name)>
1514
0ac992ee 1515This will remove the attribute meta-object stored at
1516C<$attribute_name>, then return the removed attribute meta-object.
552e3d24 1517
0ac992ee 1518B<NOTE:>
1519Removing an attribute will only affect future instances of
1520the class, it will not make any attempt to remove the attribute from
552e3d24 1521any existing instances of the class.
1522
0ac992ee 1523It should be noted that any accessor, reader/writer or predicate
1524methods which the attribute meta-object stored at C<$attribute_name>
1525has will be removed from the class at this time. This B<will> make
1526these attributes somewhat inaccessable in previously created
1527instances. But if you are crazy enough to do this at runtime, then
a2e85e6c 1528you are crazy enough to deal with something like this :).
1529
552e3d24 1530=item B<get_attribute_list>
1531
0ac992ee 1532This returns a list of attribute names which are defined in the local
1533class. If you want a list of all applicable attributes for a class,
552e3d24 1534use the C<compute_all_applicable_attributes> method.
1535
1536=item B<compute_all_applicable_attributes>
1537
0ac992ee 1538This will traverse the inheritance heirachy and return a list of all
1539the applicable attributes for this class. It does not construct a
1540HASH reference like C<compute_all_applicable_methods> because all
1541that same information is discoverable through the attribute
c9e77dbb 1542meta-object itself.
552e3d24 1543
058c1cf5 1544=item B<find_attribute_by_name ($attr_name)>
1545
0ac992ee 1546This method will traverse the inheritance heirachy and find the
1547first attribute whose name matches C<$attr_name>, then return it.
058c1cf5 1548It will return undef if nothing is found.
1549
552e3d24 1550=back
1551
96e38ba6 1552=head2 Class Immutability
857f87a7 1553
1554=over 4
1555
96e38ba6 1556=item B<make_immutable (%options)>
1557
0ac992ee 1558This method will invoke a tranforamtion upon the class which will
1559make it immutable. Details of this transformation can be found in
96e38ba6 1560the L<Class::MOP::Immutable> documentation.
857f87a7 1561
0ac992ee 1562=item B<make_mutable>
1563
1564This method will reverse tranforamtion upon the class which
1565made it immutable.
1566
b817e248 1567=item B<create_immutable_transformer>
1568
1569Create a transformer suitable for making this class immutable
1570
857f87a7 1571=back
1572
1a09d9cc 1573=head1 AUTHORS
8b978dd5 1574
a2e85e6c 1575Stevan Little E<lt>stevan@iinteractive.comE<gt>
8b978dd5 1576
1577=head1 COPYRIGHT AND LICENSE
1578
69e3ab0a 1579Copyright 2006-2008 by Infinity Interactive, Inc.
8b978dd5 1580
1581L<http://www.iinteractive.com>
1582
1583This library is free software; you can redistribute it and/or modify
0ac992ee 1584it under the same terms as Perl itself.
8b978dd5 1585
798baea5 1586=cut