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