Clarify public-ness of Class::MOP functions
[gitmo/Class-MOP.git] / lib / Class / MOP.pm
1
2 package Class::MOP;
3
4 use strict;
5 use warnings;
6
7 use 5.008;
8
9 use MRO::Compat;
10
11 use Carp          'confess';
12 use Scalar::Util  'weaken';
13
14 BEGIN {
15     local $@;
16     eval {
17         require Sub::Name;
18         Sub::Name->import(qw(subname));
19         1
20     } or eval 'sub subname { $_[1] }';
21
22     # this is either part of core or set up appropriately by MRO::Compat
23     *check_package_cache_flag = \&mro::get_pkg_gen;
24
25     eval {
26         require Devel::GlobalDestruction;
27         Devel::GlobalDestruction->import("in_global_destruction");
28         1;
29     } or *in_global_destruction = sub () { !1 };
30 }
31
32
33 use Class::MOP::Class;
34 use Class::MOP::Attribute;
35 use Class::MOP::Method;
36
37 use Class::MOP::Immutable;
38
39 BEGIN {
40     *IS_RUNNING_ON_5_10 = ($] < 5.009_005) 
41         ? sub () { 0 }
42         : sub () { 1 };    
43
44     *HAVE_ISAREV = defined(&mro::get_isarev)
45         ? sub () { 1 }
46         : sub () { 1 };
47 }
48
49 our $VERSION   = '0.65';
50 our $XS_VERSION = $VERSION;
51 $VERSION = eval $VERSION;
52 our $AUTHORITY = 'cpan:STEVAN';    
53     
54 # after that everything is loaded, if we're allowed try to load faster XS
55 # versions of various things
56 _try_load_xs() or _load_pure_perl();
57
58 sub _try_load_xs {
59     return if $ENV{CLASS_MOP_NO_XS};
60
61     my $e = do {
62         local $@;
63         eval {
64             require XSLoader;
65             # just doing this - no warnings 'redefine' - doesn't work
66             # for some reason
67             local $^W = 0;
68             __PACKAGE__->XSLoader::load($XS_VERSION);
69         };
70         $@;
71     };
72
73     die $e if $e && $e !~ /object version|loadable object/;
74
75     return $e ? 0 : 1;
76 }
77
78 sub _load_pure_perl {
79     require Sub::Identify;
80     Sub::Identify->import('get_code_info');
81 }
82
83
84 {
85     # Metaclasses are singletons, so we cache them here.
86     # there is no need to worry about destruction though
87     # because they should die only when the program dies.
88     # After all, do package definitions even get reaped?
89     my %METAS;
90
91     # means of accessing all the metaclasses that have
92     # been initialized thus far (for mugwumps obj browser)
93     sub get_all_metaclasses         {        %METAS         }
94     sub get_all_metaclass_instances { values %METAS         }
95     sub get_all_metaclass_names     { keys   %METAS         }
96     sub get_metaclass_by_name       { $METAS{$_[0]}         }
97     sub store_metaclass_by_name     { $METAS{$_[0]} = $_[1] }
98     sub weaken_metaclass            { weaken($METAS{$_[0]}) }
99     sub does_metaclass_exist        { exists $METAS{$_[0]} && defined $METAS{$_[0]} }
100     sub remove_metaclass_by_name    { $METAS{$_[0]} = undef }
101
102     # NOTE:
103     # We only cache metaclasses, meaning instances of
104     # Class::MOP::Class. We do not cache instance of
105     # Class::MOP::Package or Class::MOP::Module. Mostly
106     # because I don't yet see a good reason to do so.
107 }
108
109 sub load_class {
110     my $class = shift;
111
112     unless ( _is_valid_class_name($class) ) {
113         my $display = defined($class) ? $class : 'undef';
114         confess "Invalid class name ($display)";
115     }
116
117     # if the class is not already loaded in the symbol table..
118     unless (is_class_loaded($class)) {
119         # require it
120         my $e = do { local $@; eval "require $class"; $@ };
121         confess "Could not load class ($class) because : $e" if $e;
122     }
123
124     get_metaclass_by_name($class) || $class if defined wantarray;
125 }
126
127 sub _is_valid_class_name {
128     my $class = shift;
129
130     return 0 if ref($class);
131     return 0 unless defined($class);
132     return 0 unless length($class);
133
134     return 1 if $class =~ /^\w+(?:::\w+)*$/;
135
136     return 0;
137 }
138
139 sub is_class_loaded {
140     my $class = shift;
141
142     return 0 if ref($class) || !defined($class) || !length($class);
143
144     # walk the symbol table tree to avoid autovififying
145     # \*{${main::}{"Foo::"}} == \*main::Foo::
146
147     my $pack = \*::;
148     foreach my $part (split('::', $class)) {
149         return 0 unless exists ${$$pack}{"${part}::"};
150         $pack = \*{${$$pack}{"${part}::"}};
151     }
152
153     # check for $VERSION or @ISA
154     return 1 if exists ${$$pack}{VERSION}
155              && defined *{${$$pack}{VERSION}}{SCALAR};
156     return 1 if exists ${$$pack}{ISA}
157              && defined *{${$$pack}{ISA}}{ARRAY};
158
159     # check for any method
160     foreach ( keys %{$$pack} ) {
161         next if substr($_, -2, 2) eq '::';
162
163         my $glob = ${$$pack}{$_} || next;
164
165         # constant subs
166         if ( IS_RUNNING_ON_5_10 ) {
167             return 1 if ref $glob eq 'SCALAR';
168         }
169
170         return 1 if defined *{$glob}{CODE};
171     }
172
173     # fail
174     return 0;
175 }
176
177
178 ## ----------------------------------------------------------------------------
179 ## Setting up our environment ...
180 ## ----------------------------------------------------------------------------
181 ## Class::MOP needs to have a few things in the global perl environment so
182 ## that it can operate effectively. Those things are done here.
183 ## ----------------------------------------------------------------------------
184
185 # ... nothing yet actually ;)
186
187 ## ----------------------------------------------------------------------------
188 ## Bootstrapping
189 ## ----------------------------------------------------------------------------
190 ## The code below here is to bootstrap our MOP with itself. This is also
191 ## sometimes called "tying the knot". By doing this, we make it much easier
192 ## to extend the MOP through subclassing and such since now you can use the
193 ## MOP itself to extend itself.
194 ##
195 ## Yes, I know, thats weird and insane, but it's a good thing, trust me :)
196 ## ----------------------------------------------------------------------------
197
198 # We need to add in the meta-attributes here so that
199 # any subclass of Class::MOP::* will be able to
200 # inherit them using &construct_instance
201
202 ## --------------------------------------------------------
203 ## Class::MOP::Package
204
205 Class::MOP::Package->meta->add_attribute(
206     Class::MOP::Attribute->new('package' => (
207         reader   => {
208             # NOTE: we need to do this in order
209             # for the instance meta-object to
210             # not fall into meta-circular death
211             #
212             # we just alias the original method
213             # rather than re-produce it here
214             'name' => \&Class::MOP::Package::name
215         },
216     ))
217 );
218
219 Class::MOP::Package->meta->add_attribute(
220     Class::MOP::Attribute->new('namespace' => (
221         reader => {
222             # NOTE:
223             # we just alias the original method
224             # rather than re-produce it here
225             'namespace' => \&Class::MOP::Package::namespace
226         },
227         init_arg => undef,
228         default  => sub { \undef }
229     ))
230 );
231
232 ## --------------------------------------------------------
233 ## Class::MOP::Module
234
235 # NOTE:
236 # yeah this is kind of stretching things a bit,
237 # but truthfully the version should be an attribute
238 # of the Module, the weirdness comes from having to
239 # stick to Perl 5 convention and store it in the
240 # $VERSION package variable. Basically if you just
241 # squint at it, it will look how you want it to look.
242 # Either as a package variable, or as a attribute of
243 # the metaclass, isn't abstraction great :)
244
245 Class::MOP::Module->meta->add_attribute(
246     Class::MOP::Attribute->new('version' => (
247         reader => {
248             # NOTE:
249             # we just alias the original method
250             # rather than re-produce it here
251             'version' => \&Class::MOP::Module::version
252         },
253         init_arg => undef,
254         default  => sub { \undef }
255     ))
256 );
257
258 # NOTE:
259 # By following the same conventions as version here,
260 # we are opening up the possibility that people can
261 # use the $AUTHORITY in non-Class::MOP modules as
262 # well.
263
264 Class::MOP::Module->meta->add_attribute(
265     Class::MOP::Attribute->new('authority' => (
266         reader => {
267             # NOTE:
268             # we just alias the original method
269             # rather than re-produce it here
270             'authority' => \&Class::MOP::Module::authority
271         },
272         init_arg => undef,
273         default  => sub { \undef }
274     ))
275 );
276
277 ## --------------------------------------------------------
278 ## Class::MOP::Class
279
280 Class::MOP::Class->meta->add_attribute(
281     Class::MOP::Attribute->new('attributes' => (
282         reader   => {
283             # NOTE: we need to do this in order
284             # for the instance meta-object to
285             # not fall into meta-circular death
286             #
287             # we just alias the original method
288             # rather than re-produce it here
289             'get_attribute_map' => \&Class::MOP::Class::get_attribute_map
290         },
291         default  => sub { {} }
292     ))
293 );
294
295 Class::MOP::Class->meta->add_attribute(
296     Class::MOP::Attribute->new('methods' => (
297         reader   => {
298             # NOTE:
299             # we just alias the original method
300             # rather than re-produce it here
301             'get_method_map' => \&Class::MOP::Class::get_method_map
302         },
303         default => sub { {} }
304     ))
305 );
306
307 Class::MOP::Class->meta->add_attribute(
308     Class::MOP::Attribute->new('superclasses' => (
309         accessor => {
310             # NOTE:
311             # we just alias the original method
312             # rather than re-produce it here
313             'superclasses' => \&Class::MOP::Class::superclasses
314         },
315         init_arg => undef,
316         default  => sub { \undef }
317     ))
318 );
319
320 Class::MOP::Class->meta->add_attribute(
321     Class::MOP::Attribute->new('attribute_metaclass' => (
322         reader   => {
323             # NOTE:
324             # we just alias the original method
325             # rather than re-produce it here
326             'attribute_metaclass' => \&Class::MOP::Class::attribute_metaclass
327         },
328         default  => 'Class::MOP::Attribute',
329     ))
330 );
331
332 Class::MOP::Class->meta->add_attribute(
333     Class::MOP::Attribute->new('method_metaclass' => (
334         reader   => {
335             # NOTE:
336             # we just alias the original method
337             # rather than re-produce it here
338             'method_metaclass' => \&Class::MOP::Class::method_metaclass
339         },
340         default  => 'Class::MOP::Method',
341     ))
342 );
343
344 Class::MOP::Class->meta->add_attribute(
345     Class::MOP::Attribute->new('instance_metaclass' => (
346         reader   => {
347             # NOTE: we need to do this in order
348             # for the instance meta-object to
349             # not fall into meta-circular death
350             #
351             # we just alias the original method
352             # rather than re-produce it here
353             'instance_metaclass' => \&Class::MOP::Class::instance_metaclass
354         },
355         default  => 'Class::MOP::Instance',
356     ))
357 );
358
359 # NOTE:
360 # we don't actually need to tie the knot with
361 # Class::MOP::Class here, it is actually handled
362 # within Class::MOP::Class itself in the
363 # construct_class_instance method.
364
365 ## --------------------------------------------------------
366 ## Class::MOP::Attribute
367
368 Class::MOP::Attribute->meta->add_attribute(
369     Class::MOP::Attribute->new('name' => (
370         reader   => {
371             # NOTE: we need to do this in order
372             # for the instance meta-object to
373             # not fall into meta-circular death
374             #
375             # we just alias the original method
376             # rather than re-produce it here
377             'name' => \&Class::MOP::Attribute::name
378         }
379     ))
380 );
381
382 Class::MOP::Attribute->meta->add_attribute(
383     Class::MOP::Attribute->new('associated_class' => (
384         reader   => {
385             # NOTE: we need to do this in order
386             # for the instance meta-object to
387             # not fall into meta-circular death
388             #
389             # we just alias the original method
390             # rather than re-produce it here
391             'associated_class' => \&Class::MOP::Attribute::associated_class
392         }
393     ))
394 );
395
396 Class::MOP::Attribute->meta->add_attribute(
397     Class::MOP::Attribute->new('accessor' => (
398         reader    => { 'accessor'     => \&Class::MOP::Attribute::accessor     },
399         predicate => { 'has_accessor' => \&Class::MOP::Attribute::has_accessor },
400     ))
401 );
402
403 Class::MOP::Attribute->meta->add_attribute(
404     Class::MOP::Attribute->new('reader' => (
405         reader    => { 'reader'     => \&Class::MOP::Attribute::reader     },
406         predicate => { 'has_reader' => \&Class::MOP::Attribute::has_reader },
407     ))
408 );
409
410 Class::MOP::Attribute->meta->add_attribute(
411     Class::MOP::Attribute->new('initializer' => (
412         reader    => { 'initializer'     => \&Class::MOP::Attribute::initializer     },
413         predicate => { 'has_initializer' => \&Class::MOP::Attribute::has_initializer },
414     ))
415 );
416
417 Class::MOP::Attribute->meta->add_attribute(
418     Class::MOP::Attribute->new('writer' => (
419         reader    => { 'writer'     => \&Class::MOP::Attribute::writer     },
420         predicate => { 'has_writer' => \&Class::MOP::Attribute::has_writer },
421     ))
422 );
423
424 Class::MOP::Attribute->meta->add_attribute(
425     Class::MOP::Attribute->new('predicate' => (
426         reader    => { 'predicate'     => \&Class::MOP::Attribute::predicate     },
427         predicate => { 'has_predicate' => \&Class::MOP::Attribute::has_predicate },
428     ))
429 );
430
431 Class::MOP::Attribute->meta->add_attribute(
432     Class::MOP::Attribute->new('clearer' => (
433         reader    => { 'clearer'     => \&Class::MOP::Attribute::clearer     },
434         predicate => { 'has_clearer' => \&Class::MOP::Attribute::has_clearer },
435     ))
436 );
437
438 Class::MOP::Attribute->meta->add_attribute(
439     Class::MOP::Attribute->new('builder' => (
440         reader    => { 'builder'     => \&Class::MOP::Attribute::builder     },
441         predicate => { 'has_builder' => \&Class::MOP::Attribute::has_builder },
442     ))
443 );
444
445 Class::MOP::Attribute->meta->add_attribute(
446     Class::MOP::Attribute->new('init_arg' => (
447         reader    => { 'init_arg'     => \&Class::MOP::Attribute::init_arg     },
448         predicate => { 'has_init_arg' => \&Class::MOP::Attribute::has_init_arg },
449     ))
450 );
451
452 Class::MOP::Attribute->meta->add_attribute(
453     Class::MOP::Attribute->new('default' => (
454         # default has a custom 'reader' method ...
455         predicate => { 'has_default' => \&Class::MOP::Attribute::has_default },
456     ))
457 );
458
459 Class::MOP::Attribute->meta->add_attribute(
460     Class::MOP::Attribute->new('associated_methods' => (
461         reader   => { 'associated_methods' => \&Class::MOP::Attribute::associated_methods },
462         default  => sub { [] }
463     ))
464 );
465
466 Class::MOP::Attribute->meta->add_method('clone' => sub {
467     my $self  = shift;
468     $self->meta->clone_object($self, @_);
469 });
470
471 ## --------------------------------------------------------
472 ## Class::MOP::Method
473 Class::MOP::Method->meta->add_attribute(
474     Class::MOP::Attribute->new('body' => (
475         reader   => { 'body' => \&Class::MOP::Method::body },
476     ))
477 );
478
479 Class::MOP::Method->meta->add_attribute(
480     Class::MOP::Attribute->new('associated_metaclass' => (
481         reader   => { 'associated_metaclass' => \&Class::MOP::Method::associated_metaclass },
482     ))
483 );
484
485 Class::MOP::Method->meta->add_attribute(
486     Class::MOP::Attribute->new('package_name' => (
487         reader   => { 'package_name' => \&Class::MOP::Method::package_name },
488     ))
489 );
490
491 Class::MOP::Method->meta->add_attribute(
492     Class::MOP::Attribute->new('name' => (
493         reader   => { 'name' => \&Class::MOP::Method::name },
494     ))
495 );
496
497 Class::MOP::Method->meta->add_method('clone' => sub {
498     my $self  = shift;
499     $self->meta->clone_object($self, @_);
500 });
501
502 ## --------------------------------------------------------
503 ## Class::MOP::Method::Wrapped
504
505 # NOTE:
506 # the way this item is initialized, this
507 # really does not follow the standard
508 # practices of attributes, but we put
509 # it here for completeness
510 Class::MOP::Method::Wrapped->meta->add_attribute(
511     Class::MOP::Attribute->new('modifier_table')
512 );
513
514 ## --------------------------------------------------------
515 ## Class::MOP::Method::Generated
516
517 Class::MOP::Method::Generated->meta->add_attribute(
518     Class::MOP::Attribute->new('is_inline' => (
519         reader   => { 'is_inline' => \&Class::MOP::Method::Generated::is_inline },
520         default  => 0, 
521     ))
522 );
523
524 ## --------------------------------------------------------
525 ## Class::MOP::Method::Accessor
526
527 Class::MOP::Method::Accessor->meta->add_attribute(
528     Class::MOP::Attribute->new('attribute' => (
529         reader   => {
530             'associated_attribute' => \&Class::MOP::Method::Accessor::associated_attribute
531         },
532     ))
533 );
534
535 Class::MOP::Method::Accessor->meta->add_attribute(
536     Class::MOP::Attribute->new('accessor_type' => (
537         reader   => { 'accessor_type' => \&Class::MOP::Method::Accessor::accessor_type },
538     ))
539 );
540
541 ## --------------------------------------------------------
542 ## Class::MOP::Method::Constructor
543
544 Class::MOP::Method::Constructor->meta->add_attribute(
545     Class::MOP::Attribute->new('options' => (
546         reader   => {
547             'options' => \&Class::MOP::Method::Constructor::options
548         },
549         default  => sub { +{} }
550     ))
551 );
552
553 Class::MOP::Method::Constructor->meta->add_attribute(
554     Class::MOP::Attribute->new('associated_metaclass' => (
555         init_arg => "metaclass", # FIXME alias and rename
556         reader   => {
557             'associated_metaclass' => \&Class::MOP::Method::Constructor::associated_metaclass
558         },
559     ))
560 );
561
562 ## --------------------------------------------------------
563 ## Class::MOP::Instance
564
565 # NOTE:
566 # these don't yet do much of anything, but are just
567 # included for completeness
568
569 Class::MOP::Instance->meta->add_attribute(
570     Class::MOP::Attribute->new('associated_metaclass',
571         reader   => { associated_metaclass => \&Class::MOP::Instance::associated_metaclass },
572     ),
573 );
574
575 Class::MOP::Instance->meta->add_attribute(
576     Class::MOP::Attribute->new('_class_name',
577         init_arg => undef,
578         reader   => { _class_name => \&Class::MOP::Instance::_class_name },
579         #lazy     => 1, # not yet supported by Class::MOP but out our version does it anyway
580         #default  => sub { $_[0]->associated_metaclass->name },
581     ),
582 );
583
584 Class::MOP::Instance->meta->add_attribute(
585     Class::MOP::Attribute->new('attributes',
586         reader   => { attributes => \&Class::MOP::Instance::get_all_attributes },
587     ),
588 );
589
590 Class::MOP::Instance->meta->add_attribute(
591     Class::MOP::Attribute->new('slots',
592         reader   => { slots => \&Class::MOP::Instance::slots },
593     ),
594 );
595
596 Class::MOP::Instance->meta->add_attribute(
597     Class::MOP::Attribute->new('slot_hash',
598         reader   => { slot_hash => \&Class::MOP::Instance::slot_hash },
599     ),
600 );
601
602
603 # we need the meta instance of the meta instance to be created now, in order
604 # for the constructor to be able to use it
605 Class::MOP::Instance->meta->get_meta_instance;
606
607 # pretend the add_method never happenned. it hasn't yet affected anything
608 undef Class::MOP::Instance->meta->{_package_cache_flag};
609
610 ## --------------------------------------------------------
611 ## Now close all the Class::MOP::* classes
612
613 # NOTE:
614 # we don't need to inline the
615 # constructors or the accessors
616 # this only lengthens the compile
617 # time of the MOP, and gives us
618 # no actual benefits.
619
620 $_->meta->make_immutable(
621     inline_constructor  => 1,
622     replace_constructor => 1,
623     constructor_name    => "_new",
624     inline_accessors => 0,
625 ) for qw/
626     Class::MOP::Package
627     Class::MOP::Module
628     Class::MOP::Class
629
630     Class::MOP::Attribute
631     Class::MOP::Method
632     Class::MOP::Instance
633
634     Class::MOP::Object
635
636     Class::MOP::Method::Generated
637
638     Class::MOP::Method::Accessor
639     Class::MOP::Method::Constructor
640     Class::MOP::Method::Wrapped
641 /;
642
643 1;
644
645 __END__
646
647 =pod
648
649 =head1 NAME
650
651 Class::MOP - A Meta Object Protocol for Perl 5
652
653 =head1 DESCRIPTON
654
655 This module is a fully functioning meta object protocol for the
656 Perl 5 object system. It makes no attempt to change the behavior or
657 characteristics of the Perl 5 object system, only to create a
658 protocol for its manipulation and introspection.
659
660 That said, it does attempt to create the tools for building a rich
661 set of extensions to the Perl 5 object system. Every attempt has been
662 made for these tools to keep to the spirit of the Perl 5 object
663 system that we all know and love.
664
665 This documentation is admittedly sparse on details, as time permits
666 I will try to improve them. For now, I suggest looking at the items
667 listed in the L<SEE ALSO> section for more information. In particular
668 the book "The Art of the Meta Object Protocol" was very influential
669 in the development of this system.
670
671 =head2 What is a Meta Object Protocol?
672
673 A meta object protocol is an API to an object system.
674
675 To be more specific, it is a set of abstractions of the components of
676 an object system (typically things like; classes, object, methods,
677 object attributes, etc.). These abstractions can then be used to both
678 inspect and manipulate the object system which they describe.
679
680 It can be said that there are two MOPs for any object system; the
681 implicit MOP, and the explicit MOP. The implicit MOP handles things
682 like method dispatch or inheritance, which happen automatically as
683 part of how the object system works. The explicit MOP typically
684 handles the introspection/reflection features of the object system.
685 All object systems have implicit MOPs, without one, they would not
686 work. Explict MOPs however as less common, and depending on the
687 language can vary from restrictive (Reflection in Java or C#) to
688 wide open (CLOS is a perfect example).
689
690 =head2 Yet Another Class Builder!! Why?
691
692 This is B<not> a class builder so much as it is a I<class builder
693 B<builder>>. My intent is that an end user does not use this module
694 directly, but instead this module is used by module authors to
695 build extensions and features onto the Perl 5 object system.
696
697 =head2 Who is this module for?
698
699 This module is specifically for anyone who has ever created or
700 wanted to create a module for the Class:: namespace. The tools which
701 this module will provide will hopefully make it easier to do more
702 complex things with Perl 5 classes by removing such barriers as
703 the need to hack the symbol tables, or understand the fine details
704 of method dispatch.
705
706 =head2 What changes do I have to make to use this module?
707
708 This module was designed to be as unintrusive as possible. Many of
709 its features are accessible without B<any> change to your existsing
710 code at all. It is meant to be a compliment to your existing code and
711 not an intrusion on your code base. Unlike many other B<Class::>
712 modules, this module B<does not> require you subclass it, or even that
713 you C<use> it in within your module's package.
714
715 The only features which requires additions to your code are the
716 attribute handling and instance construction features, and these are
717 both completely optional features. The only reason for this is because
718 Perl 5's object system does not actually have these features built
719 in. More information about this feature can be found below.
720
721 =head2 A Note about Performance?
722
723 It is a common misconception that explict MOPs are performance drains.
724 But this is not a universal truth at all, it is an side-effect of
725 specific implementations. For instance, using Java reflection is much
726 slower because the JVM cannot take advantage of any compiler
727 optimizations, and the JVM has to deal with much more runtime type
728 information as well. Reflection in C# is marginally better as it was
729 designed into the language and runtime (the CLR). In contrast, CLOS
730 (the Common Lisp Object System) was built to support an explicit MOP,
731 and so performance is tuned for it.
732
733 This library in particular does it's absolute best to avoid putting
734 B<any> drain at all upon your code's performance. In fact, by itself
735 it does nothing to affect your existing code. So you only pay for
736 what you actually use.
737
738 =head2 About Metaclass compatibility
739
740 This module makes sure that all metaclasses created are both upwards
741 and downwards compatible. The topic of metaclass compatibility is
742 highly esoteric and is something only encountered when doing deep and
743 involved metaclass hacking. There are two basic kinds of metaclass
744 incompatibility; upwards and downwards.
745
746 Upwards metaclass compatibility means that the metaclass of a
747 given class is either the same as (or a subclass of) all of the
748 class's ancestors.
749
750 Downward metaclass compatibility means that the metaclasses of a
751 given class's anscestors are all either the same as (or a subclass
752 of) that metaclass.
753
754 Here is a diagram showing a set of two classes (C<A> and C<B>) and
755 two metaclasses (C<Meta::A> and C<Meta::B>) which have correct
756 metaclass compatibility both upwards and downwards.
757
758     +---------+     +---------+
759     | Meta::A |<----| Meta::B |      <....... (instance of  )
760     +---------+     +---------+      <------- (inherits from)
761          ^               ^
762          :               :
763     +---------+     +---------+
764     |    A    |<----|    B    |
765     +---------+     +---------+
766
767 As I said this is a highly esoteric topic and one you will only run
768 into if you do a lot of subclassing of B<Class::MOP::Class>. If you
769 are interested in why this is an issue see the paper
770 I<Uniform and safe metaclass composition> linked to in the
771 L<SEE ALSO> section of this document.
772
773 =head2 Using custom metaclasses
774
775 Always use the metaclass pragma when using a custom metaclass, this
776 will ensure the proper initialization order and not accidentely
777 create an incorrect type of metaclass for you. This is a very rare
778 problem, and one which can only occur if you are doing deep metaclass
779 programming. So in other words, don't worry about it.
780
781 =head1 PROTOCOLS
782
783 The protocol is divided into 4 main sub-protocols:
784
785 =over 4
786
787 =item The Class protocol
788
789 This provides a means of manipulating and introspecting a Perl 5
790 class. It handles all of symbol table hacking for you, and provides
791 a rich set of methods that go beyond simple package introspection.
792
793 See L<Class::MOP::Class> for more details.
794
795 =item The Attribute protocol
796
797 This provides a consistent represenation for an attribute of a
798 Perl 5 class. Since there are so many ways to create and handle
799 attributes in Perl 5 OO, this attempts to provide as much of a
800 unified approach as possible, while giving the freedom and
801 flexibility to subclass for specialization.
802
803 See L<Class::MOP::Attribute> for more details.
804
805 =item The Method protocol
806
807 This provides a means of manipulating and introspecting methods in
808 the Perl 5 object system. As with attributes, there are many ways to
809 approach this topic, so we try to keep it pretty basic, while still
810 making it possible to extend the system in many ways.
811
812 See L<Class::MOP::Method> for more details.
813
814 =item The Instance protocol
815
816 This provides a layer of abstraction for creating object instances. 
817 Since the other layers use this protocol, it is relatively easy to 
818 change the type of your instances from the default HASH ref to other
819 types of references. Several examples are provided in the F<examples/> 
820 directory included in this distribution.
821
822 See L<Class::MOP::Instance> for more details.
823
824 =back
825
826 =head1 FUNCTIONS
827
828 =head2 Constants
829
830 =over 4
831
832 =item I<IS_RUNNING_ON_5_10>
833
834 We set this constant depending on what version perl we are on, this 
835 allows us to take advantage of new 5.10 features and stay backwards 
836 compat.
837
838 =item I<HAVE_ISAREV>
839
840 Whether or not C<mro> provides C<get_isarev>, a much faster way to get all the
841 subclasses of a certain class.
842
843 =back
844
845 =head2 Utility functions
846
847 Note that these are all called as B<functions, not methods>. They are
848 "public" in the sense that they're documented for use by other
849 Class::MOP and Moose modules, but they're not really intended for use
850 in end-user code.
851
852 =over 4
853
854 =item B<load_class ($class_name)>
855
856 This will load a given C<$class_name> and if it does not have an
857 already initialized metaclass, then it will intialize one for it.
858 This function can be used in place of tricks like 
859 C<eval "use $module"> or using C<require>.
860
861 =item B<is_class_loaded ($class_name)>
862
863 This will return a boolean depending on if the C<$class_name> has
864 been loaded.
865
866 NOTE: This does a basic check of the symbol table to try and
867 determine as best it can if the C<$class_name> is loaded, it
868 is probably correct about 99% of the time.
869
870 =item B<check_package_cache_flag ($pkg)>
871
872 This will return an integer that is managed by C<Class::MOP::Class>
873 to determine if a module's symbol table has been altered. 
874
875 In Perl 5.10 or greater, this flag is package specific. However in 
876 versions prior to 5.10, this will use the C<PL_sub_generation> variable
877 which is not package specific. 
878
879 =item B<get_code_info ($code)>
880
881 This function returns two values, the name of the package the C<$code> 
882 is from and the name of the C<$code> itself. This is used by several 
883 elements of the MOP to detemine where a given C<$code> reference is from.
884
885 =item B<subname ($name, $code)>
886
887 B<NOTE: DO NOT USE THIS FUNCTION, IT IS FOR INTERNAL USE ONLY!>
888
889 If possible, we will load the L<Sub::Name> module and this will function 
890 as C<Sub::Name::subname> does, otherwise it will just return the C<$code>
891 argument.
892
893 =item B<in_global_destruction>
894
895 If L<Devel::GlobalDestruction> is available, this returns true under global
896 destruction.
897
898 Otherwise it's a constant returning false.
899
900 =back
901
902 =head2 Metaclass cache functions
903
904 Class::MOP holds a cache of metaclasses, the following are functions
905 (B<not methods>) which can be used to access that cache. It is not
906 recommended that you mess with this, bad things could happen. But if
907 you are brave and willing to risk it, go for it.
908
909 =over 4
910
911 =item B<get_all_metaclasses>
912
913 This will return an hash of all the metaclass instances that have
914 been cached by B<Class::MOP::Class> keyed by the package name.
915
916 =item B<get_all_metaclass_instances>
917
918 This will return an array of all the metaclass instances that have
919 been cached by B<Class::MOP::Class>.
920
921 =item B<get_all_metaclass_names>
922
923 This will return an array of all the metaclass names that have
924 been cached by B<Class::MOP::Class>.
925
926 =item B<get_metaclass_by_name ($name)>
927
928 This will return a cached B<Class::MOP::Class> instance of nothing
929 if no metaclass exist by that C<$name>.
930
931 =item B<store_metaclass_by_name ($name, $meta)>
932
933 This will store a metaclass in the cache at the supplied C<$key>.
934
935 =item B<weaken_metaclass ($name)>
936
937 In rare cases it is desireable to store a weakened reference in 
938 the metaclass cache. This function will weaken the reference to 
939 the metaclass stored in C<$name>.
940
941 =item B<does_metaclass_exist ($name)>
942
943 This will return true of there exists a metaclass stored in the 
944 C<$name> key and return false otherwise.
945
946 =item B<remove_metaclass_by_name ($name)>
947
948 This will remove a the metaclass stored in the C<$name> key.
949
950 =back
951
952 =head1 SEE ALSO
953
954 =head2 Books
955
956 There are very few books out on Meta Object Protocols and Metaclasses
957 because it is such an esoteric topic. The following books are really
958 the only ones I have found. If you know of any more, B<I<please>>
959 email me and let me know, I would love to hear about them.
960
961 =over 4
962
963 =item "The Art of the Meta Object Protocol"
964
965 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
966
967 =item "Putting MetaClasses to Work"
968
969 =item "Smalltalk: The Language"
970
971 =back
972
973 =head2 Papers
974
975 =over 4
976
977 =item Uniform and safe metaclass composition
978
979 An excellent paper by the people who brought us the original Traits paper.
980 This paper is on how Traits can be used to do safe metaclass composition,
981 and offers an excellent introduction section which delves into the topic of
982 metaclass compatibility.
983
984 L<http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMetaclassTrait.pdf>
985
986 =item Safe Metaclass Programming
987
988 This paper seems to precede the above paper, and propose a mix-in based
989 approach as opposed to the Traits based approach. Both papers have similar
990 information on the metaclass compatibility problem space.
991
992 L<http://citeseer.ist.psu.edu/37617.html>
993
994 =back
995
996 =head2 Prior Art
997
998 =over 4
999
1000 =item The Perl 6 MetaModel work in the Pugs project
1001
1002 =over 4
1003
1004 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
1005
1006 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
1007
1008 =back
1009
1010 =back
1011
1012 =head2 Articles
1013
1014 =over 4
1015
1016 =item CPAN Module Review of Class::MOP
1017
1018 L<http://www.oreillynet.com/onlamp/blog/2006/06/cpan_module_review_classmop.html>
1019
1020 =back
1021
1022 =head1 SIMILAR MODULES
1023
1024 As I have said above, this module is a class-builder-builder, so it is
1025 not the same thing as modules like L<Class::Accessor> and
1026 L<Class::MethodMaker>. That being said there are very few modules on CPAN
1027 with similar goals to this module. The one I have found which is most
1028 like this module is L<Class::Meta>, although it's philosophy and the MOP it
1029 creates are very different from this modules.
1030
1031 =head1 BUGS
1032
1033 All complex software has bugs lurking in it, and this module is no
1034 exception. If you find a bug please either email me, or add the bug
1035 to cpan-RT.
1036
1037 =head1 ACKNOWLEDGEMENTS
1038
1039 =over 4
1040
1041 =item Rob Kinyon
1042
1043 Thanks to Rob for actually getting the development of this module kick-started.
1044
1045 =back
1046
1047 =head1 AUTHORS
1048
1049 Stevan Little E<lt>stevan@iinteractive.comE<gt>
1050
1051 B<with contributions from:>
1052
1053 Brandon (blblack) Black
1054
1055 Guillermo (groditi) Roditi
1056
1057 Matt (mst) Trout
1058
1059 Rob (robkinyon) Kinyon
1060
1061 Yuval (nothingmuch) Kogman
1062
1063 Scott (konobi) McWhirter
1064
1065 =head1 COPYRIGHT AND LICENSE
1066
1067 Copyright 2006-2008 by Infinity Interactive, Inc.
1068
1069 L<http://www.iinteractive.com>
1070
1071 This library is free software; you can redistribute it and/or modify
1072 it under the same terms as Perl itself.
1073
1074 =cut