Class::MOP - getting there
[gitmo/Class-MOP.git] / lib / Class / MOP.pm
1
2 package Class::MOP;
3
4 use strict;
5 use warnings;
6
7 use Scalar::Util 'blessed';
8
9 use Class::MOP::Class;
10 use Class::MOP::Attribute;
11 use Class::MOP::Method;
12
13 our $VERSION = '0.01';
14
15 sub import {
16     shift;
17     return unless @_;
18     if ($_[0] eq ':universal') {
19         *UNIVERSAL::meta = sub { 
20             Class::MOP::Class->initialize(blessed($_[0]) || $_[0]) 
21         };
22     }
23 }
24
25 1;
26
27 __END__
28
29 =pod
30
31 =head1 NAME 
32
33 Class::MOP - A Meta Object Protocol for Perl 5
34
35 =head1 SYNOPSIS
36
37   use Class::MOP ':universal';
38   
39   package Foo;
40   
41   Foo->meta->add_method('foo' => sub { ... });
42
43 =head1 DESCRIPTON
44
45 This module is an attempt to create a meta object protocol for the 
46 Perl 5 object system. It makes no attempt to change the behavior or 
47 characteristics of the Perl 5 object system, only to create a 
48 protocol for its manipulation and introspection.
49
50 That said, it does attempt to create the tools for building a rich 
51 set of extensions to the Perl 5 object system. Every attempt has been 
52 made for these tools to keep to the spirit of the Perl 5 object 
53 system that we all know and love.
54
55 =head2 What is a Meta Object Protocol?
56
57 A meta object protocol is an API to an object system. 
58
59 To be more specific, it is a set of abstractions of the components of 
60 an object system (typically things like; classes, object, methods, 
61 object attributes, etc.). These abstractions can then be used to both 
62 inspect and manipulate the object system which they describe.
63
64 It can be said that there are two MOPs for any object system; the 
65 implicit MOP, and the explicit MOP. The implicit MOP handles things 
66 like method dispatch or inheritance, which happen automatically as 
67 part of how the object system works. The explicit MOP typically 
68 handles the introspection/reflection features of the object system. 
69 All object systems have implicit MOPs, without one, they would not 
70 work. Explict MOPs however as less common, and depending on the 
71 language can vary from restrictive (Reflection in Java or C#) to 
72 wide open (CLOS is a perfect example). 
73
74 =head2 Yet Another Class Builder!! Why?
75
76 This is B<not> a class builder so much as it is a I<class builder 
77 B<builder>>. My intent is that an end user does not use this module 
78 directly, but instead this module is used by module authors to 
79 build extensions and features onto the Perl 5 object system. 
80
81 =head2 Who is this module for?
82
83 This module is specifically for anyone who has ever created or 
84 wanted to create a module for the Class:: namespace. The tools which 
85 this module will provide will hopefully make it easier to do more 
86 complex things with Perl 5 classes by removing such barriers as 
87 the need to hack the symbol tables, or understand the fine details 
88 of method dispatch. 
89
90 =head2 What changes do I have to make to use this module?
91
92 This module was designed to be as unintrusive as possible. Many of 
93 it's features are accessible without B<any> change to your existsing 
94 code at all. It is meant to be a compliment to your existing code and 
95 not an intrusion on your code base. Unlike many other B<Class::> 
96 modules, this module does require you subclass it, or even that you 
97 C<use> it in within your module's package. 
98
99 The only features which requires additions to your code are the 
100 attribute handling and instance construction features, and these are
101 both optional features as well. The only reason for this is because 
102 Perl 5's object system does not actually have these features built 
103 in. More information about this feature can be found below.
104
105 =head2 A Note about Performance?
106
107 It is a common misconception that explict MOPs are performance drains. 
108 But this is not a universal truth at all, it is an side-effect of 
109 specific implementations. For instance, using Java reflection is much 
110 slower because the JVM cannot take advantage of any compiler 
111 optimizations, and the JVM has to deal with much more runtime type 
112 information as well. Reflection in C# is marginally better as it was 
113 designed into the language and runtime (the CLR). In contrast, CLOS 
114 (the Common Lisp Object System) was built to support an explicit MOP, 
115 and so performance is tuned for it. 
116
117 This library in particular does it's absolute best to avoid putting 
118 B<any> drain at all upon your code's performance. In fact, by itself 
119 it does nothing to affect your existing code. So you only pay for 
120 what you actually use.
121
122 =head1 PROTOCOLS
123
124 The protocol is divided into 3 main sub-protocols:
125
126 =over 4
127
128 =item The Class protocol
129
130 This provides a means of manipulating and introspecting a Perl 5 
131 class. It handles all of symbol table hacking for you, and provides 
132 a rich set of methods that go beyond simple package introspection.
133
134 See L<Class::MOP::Class> for more details.
135
136 =item The Attribute protocol
137
138 This provides a consistent represenation for an attribute of a 
139 Perl 5 class. Since there are so many ways to create and handle 
140 atttributes in Perl 5 OO, this attempts to provide as much of a 
141 unified approach as possible, while giving the freedom and 
142 flexibility to subclass for specialization.
143
144 See L<Class::MOP::Attribute> for more details.
145
146 =item The Method protocol
147
148 This provides a means of manipulating and introspecting methods in 
149 the Perl 5 object system. As with attributes, there are many ways to 
150 approach this topic, so we try to keep it pretty basic, while still 
151 making it possible to extend the system in many ways.
152
153 See L<Class::MOP::Method> for more details.
154
155 =back
156
157 =head1 SEE ALSO
158
159 =head2 Books
160
161 =over 4
162
163 =item "The Art of the Meta Object Protocol"
164
165 =item "Advances in Object-Oriented Metalevel Architecture and Reflection"
166
167 =back
168
169 =head2 Prior Art
170
171 =over 4
172
173 =item The Perl 6 MetaModel work
174
175 =over 4
176
177 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
178
179 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
180
181 =back
182
183 =back
184
185 =head1 AUTHOR
186
187 Stevan Little E<gt>stevan@iinteractive.comE<lt>
188
189 Rob Kinyon E<gt>rob@iinteractive.comE<lt>
190
191 =head1 COPYRIGHT AND LICENSE
192
193 Copyright 2006 by Infinity Interactive, Inc.
194
195 L<http://www.iinteractive.com>
196
197 This library is free software; you can redistribute it and/or modify
198 it under the same terms as Perl itself. 
199
200 =cut