1Class::MOP(3) User Contributed Perl Documentation Class::MOP(3)
2
3
4
6 Class::MOP - A Meta Object Protocol for Perl 5
7
9 This module is an attempt to create a meta object protocol for the Perl
10 5 object system. It makes no attempt to change the behavior or charac‐
11 teristics of the Perl 5 object system, only to create a protocol for
12 its manipulation and introspection.
13
14 That said, it does attempt to create the tools for building a rich set
15 of extensions to the Perl 5 object system. Every attempt has been made
16 for these tools to keep to the spirit of the Perl 5 object system that
17 we all know and love.
18
19 This documentation is admittedly sparse on details, as time permits I
20 will try to improve them. For now, I suggest looking at the items
21 listed in the "SEE ALSO" section for more information. In particular
22 the book "The Art of the Meta Object Protocol" was very influential in
23 the development of this system.
24
25 What is a Meta Object Protocol?
26
27 A meta object protocol is an API to an object system.
28
29 To be more specific, it is a set of abstractions of the components of
30 an object system (typically things like; classes, object, methods,
31 object attributes, etc.). These abstractions can then be used to both
32 inspect and manipulate the object system which they describe.
33
34 It can be said that there are two MOPs for any object system; the
35 implicit MOP, and the explicit MOP. The implicit MOP handles things
36 like method dispatch or inheritance, which happen automatically as part
37 of how the object system works. The explicit MOP typically handles the
38 introspection/reflection features of the object system. All object
39 systems have implicit MOPs, without one, they would not work. Explict
40 MOPs however as less common, and depending on the language can vary
41 from restrictive (Reflection in Java or C#) to wide open (CLOS is a
42 perfect example).
43
44 Yet Another Class Builder!! Why?
45
46 This is not a class builder so much as it is a class builder builder.
47 My intent is that an end user does not use this module directly, but
48 instead this module is used by module authors to build extensions and
49 features onto the Perl 5 object system.
50
51 Who is this module for?
52
53 This module is specifically for anyone who has ever created or wanted
54 to create a module for the Class:: namespace. The tools which this mod‐
55 ule will provide will hopefully make it easier to do more complex
56 things with Perl 5 classes by removing such barriers as the need to
57 hack the symbol tables, or understand the fine details of method dis‐
58 patch.
59
60 What changes do I have to make to use this module?
61
62 This module was designed to be as unintrusive as possible. Many of its
63 features are accessible without any change to your existsing code at
64 all. It is meant to be a compliment to your existing code and not an
65 intrusion on your code base. Unlike many other Class:: modules, this
66 module does not require you subclass it, or even that you "use" it in
67 within your module's package.
68
69 The only features which requires additions to your code are the
70 attribute handling and instance construction features, and these are
71 both completely optional features. The only reason for this is because
72 Perl 5's object system does not actually have these features built in.
73 More information about this feature can be found below.
74
75 A Note about Performance?
76
77 It is a common misconception that explict MOPs are performance drains.
78 But this is not a universal truth at all, it is an side-effect of spe‐
79 cific implementations. For instance, using Java reflection is much
80 slower because the JVM cannot take advantage of any compiler optimiza‐
81 tions, and the JVM has to deal with much more runtime type information
82 as well. Reflection in C# is marginally better as it was designed into
83 the language and runtime (the CLR). In contrast, CLOS (the Common Lisp
84 Object System) was built to support an explicit MOP, and so performance
85 is tuned for it.
86
87 This library in particular does it's absolute best to avoid putting any
88 drain at all upon your code's performance. In fact, by itself it does
89 nothing to affect your existing code. So you only pay for what you
90 actually use.
91
92 About Metaclass compatibility
93
94 This module makes sure that all metaclasses created are both upwards
95 and downwards compatible. The topic of metaclass compatibility is
96 highly esoteric and is something only encountered when doing deep and
97 involved metaclass hacking. There are two basic kinds of metaclass
98 incompatibility; upwards and downwards.
99
100 Upwards metaclass compatibility means that the metaclass of a given
101 class is either the same as (or a subclass of) all of the class's
102 ancestors.
103
104 Downward metaclass compatibility means that the metaclasses of a given
105 class's anscestors are all either the same as (or a subclass of) that
106 metaclass.
107
108 Here is a diagram showing a set of two classes ("A" and "B") and two
109 metaclasses ("Meta::A" and "Meta::B") which have correct metaclass com‐
110 patibility both upwards and downwards.
111
112 +---------+ +---------+
113 ⎪ Meta::A ⎪<----⎪ Meta::B ⎪ <....... (instance of )
114 +---------+ +---------+ <------- (inherits from)
115 ^ ^
116 : :
117 +---------+ +---------+
118 ⎪ A ⎪<----⎪ B ⎪
119 +---------+ +---------+
120
121 As I said this is a highly esoteric topic and one you will only run
122 into if you do a lot of subclassing of Class::MOP::Class. If you are
123 interested in why this is an issue see the paper Uniform and safe meta‐
124 class composition linked to in the "SEE ALSO" section of this document.
125
126 Using custom metaclasses
127
128 Always use the metaclass pragma when using a custom metaclass, this
129 will ensure the proper initialization order and not accidentely create
130 an incorrect type of metaclass for you. This is a very rare problem,
131 and one which can only occur if you are doing deep metaclass program‐
132 ming. So in other words, don't worry about it.
133
135 The protocol is divided into 3 main sub-protocols:
136
137 The Class protocol
138 This provides a means of manipulating and introspecting a Perl 5
139 class. It handles all of symbol table hacking for you, and provides
140 a rich set of methods that go beyond simple package introspection.
141
142 See Class::MOP::Class for more details.
143
144 The Attribute protocol
145 This provides a consistent represenation for an attribute of a Perl
146 5 class. Since there are so many ways to create and handle att‐
147 tributes in Perl 5 OO, this attempts to provide as much of a uni‐
148 fied approach as possible, while giving the freedom and flexibility
149 to subclass for specialization.
150
151 See Class::MOP::Attribute for more details.
152
153 The Method protocol
154 This provides a means of manipulating and introspecting methods in
155 the Perl 5 object system. As with attributes, there are many ways
156 to approach this topic, so we try to keep it pretty basic, while
157 still making it possible to extend the system in many ways.
158
159 See Class::MOP::Method for more details.
160
162 Utility functions
163
164 load_class ($class_name)
165 This will load a given $class_name and if it does not have an
166 already initialized metaclass, then it will intialize one for it.
167
168 is_class_loaded ($class_name)
169 This will return a boolean depending on if the $class_name has been
170 loaded.
171
172 NOTE: This does a basic check of the symbol table to try and deter‐
173 mine as best it can if the $class_name is loaded, it is probably
174 correct about 99% of the time.
175
176 Metaclass cache functions
177
178 Class::MOP holds a cache of metaclasses, the following are functions
179 (not methods) which can be used to access that cache. It is not recom‐
180 mended that you mess with this, bad things could happen. But if you are
181 brave and willing to risk it, go for it.
182
183 get_all_metaclasses
184 This will return an hash of all the metaclass instances that have
185 been cached by Class::MOP::Class keyed by the package name.
186
187 get_all_metaclass_instances
188 This will return an array of all the metaclass instances that have
189 been cached by Class::MOP::Class.
190
191 get_all_metaclass_names
192 This will return an array of all the metaclass names that have been
193 cached by Class::MOP::Class.
194
195 get_metaclass_by_name ($name)
196 store_metaclass_by_name ($name, $meta)
197 weaken_metaclass ($name)
198 does_metaclass_exist ($name)
199 remove_metaclass_by_name ($name)
200
202 Books
203
204 There are very few books out on Meta Object Protocols and Metaclasses
205 because it is such an esoteric topic. The following books are really
206 the only ones I have found. If you know of any more, please email me
207 and let me know, I would love to hear about them.
208
209 "The Art of the Meta Object Protocol"
210 "Advances in Object-Oriented Metalevel Architecture and Reflection"
211 "Putting MetaClasses to Work"
212 "Smalltalk: The Language"
213
214 Papers
215
216 Uniform and safe metaclass composition
217 An excellent paper by the people who brought us the original Traits
218 paper. This paper is on how Traits can be used to do safe meta‐
219 class composition, and offers an excellent introduction section
220 which delves into the topic of metaclass compatibility.
221
222 <http://www.iam.unibe.ch/~scg/Archive/Papers/Duca05ySafeMeta‐
223 classTrait.pdf>
224
225 Safe Metaclass Programming
226 This paper seems to precede the above paper, and propose a mix-in
227 based approach as opposed to the Traits based approach. Both papers
228 have similar information on the metaclass compatibility problem
229 space.
230
231 <http://citeseer.ist.psu.edu/37617.html>
232
233 Prior Art
234
235 The Perl 6 MetaModel work in the Pugs project
236 <http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel>
237 <http://svn.openfoundry.org/pugs/perl5/Perl6-ObjectSpace>
238
239 Articles
240
241 CPAN Module Review of Class::MOP
242 <http://www.oreillynet.com/onlamp/blog/2006/06/cpan_mod‐
243 ule_review_classmop.html>
244
246 As I have said above, this module is a class-builder-builder, so it is
247 not the same thing as modules like Class::Accessor and Class::Method‐
248 Maker. That being said there are very few modules on CPAN with similar
249 goals to this module. The one I have found which is most like this mod‐
250 ule is Class::Meta, although it's philosophy and the MOP it creates are
251 very different from this modules.
252
254 All complex software has bugs lurking in it, and this module is no
255 exception. If you find a bug please either email me, or add the bug to
256 cpan-RT.
257
259 Rob Kinyon
260 Thanks to Rob for actually getting the development of this module
261 kick-started.
262
264 Stevan Little <stevan@iinteractive.com>
265
266 with contributions from:
267
268 Brandon (blblack) Black
269
270 Guillermo (groditi) Roditi
271
272 Matt (mst) Trout
273
274 Rob (robkinyon) Kinyon
275
276 Yuval (nothingmuch) Kogman
277
279 Copyright 2006, 2007 by Infinity Interactive, Inc.
280
281 <http://www.iinteractive.com>
282
283 This library is free software; you can redistribute it and/or modify it
284 under the same terms as Perl itself.
285
286
287
288perl v5.8.8 2007-11-07 Class::MOP(3)