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