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