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