1MakeMethods::Composite:U:sIenrheCrointtarbilbeu(t3e)d PeMralkeDMoectuhmoednst:a:tCioomnposite::Inheritable(3)
2
3
4
6 Class::MakeMethods::Composite::Inheritable - Overridable data
7
9 package MyClass;
10
11 use Class::MakeMethods( 'Composite::Inheritable:scalar' => 'foo' );
12 # We now have an accessor method for an "inheritable" scalar value
13
14 MyClass->foo( 'Foozle' ); # Set a class-wide value
15 print MyClass->foo(); # Retrieve class-wide value
16
17 my $obj = MyClass->new(...);
18 print $obj->foo(); # All instances "inherit" that value...
19
20 $obj->foo( 'Foible' ); # until you set a value for an instance.
21 print $obj->foo(); # This now finds object-specific value.
22 ...
23
24 package MySubClass;
25 @ISA = 'MyClass';
26
27 print MySubClass->foo(); # Intially same as superclass,
28 MySubClass->foo('Foobar'); # but overridable per subclass,
29 print $subclass_obj->foo(); # and shared by its instances
30 $subclass_obj->foo('Fosil');# until you override them...
31 ...
32
33 # Similar behaviour for hashes and arrays is currently incomplete
34 package MyClass;
35 use Class::MakeMethods::Composite::Inheritable (
36 array => 'my_list',
37 hash => 'my_index',
38 );
39
40 MyClass->my_list(0 => 'Foozle', 1 => 'Bang!');
41 print MyClass->my_list(1);
42
43 MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
44 print MyClass->my_index('foo');
45
47 The MakeMethods subclass provides accessor methods that search an
48 inheritance tree to find a value. This allows you to set a shared or
49 default value for a given class, optionally override it in a subclass,
50 and then optionally override it on a per-instance basis.
51
52 Note that all MakeMethods methods are inheritable, in the sense that
53 they work as expected for subclasses. These methods are different in
54 that the data accessed by each method can be inherited or overridden in
55 each subclass or instance. See Class::MakeMethods::Utility::Inheritable
56 for more about this type of "inheritable" or overridable" data.
57
58 Class::MakeMethods Calling Interface
59
60 When you "use" this package, the method declarations you provide as
61 arguments cause subroutines to be generated and installed in your mod‐
62 ule.
63
64 See "Calling Conventions" in Class::MakeMethods::Standard for more
65 information.
66
67 Class::MakeMethods::Standard Declaration Syntax
68
69 To declare methods, pass in pairs of a method-type name followed by one
70 or more method names.
71
72 See the "METHOD GENERATOR TYPES" section below for a list of the sup‐
73 ported values of generator_type.
74
75 See "Declaration Syntax" in Class::MakeMethods::Standard and "Parameter
76 Syntax" in Class::MakeMethods::Standard for more information.
77
79 scalar - Overrideable Accessor
80
81 For each method name passed, uses a closure to generate a subroutine
82 with the following characteristics:
83
84 · May be called as a class or instance method, on the declaring class
85 or any subclass.
86
87 · If called without any arguments returns the current value for the
88 callee. If the callee has not had a value defined for this method,
89 searches up from instance to class, and from class to superclass,
90 until a callee with a value is located.
91
92 · If called with an argument, stores that as the value associated
93 with the callee, whether instance or class, and returns it,
94
95 · If called with multiple arguments, stores a reference to a new
96 array with those arguments as contents, and returns that array ref‐
97 erence.
98
99 Sample declaration and usage:
100
101 package MyClass;
102 use Class::MakeMethods::Composite::Inheritable (
103 scalar => 'foo',
104 );
105 ...
106
107 # Store value
108 MyClass->foo('Foozle');
109
110 # Retrieve value
111 print MyClass->foo;
112
113 array - Overrideable Ref Accessor
114
115 For each method name passed, uses a closure to generate a subroutine
116 with the following characteristics:
117
118 · May be called as a class method, or on any instance or subclass,
119 Must be called on a hash-based instance.
120
121 · The class value will be a reference to an array (or undef).
122
123 · If called without any arguments, returns the current array-ref
124 value (or undef).
125
126 · If called with a single non-ref argument, uses that argument as an
127 index to retrieve from the referenced array, and returns that value
128 (or undef).
129
130 · If called with a single array ref argument, uses that list to
131 return a slice of the referenced array.
132
133 · If called with a list of argument pairs, each with a non-ref index
134 and an associated value, stores the value at the given index in the
135 referenced array. If the class value was previously undefined, a
136 new array is autovivified. The current value in each position will
137 be overwritten, and later arguments with the same index will over‐
138 ride earlier ones. Returns the current array-ref value.
139
140 · If called with a list of argument pairs, each with the first item
141 being a reference to an array of up to two numbers, loops over each
142 pair and uses those numbers to splice the value array.
143
144 The first controlling number is the position at which the splice
145 will begin. Zero will start before the first item in the list. Neg‐
146 ative numbers count backwards from the end of the array.
147
148 The second number is the number of items to be removed from the
149 list. If it is omitted, or undefined, or zero, no items are
150 removed. If it is a positive integer, that many items will be
151 returned.
152
153 If both numbers are omitted, or are both undefined, they default to
154 containing the entire value array.
155
156 If the second argument is undef, no values will be inserted; if it
157 is a non-reference value, that one value will be inserted; if it is
158 an array-ref, its values will be copied.
159
160 The method returns the items that removed from the array, if any.
161
162 Sample declaration and usage:
163
164 package MyClass;
165 use Class::MakeMethods::Composite::Inheritable (
166 array => 'bar',
167 );
168 ...
169
170 # Clear and set contents of list
171 print MyClass->bar([ 'Spume', 'Frost' ] );
172
173 # Set values by position
174 MyClass->bar(0 => 'Foozle', 1 => 'Bang!');
175
176 # Positions may be overwritten, and in any order
177 MyClass->bar(2 => 'And Mash', 1 => 'Blah!');
178
179 # Retrieve value by position
180 print MyClass->bar(1);
181
182 # Direct access to referenced array
183 print scalar @{ MyClass->bar() };
184
185 There are also calling conventions for slice and splice operations:
186
187 # Retrieve slice of values by position
188 print join(', ', MyClass->bar( undef, [0, 2] ) );
189
190 # Insert an item at position in the array
191 MyClass->bar([3], 'Potatoes' );
192
193 # Remove 1 item from position 3 in the array
194 MyClass->bar([3, 1], undef );
195
196 # Set a new value at position 2, and return the old value
197 print MyClass->bar([2, 1], 'Froth' );
198
199 NOTE: THIS METHOD GENERATOR HAS NOT BEEN WRITTEN YET.
200
201 hash - Overrideable Ref Accessor
202
203 For each method name passed, uses a closure to generate a subroutine
204 with the following characteristics:
205
206 · May be called as a class method, or on any instance or subclass,
207 Must be called on a hash-based instance.
208
209 · The class value will be a reference to a hash (or undef).
210
211 · If called without any arguments returns the contents of the hash in
212 list context, or a hash reference in scalar context for the callee.
213 If the callee has not had a value defined for this method, searches
214 up from instance to class, and from class to superclass, until a
215 callee with a value is located.
216
217 · If called with one non-ref argument, uses that argument as an index
218 to retrieve from the referenced hash, and returns that value (or
219 undef). If the callee has not had a value defined for this method,
220 searches up from instance to class, and from class to superclass,
221 until a callee with a value is located.
222
223 · If called with one array-ref argument, uses the contents of that
224 array to retrieve a slice of the referenced hash. If the callee has
225 not had a value defined for this method, searches up from instance
226 to class, and from class to superclass, until a callee with a value
227 is located.
228
229 · If called with one hash-ref argument, sets the contents of the ref‐
230 erenced hash to match that provided.
231
232 · If called with a list of key-value pairs, stores the value under
233 the given key in the hash associated with the callee, whether
234 instance or class. If the callee did not previously have a hash-ref
235 value associated with it, searches up instance to class, and from
236 class to superclass, until a callee with a value is located, and
237 copies that hash before making the assignments. The current value
238 under each key will be overwritten, and later arguments with the
239 same key will override earlier ones. Returns the contents of the
240 hash in list context, or a hash reference in scalar context.
241
242 Sample declaration and usage:
243
244 package MyClass;
245 use Class::MakeMethods::Composite::Inheritable (
246 hash => 'baz',
247 );
248 ...
249
250 # Set values by key
251 MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!');
252
253 # Values may be overwritten, and in any order
254 MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
255
256 # Retrieve value by key
257 print MyClass->baz('foo');
258
259 # Retrive slice of values by position
260 print join(', ', MyClass->baz( ['foo', 'bar'] ) );
261
262 # Direct access to referenced hash
263 print keys %{ MyClass->baz() };
264
265 # Reset the hash contents to empty
266 @{ MyClass->baz() } = ();
267
268 NOTE: THIS METHOD GENERATOR IS INCOMPLETE.
269
270 hook - Overrideable array of subroutines
271
272 A hook method is called from the outside as a normal method. However,
273 internally, it contains an array of subroutine references, each of
274 which are called in turn to produce the method's results.
275
276 Subroutines may be added to the hook's array by calling it with a
277 blessed subroutine reference, as shown below. Subroutines may be added
278 on a class-wide basis or on an individual object.
279
280 You might want to use this type of method to provide an easy way for
281 callbacks to be registered.
282
283 package MyClass;
284 use Class::MakeMethods::Composite::Inheritable ( 'hook' => 'init' );
285
286 MyClass->init( Class::MakeMethods::Composite::Inheritable->Hook( sub {
287 my $callee = shift;
288 warn "Init...";
289 } );
290
291 my $obj = MyClass->new;
292 $obj->init();
293
294 object - Overrideable Ref Accessor
295
296 For each method name passed, uses a closure to generate a subroutine
297 with the following characteristics:
298
299 · May be called as a class method, or on any instance or subclass,
300 Must be called on a hash-based instance.
301
302 · The class value will be a reference to an object (or undef).
303
304 · If called without any arguments returns the current value for the
305 callee. If the callee has not had a value defined for this method,
306 searches up from instance to class, and from class to superclass,
307 until a callee with a value is located.
308
309 · If called with an argument, stores that as the value associated
310 with the callee, whether instance or class, and returns it,
311
312 Sample declaration and usage:
313
314 package MyClass;
315 use Class::MakeMethods::Composite::Inheritable (
316 object => 'foo',
317 );
318 ...
319
320 # Store value
321 MyClass->foo( Foozle->new() );
322
323 # Retrieve value
324 print MyClass->foo;
325
326 NOTE: THIS METHOD GENERATOR HAS NOT BEEN WRITTEN YET.
327
329 See Class::MakeMethods for general information about this distribution.
330
331 See Class::MakeMethods::Composite for more about this family of sub‐
332 classes.
333
334
335
336perl v5.8.8 2004-09M-a0k6eMethods::Composite::Inheritable(3)