10 use Scalar::Util 'weaken';
12 use Sub::Identify 'get_code_info';
18 Sub::Name->import(qw(subname));
20 } or eval 'sub subname { $_[1] }';
22 # this is either part of core or set up appropriately by MRO::Compat
23 *check_package_cache_flag = \&mro::get_pkg_gen;
26 require Devel::GlobalDestruction;
27 Devel::GlobalDestruction->import("in_global_destruction");
29 } or *in_global_destruction = sub () { '' };
33 use Class::MOP::Class;
34 use Class::MOP::Attribute;
35 use Class::MOP::Method;
37 use Class::MOP::Immutable;
40 *IS_RUNNING_ON_5_10 = ($] < 5.009_005)
44 *HAVE_ISAREV = defined(&mro::get_isarev)
49 our $VERSION = '0.65';
50 our $AUTHORITY = 'cpan:STEVAN';
52 # after that everything is loaded, if we're allowed try to load faster XS
53 # versions of various things
54 unless ($ENV{CLASS_MOP_NO_XS}) {
59 __PACKAGE__->XSLoader::load($VERSION);
64 die $e if $e && $e !~ /object version|loadable object/;
68 # Metaclasses are singletons, so we cache them here.
69 # there is no need to worry about destruction though
70 # because they should die only when the program dies.
71 # After all, do package definitions even get reaped?
74 # means of accessing all the metaclasses that have
75 # been initialized thus far (for mugwumps obj browser)
76 sub get_all_metaclasses { %METAS }
77 sub get_all_metaclass_instances { values %METAS }
78 sub get_all_metaclass_names { keys %METAS }
79 sub get_metaclass_by_name { $METAS{$_[0]} }
80 sub store_metaclass_by_name { $METAS{$_[0]} = $_[1] }
81 sub weaken_metaclass { weaken($METAS{$_[0]}) }
82 sub does_metaclass_exist { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
83 sub remove_metaclass_by_name { $METAS{$_[0]} = undef }
86 # We only cache metaclasses, meaning instances of
87 # Class::MOP::Class. We do not cache instance of
88 # Class::MOP::Package or Class::MOP::Module. Mostly
89 # because I don't yet see a good reason to do so.
95 if (ref($class) || !defined($class) || !length($class)) {
96 my $display = defined($class) ? $class : 'undef';
97 confess "Invalid class name ($display)";
100 # if the class is not already loaded in the symbol table..
101 unless (is_class_loaded($class)) {
103 my $file = $class . '.pm';
105 my $e = do { local $@; eval { require($file) }; $@ };
106 confess "Could not load class ($class) because : $e" if $e;
109 # initialize a metaclass if necessary
110 unless (does_metaclass_exist($class)) {
111 my $e = do { local $@; eval { Class::MOP::Class->initialize($class) }; $@ };
112 confess "Could not initialize class ($class) because : $e" if $e;
115 return get_metaclass_by_name($class) if defined wantarray;
118 sub is_class_loaded {
121 return 0 if ref($class) || !defined($class) || !length($class);
123 # walk the symbol table tree to avoid autovififying
124 # \*{${main::}{"Foo::"}} == \*main::Foo::
127 foreach my $part (split('::', $class)) {
128 return 0 unless exists ${$$pack}{"${part}::"};
129 $pack = \*{${$$pack}{"${part}::"}};
132 # check for $VERSION or @ISA
133 return 1 if exists ${$$pack}{VERSION}
134 && defined *{${$$pack}{VERSION}}{SCALAR};
135 return 1 if exists ${$$pack}{ISA}
136 && defined *{${$$pack}{ISA}}{ARRAY};
138 # check for any method
139 foreach ( keys %{$$pack} ) {
140 next if substr($_, -2, 2) eq '::';
142 my $glob = ${$$pack}{$_} || next;
145 if ( IS_RUNNING_ON_5_10 ) {
146 return 1 if ref $glob eq 'SCALAR';
149 return 1 if defined *{$glob}{CODE};
157 ## ----------------------------------------------------------------------------
158 ## Setting up our environment ...
159 ## ----------------------------------------------------------------------------
160 ## Class::MOP needs to have a few things in the global perl environment so
161 ## that it can operate effectively. Those things are done here.
162 ## ----------------------------------------------------------------------------
164 # ... nothing yet actually ;)
166 ## ----------------------------------------------------------------------------
168 ## ----------------------------------------------------------------------------
169 ## The code below here is to bootstrap our MOP with itself. This is also
170 ## sometimes called "tying the knot". By doing this, we make it much easier
171 ## to extend the MOP through subclassing and such since now you can use the
172 ## MOP itself to extend itself.
174 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
175 ## ----------------------------------------------------------------------------
177 # We need to add in the meta-attributes here so that
178 # any subclass of Class::MOP::* will be able to
179 # inherit them using &construct_instance
181 ## --------------------------------------------------------
182 ## Class::MOP::Package
184 Class::MOP::Package->meta->add_attribute(
185 Class::MOP::Attribute->new('package' => (
187 # NOTE: we need to do this in order
188 # for the instance meta-object to
189 # not fall into meta-circular death
191 # we just alias the original method
192 # rather than re-produce it here
193 'name' => \&Class::MOP::Package::name
198 Class::MOP::Package->meta->add_attribute(
199 Class::MOP::Attribute->new('namespace' => (
202 # we just alias the original method
203 # rather than re-produce it here
204 'namespace' => \&Class::MOP::Package::namespace
207 default => sub { \undef }
212 # use the metaclass to construct the meta-package
213 # which is a superclass of the metaclass itself :P
214 Class::MOP::Package->meta->add_method('initialize' => sub {
216 my $package_name = shift;
217 $class->meta->new_object('package' => $package_name, @_);
220 ## --------------------------------------------------------
221 ## Class::MOP::Module
224 # yeah this is kind of stretching things a bit,
225 # but truthfully the version should be an attribute
226 # of the Module, the weirdness comes from having to
227 # stick to Perl 5 convention and store it in the
228 # $VERSION package variable. Basically if you just
229 # squint at it, it will look how you want it to look.
230 # Either as a package variable, or as a attribute of
231 # the metaclass, isn't abstraction great :)
233 Class::MOP::Module->meta->add_attribute(
234 Class::MOP::Attribute->new('version' => (
237 # we just alias the original method
238 # rather than re-produce it here
239 'version' => \&Class::MOP::Module::version
242 default => sub { \undef }
247 # By following the same conventions as version here,
248 # we are opening up the possibility that people can
249 # use the $AUTHORITY in non-Class::MOP modules as
252 Class::MOP::Module->meta->add_attribute(
253 Class::MOP::Attribute->new('authority' => (
256 # we just alias the original method
257 # rather than re-produce it here
258 'authority' => \&Class::MOP::Module::authority
261 default => sub { \undef }
265 ## --------------------------------------------------------
268 Class::MOP::Class->meta->add_attribute(
269 Class::MOP::Attribute->new('attributes' => (
271 # NOTE: we need to do this in order
272 # for the instance meta-object to
273 # not fall into meta-circular death
275 # we just alias the original method
276 # rather than re-produce it here
277 'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
279 default => sub { {} }
283 Class::MOP::Class->meta->add_attribute(
284 Class::MOP::Attribute->new('methods' => (
287 # we just alias the original method
288 # rather than re-produce it here
289 'get_method_map' => \&Class::MOP::Class::get_method_map
291 default => sub { {} }
295 Class::MOP::Class->meta->add_attribute(
296 Class::MOP::Attribute->new('superclasses' => (
299 # we just alias the original method
300 # rather than re-produce it here
301 'superclasses' => \&Class::MOP::Class::superclasses
304 default => sub { \undef }
308 Class::MOP::Class->meta->add_attribute(
309 Class::MOP::Attribute->new('attribute_metaclass' => (
312 # we just alias the original method
313 # rather than re-produce it here
314 'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
316 default => 'Class::MOP::Attribute',
320 Class::MOP::Class->meta->add_attribute(
321 Class::MOP::Attribute->new('method_metaclass' => (
324 # we just alias the original method
325 # rather than re-produce it here
326 'method_metaclass' => \&Class::MOP::Class::method_metaclass
328 default => 'Class::MOP::Method',
332 Class::MOP::Class->meta->add_attribute(
333 Class::MOP::Attribute->new('instance_metaclass' => (
335 # NOTE: we need to do this in order
336 # for the instance meta-object to
337 # not fall into meta-circular death
339 # we just alias the original method
340 # rather than re-produce it here
341 'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
343 default => 'Class::MOP::Instance',
348 # we don't actually need to tie the knot with
349 # Class::MOP::Class here, it is actually handled
350 # within Class::MOP::Class itself in the
351 # construct_class_instance method.
353 ## --------------------------------------------------------
354 ## Class::MOP::Attribute
356 Class::MOP::Attribute->meta->add_attribute(
357 Class::MOP::Attribute->new('name' => (
359 # NOTE: we need to do this in order
360 # for the instance meta-object to
361 # not fall into meta-circular death
363 # we just alias the original method
364 # rather than re-produce it here
365 'name' => \&Class::MOP::Attribute::name
370 Class::MOP::Attribute->meta->add_attribute(
371 Class::MOP::Attribute->new('associated_class' => (
373 # NOTE: we need to do this in order
374 # for the instance meta-object to
375 # not fall into meta-circular death
377 # we just alias the original method
378 # rather than re-produce it here
379 'associated_class' => \&Class::MOP::Attribute::associated_class
384 Class::MOP::Attribute->meta->add_attribute(
385 Class::MOP::Attribute->new('accessor' => (
386 reader => { 'accessor' => \&Class::MOP::Attribute::accessor },
387 predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
391 Class::MOP::Attribute->meta->add_attribute(
392 Class::MOP::Attribute->new('reader' => (
393 reader => { 'reader' => \&Class::MOP::Attribute::reader },
394 predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
398 Class::MOP::Attribute->meta->add_attribute(
399 Class::MOP::Attribute->new('initializer' => (
400 reader => { 'initializer' => \&Class::MOP::Attribute::initializer },
401 predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
405 Class::MOP::Attribute->meta->add_attribute(
406 Class::MOP::Attribute->new('writer' => (
407 reader => { 'writer' => \&Class::MOP::Attribute::writer },
408 predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
412 Class::MOP::Attribute->meta->add_attribute(
413 Class::MOP::Attribute->new('predicate' => (
414 reader => { 'predicate' => \&Class::MOP::Attribute::predicate },
415 predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
419 Class::MOP::Attribute->meta->add_attribute(
420 Class::MOP::Attribute->new('clearer' => (
421 reader => { 'clearer' => \&Class::MOP::Attribute::clearer },
422 predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
426 Class::MOP::Attribute->meta->add_attribute(
427 Class::MOP::Attribute->new('builder' => (
428 reader => { 'builder' => \&Class::MOP::Attribute::builder },
429 predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
433 Class::MOP::Attribute->meta->add_attribute(
434 Class::MOP::Attribute->new('init_arg' => (
435 reader => { 'init_arg' => \&Class::MOP::Attribute::init_arg },
436 predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
440 Class::MOP::Attribute->meta->add_attribute(
441 Class::MOP::Attribute->new('default' => (
442 # default has a custom 'reader' method ...
443 predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
447 Class::MOP::Attribute->meta->add_attribute(
448 Class::MOP::Attribute->new('associated_methods' => (
449 reader => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
450 default => sub { [] }
454 # NOTE: (meta-circularity)
455 # This should be one of the last things done
456 # it will "tie the knot" with Class::MOP::Attribute
457 # so that it uses the attributes meta-objects
458 # to construct itself.
459 Class::MOP::Attribute->meta->add_method('new' => sub {
460 my ( $class, @args ) = @_;
462 unshift @args, "name" if @args % 2 == 1;
465 my $name = $options{name};
467 (defined $name && $name)
468 || confess "You must provide a name for the attribute";
469 $options{init_arg} = $name
470 if not exists $options{init_arg};
472 if(exists $options{builder}){
473 confess("builder must be a defined scalar value which is a method name")
474 if ref $options{builder} || !(defined $options{builder});
475 confess("Setting both default and builder is not allowed.")
476 if exists $options{default};
478 (Class::MOP::Attribute::is_default_a_coderef(\%options))
479 || confess("References are not allowed as default values, you must ".
480 "wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
481 if exists $options{default} && ref $options{default};
484 # return the new object
485 $class->meta->new_object(%options);
488 Class::MOP::Attribute->meta->add_method('clone' => sub {
490 $self->meta->clone_object($self, @_);
493 ## --------------------------------------------------------
494 ## Class::MOP::Method
495 Class::MOP::Method->meta->add_attribute(
496 Class::MOP::Attribute->new('body' => (
497 reader => { 'body' => \&Class::MOP::Method::body },
501 Class::MOP::Method->meta->add_attribute(
502 Class::MOP::Attribute->new('associated_metaclass' => (
503 reader => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
507 Class::MOP::Method->meta->add_attribute(
508 Class::MOP::Attribute->new('package_name' => (
509 reader => { 'package_name' => \&Class::MOP::Method::package_name },
513 Class::MOP::Method->meta->add_attribute(
514 Class::MOP::Attribute->new('name' => (
515 reader => { 'name' => \&Class::MOP::Method::name },
519 # FIMXE prime candidate for immutablization
520 Class::MOP::Method->meta->add_method('wrap' => sub {
521 my ( $class, @args ) = @_;
523 unshift @args, 'body' if @args % 2 == 1;
526 my $code = $options{body};
528 ('CODE' eq ref($code))
529 || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
531 ($options{package_name} && $options{name})
532 || confess "You must supply the package_name and name parameters";
534 # return the new object
535 $class->meta->new_object(%options);
538 Class::MOP::Method->meta->add_method('clone' => sub {
540 $self->meta->clone_object($self, @_);
543 ## --------------------------------------------------------
544 ## Class::MOP::Method::Wrapped
547 # the way this item is initialized, this
548 # really does not follow the standard
549 # practices of attributes, but we put
550 # it here for completeness
551 Class::MOP::Method::Wrapped->meta->add_attribute(
552 Class::MOP::Attribute->new('modifier_table')
555 ## --------------------------------------------------------
556 ## Class::MOP::Method::Generated
558 Class::MOP::Method::Generated->meta->add_attribute(
559 Class::MOP::Attribute->new('is_inline' => (
560 reader => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
565 Class::MOP::Method::Generated->meta->add_method('new' => sub {
566 my ($class, %options) = @_;
567 ($options{package_name} && $options{name})
568 || confess "You must supply the package_name and name parameters";
569 my $self = $class->meta->new_object(%options);
570 $self->initialize_body;
574 ## --------------------------------------------------------
575 ## Class::MOP::Method::Accessor
577 Class::MOP::Method::Accessor->meta->add_attribute(
578 Class::MOP::Attribute->new('attribute' => (
580 'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
585 Class::MOP::Method::Accessor->meta->add_attribute(
586 Class::MOP::Attribute->new('accessor_type' => (
587 reader => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
591 Class::MOP::Method::Accessor->meta->add_method('new' => sub {
595 (exists $options{attribute})
596 || confess "You must supply an attribute to construct with";
598 (exists $options{accessor_type})
599 || confess "You must supply an accessor_type to construct with";
601 (Scalar::Util::blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
602 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
604 ($options{package_name} && $options{name})
605 || confess "You must supply the package_name and name parameters";
607 # return the new object
608 my $self = $class->meta->new_object(%options);
610 # we don't want this creating
611 # a cycle in the code, if not
613 Scalar::Util::weaken($self->{'attribute'});
615 $self->initialize_body;
621 ## --------------------------------------------------------
622 ## Class::MOP::Method::Constructor
624 Class::MOP::Method::Constructor->meta->add_attribute(
625 Class::MOP::Attribute->new('options' => (
627 'options' => \&Class::MOP::Method::Constructor::options
629 default => sub { +{} }
633 Class::MOP::Method::Constructor->meta->add_attribute(
634 Class::MOP::Attribute->new('associated_metaclass' => (
635 init_arg => "metaclass", # FIXME alias and rename
637 'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
642 Class::MOP::Method::Constructor->meta->add_method('new' => sub {
646 (Scalar::Util::blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class'))
647 || confess "You must pass a metaclass instance if you want to inline"
648 if $options{is_inline};
650 ($options{package_name} && $options{name})
651 || confess "You must supply the package_name and name parameters";
653 # return the new object
654 my $self = $class->meta->new_object(%options);
656 # we don't want this creating
657 # a cycle in the code, if not
659 Scalar::Util::weaken($self->{'associated_metaclass'});
661 $self->initialize_body;
666 ## --------------------------------------------------------
667 ## Class::MOP::Instance
670 # these don't yet do much of anything, but are just
671 # included for completeness
673 Class::MOP::Instance->meta->add_attribute(
674 Class::MOP::Attribute->new('associated_metaclass',
675 reader => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
679 Class::MOP::Instance->meta->add_attribute(
680 Class::MOP::Attribute->new('_class_name',
682 reader => { _class_name => \&Class::MOP::Instance::_class_name },
683 #lazy => 1, # not yet supported by Class::MOP but out our version does it anyway
684 #default => sub { $_[0]->associated_metaclass->name },
688 Class::MOP::Instance->meta->add_attribute(
689 Class::MOP::Attribute->new('attributes',
690 reader => { attributes => \&Class::MOP::Instance::attributes },
694 Class::MOP::Instance->meta->add_attribute(
695 Class::MOP::Attribute->new('slots',
696 reader => { slots => \&Class::MOP::Instance::slots },
700 Class::MOP::Instance->meta->add_attribute(
701 Class::MOP::Attribute->new('slot_hash',
702 reader => { slot_hash => \&Class::MOP::Instance::slot_hash },
707 # we need the meta instance of the meta instance to be created now, in order
708 # for the constructor to be able to use it
709 Class::MOP::Instance->meta->get_meta_instance;
711 Class::MOP::Instance->meta->add_method('new' => sub {
713 my $options = $class->BUILDARGS(@_);
715 my $self = $class->meta->new_object(%$options);
717 Scalar::Util::weaken($self->{'associated_metaclass'});
722 # pretend the add_method never happenned. it hasn't yet affected anything
723 undef Class::MOP::Instance->meta->{_package_cache_flag};
725 ## --------------------------------------------------------
726 ## Now close all the Class::MOP::* classes
729 # we don't need to inline the
730 # constructors or the accessors
731 # this only lengthens the compile
732 # time of the MOP, and gives us
733 # no actual benefits.
735 $_->meta->make_immutable(
736 ( $_->can("_new") ? (
737 inline_constructor => 1,
738 constructor_name => "_new",
740 inline_constructor => 0,
742 inline_accessors => 0,
748 Class::MOP::Attribute
754 Class::MOP::Method::Generated
756 Class::MOP::Method::Accessor
757 Class::MOP::Method::Constructor
758 Class::MOP::Method::Wrapped
769 Class::MOP - A Meta Object Protocol for Perl 5
773 This module is a fully functioning meta object protocol for the
774 Perl 5 object system. It makes no attempt to change the behavior or
775 characteristics of the Perl 5 object system, only to create a
776 protocol for its manipulation and introspection.
778 That said, it does attempt to create the tools for building a rich
779 set of extensions to the Perl 5 object system. Every attempt has been
780 made for these tools to keep to the spirit of the Perl 5 object
781 system that we all know and love.
783 This documentation is admittedly sparse on details, as time permits
784 I will try to improve them. For now, I suggest looking at the items
785 listed in the L<SEE ALSO> section for more information. In particular
786 the book "The Art of the Meta Object Protocol" was very influential
787 in the development of this system.
789 =head2 What is a Meta Object Protocol?
791 A meta object protocol is an API to an object system.
793 To be more specific, it is a set of abstractions of the components of
794 an object system (typically things like; classes, object, methods,
795 object attributes, etc.). These abstractions can then be used to both
796 inspect and manipulate the object system which they describe.
798 It can be said that there are two MOPs for any object system; the
799 implicit MOP, and the explicit MOP. The implicit MOP handles things
800 like method dispatch or inheritance, which happen automatically as
801 part of how the object system works. The explicit MOP typically
802 handles the introspection/reflection features of the object system.
803 All object systems have implicit MOPs, without one, they would not
804 work. Explict MOPs however as less common, and depending on the
805 language can vary from restrictive (Reflection in Java or C#) to
806 wide open (CLOS is a perfect example).
808 =head2 Yet Another Class Builder!! Why?
810 This is B<not> a class builder so much as it is a I<class builder
811 B<builder>>. My intent is that an end user does not use this module
812 directly, but instead this module is used by module authors to
813 build extensions and features onto the Perl 5 object system.
815 =head2 Who is this module for?
817 This module is specifically for anyone who has ever created or
818 wanted to create a module for the Class:: namespace. The tools which
819 this module will provide will hopefully make it easier to do more
820 complex things with Perl 5 classes by removing such barriers as
821 the need to hack the symbol tables, or understand the fine details
824 =head2 What changes do I have to make to use this module?
826 This module was designed to be as unintrusive as possible. Many of
827 its features are accessible without B<any> change to your existsing
828 code at all. It is meant to be a compliment to your existing code and
829 not an intrusion on your code base. Unlike many other B<Class::>
830 modules, this module B<does not> require you subclass it, or even that
831 you C<use> it in within your module's package.
833 The only features which requires additions to your code are the
834 attribute handling and instance construction features, and these are
835 both completely optional features. The only reason for this is because
836 Perl 5's object system does not actually have these features built
837 in. More information about this feature can be found below.
839 =head2 A Note about Performance?
841 It is a common misconception that explict MOPs are performance drains.
842 But this is not a universal truth at all, it is an side-effect of
843 specific implementations. For instance, using Java reflection is much
844 slower because the JVM cannot take advantage of any compiler
845 optimizations, and the JVM has to deal with much more runtime type
846 information as well. Reflection in C# is marginally better as it was
847 designed into the language and runtime (the CLR). In contrast, CLOS
848 (the Common Lisp Object System) was built to support an explicit MOP,
849 and so performance is tuned for it.
851 This library in particular does it's absolute best to avoid putting
852 B<any> drain at all upon your code's performance. In fact, by itself
853 it does nothing to affect your existing code. So you only pay for
854 what you actually use.
856 =head2 About Metaclass compatibility
858 This module makes sure that all metaclasses created are both upwards
859 and downwards compatible. The topic of metaclass compatibility is
860 highly esoteric and is something only encountered when doing deep and
861 involved metaclass hacking. There are two basic kinds of metaclass
862 incompatibility; upwards and downwards.
864 Upwards metaclass compatibility means that the metaclass of a
865 given class is either the same as (or a subclass of) all of the
868 Downward metaclass compatibility means that the metaclasses of a
869 given class's anscestors are all either the same as (or a subclass
872 Here is a diagram showing a set of two classes (C<A> and C<B>) and
873 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
874 metaclass compatibility both upwards and downwards.
876 +---------+ +---------+
877 | Meta::A |<----| Meta::B | <....... (instance of )
878 +---------+ +---------+ <------- (inherits from)
881 +---------+ +---------+
883 +---------+ +---------+
885 As I said this is a highly esoteric topic and one you will only run
886 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
887 are interested in why this is an issue see the paper
888 I<Uniform and safe metaclass composition> linked to in the
889 L<SEE ALSO> section of this document.
891 =head2 Using custom metaclasses
893 Always use the metaclass pragma when using a custom metaclass, this
894 will ensure the proper initialization order and not accidentely
895 create an incorrect type of metaclass for you. This is a very rare
896 problem, and one which can only occur if you are doing deep metaclass
897 programming. So in other words, don't worry about it.
901 The protocol is divided into 4 main sub-protocols:
905 =item The Class protocol
907 This provides a means of manipulating and introspecting a Perl 5
908 class. It handles all of symbol table hacking for you, and provides
909 a rich set of methods that go beyond simple package introspection.
911 See L<Class::MOP::Class> for more details.
913 =item The Attribute protocol
915 This provides a consistent represenation for an attribute of a
916 Perl 5 class. Since there are so many ways to create and handle
917 attributes in Perl 5 OO, this attempts to provide as much of a
918 unified approach as possible, while giving the freedom and
919 flexibility to subclass for specialization.
921 See L<Class::MOP::Attribute> for more details.
923 =item The Method protocol
925 This provides a means of manipulating and introspecting methods in
926 the Perl 5 object system. As with attributes, there are many ways to
927 approach this topic, so we try to keep it pretty basic, while still
928 making it possible to extend the system in many ways.
930 See L<Class::MOP::Method> for more details.
932 =item The Instance protocol
934 This provides a layer of abstraction for creating object instances.
935 Since the other layers use this protocol, it is relatively easy to
936 change the type of your instances from the default HASH ref to other
937 types of references. Several examples are provided in the F<examples/>
938 directory included in this distribution.
940 See L<Class::MOP::Instance> for more details.
950 =item I<IS_RUNNING_ON_5_10>
952 We set this constant depending on what version perl we are on, this
953 allows us to take advantage of new 5.10 features and stay backwards
958 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
959 subclasses of a certain class.
963 =head2 Utility functions
967 =item B<load_class ($class_name)>
969 This will load a given C<$class_name> and if it does not have an
970 already initialized metaclass, then it will intialize one for it.
971 This function can be used in place of tricks like
972 C<eval "use $module"> or using C<require>.
974 =item B<is_class_loaded ($class_name)>
976 This will return a boolean depending on if the C<$class_name> has
979 NOTE: This does a basic check of the symbol table to try and
980 determine as best it can if the C<$class_name> is loaded, it
981 is probably correct about 99% of the time.
983 =item B<check_package_cache_flag ($pkg)>
985 This will return an integer that is managed by C<Class::MOP::Class>
986 to determine if a module's symbol table has been altered.
988 In Perl 5.10 or greater, this flag is package specific. However in
989 versions prior to 5.10, this will use the C<PL_sub_generation> variable
990 which is not package specific.
992 =item B<get_code_info ($code)>
994 This function returns two values, the name of the package the C<$code>
995 is from and the name of the C<$code> itself. This is used by several
996 elements of the MOP to detemine where a given C<$code> reference is from.
998 =item B<subname ($name, $code)>
1000 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
1002 If possible, we will load the L<Sub::Name> module and this will function
1003 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
1006 =item B<in_global_destruction>
1008 If L<Devel::GlobalDestruction> is available, this returns true under global
1011 Otherwise it's a constant returning false.
1015 =head2 Metaclass cache functions
1017 Class::MOP holds a cache of metaclasses, the following are functions
1018 (B<not methods>) which can be used to access that cache. It is not
1019 recommended that you mess with this, bad things could happen. But if
1020 you are brave and willing to risk it, go for it.
1024 =item B<get_all_metaclasses>
1026 This will return an hash of all the metaclass instances that have
1027 been cached by B<Class::MOP::Class> keyed by the package name.
1029 =item B<get_all_metaclass_instances>
1031 This will return an array of all the metaclass instances that have
1032 been cached by B<Class::MOP::Class>.
1034 =item B<get_all_metaclass_names>
1036 This will return an array of all the metaclass names that have
1037 been cached by B<Class::MOP::Class>.
1039 =item B<get_metaclass_by_name ($name)>
1041 This will return a cached B<Class::MOP::Class> instance of nothing
1042 if no metaclass exist by that C<$name>.
1044 =item B<store_metaclass_by_name ($name, $meta)>
1046 This will store a metaclass in the cache at the supplied C<$key>.
1048 =item B<weaken_metaclass ($name)>
1050 In rare cases it is desireable to store a weakened reference in
1051 the metaclass cache. This function will weaken the reference to
1052 the metaclass stored in C<$name>.
1054 =item B<does_metaclass_exist ($name)>
1056 This will return true of there exists a metaclass stored in the
1057 C<$name> key and return false otherwise.
1059 =item B<remove_metaclass_by_name ($name)>
1061 This will remove a the metaclass stored in the C<$name> key.
1069 There are very few books out on Meta Object Protocols and Metaclasses
1070 because it is such an esoteric topic. The following books are really
1071 the only ones I have found. If you know of any more, B<I<please>>
1072 email me and let me know, I would love to hear about them.
1076 =item "The Art of the Meta Object Protocol"
1078 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
1080 =item "Putting MetaClasses to Work"
1082 =item "Smalltalk: The Language"
1090 =item Uniform and safe metaclass composition
1092 An excellent paper by the people who brought us the original Traits paper.
1093 This paper is on how Traits can be used to do safe metaclass composition,
1094 and offers an excellent introduction section which delves into the topic of
1095 metaclass compatibility.
1097 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
1099 =item Safe Metaclass Programming
1101 This paper seems to precede the above paper, and propose a mix-in based
1102 approach as opposed to the Traits based approach. Both papers have similar
1103 information on the metaclass compatibility problem space.
1105 L<http://citeseer.ist.psu.edu/37617.html>
1113 =item The Perl 6 MetaModel work in the Pugs project
1117 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1119 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1129 =item CPAN Module Review of Class::MOP
1131 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1135 =head1 SIMILAR MODULES
1137 As I have said above, this module is a class-builder-builder, so it is
1138 not the same thing as modules like L<Class::Accessor> and
1139 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1140 with similar goals to this module. The one I have found which is most
1141 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1142 creates are very different from this modules.
1146 All complex software has bugs lurking in it, and this module is no
1147 exception. If you find a bug please either email me, or add the bug
1150 =head1 ACKNOWLEDGEMENTS
1156 Thanks to Rob for actually getting the development of this module kick-started.
1162 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1164 B<with contributions from:>
1166 Brandon (blblack) Black
1168 Guillermo (groditi) Roditi
1172 Rob (robkinyon) Kinyon
1174 Yuval (nothingmuch) Kogman
1176 Scott (konobi) McWhirter
1178 =head1 COPYRIGHT AND LICENSE
1180 Copyright 2006-2008 by Infinity Interactive, Inc.
1182 L<http://www.iinteractive.com>
1184 This library is free software; you can redistribute it and/or modify
1185 it under the same terms as Perl itself.