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