1MakeMethods::Composite:U:sAerrraCyo(n3t)ributed Perl DocMuamkeenMteatthioodns::Composite::Array(3)
2
3
4
6 Class::MakeMethods::Composite::Array - Basic array methods
7
9 package MyObject;
10 use Class::MakeMethods::Composite::Array (
11 new => 'new',
12 scalar => [ 'foo', 'bar' ],
13 array => 'my_list',
14 hash => 'my_index',
15 );
16 ...
17
18 my $obj = MyObject->new( foo => 'Foozle' );
19 print $obj->foo();
20
21 $obj->bar('Barbados');
22 print $obj->bar();
23
24 $obj->my_list(0 => 'Foozle', 1 => 'Bang!');
25 print $obj->my_list(1);
26
27 $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
28 print $obj->my_index('foo');
29
31 The Composite::Array suclass of MakeMethods provides a basic construc‐
32 tor and accessors for blessed-array object instances.
33
34 Class::MakeMethods Calling Conventions
35
36 When you "use" this package, the method declarations you provide as
37 arguments cause subroutines to be generated and installed in your mod‐
38 ule.
39
40 You can also omit the arguments to "use" and instead make methods at
41 runtime by passing the declarations to a subsequent call to "make()".
42
43 You may include any number of declarations in each call to "use" or
44 "make()". If methods with the same name already exist, earlier calls to
45 "use" or "make()" win over later ones, but within each call, later dec‐
46 larations superceed earlier ones.
47
48 You can install methods in a different package by passing "-TargetClass
49 => package" as your first arguments to "use" or "make".
50
51 See Class::MakeMethods for more details.
52
53 Class::MakeMethods::Basic Declaration Syntax
54
55 The following types of Basic declarations are supported:
56
57 · generator_type => "method_name"
58
59 · generator_type => "name_1 name_2..."
60
61 · generator_type => [ "name_1", "name_2", ...]
62
63 See the "METHOD GENERATOR TYPES" section below for a list of the sup‐
64 ported values of generator_type.
65
66 For each method name you provide, a subroutine of the indicated type
67 will be generated and installed under that name in your module.
68
69 Method names should start with a letter, followed by zero or more let‐
70 ters, numbers, or underscores.
71
72 Class::MakeMethods::Composite Declaration Syntax
73
74 The Composite syntax also provides several ways to optionally associate
75 a hash of additional parameters with a given method name.
76
77 · generator_type => [ "name_1" => { param=>value... }, ... ]
78
79 A hash of parameters to use just for this method name.
80
81 (Note: to prevent confusion with self-contained definition hashes,
82 described below, parameter hashes following a method name must not
83 contain the key 'name'.)
84
85 · generator_type => [ [ "name_1", "name_2", ... ] => {
86 param=>value... } ]
87
88 Each of these method names gets a copy of the same set of parame‐
89 ters.
90
91 · generator_type => [ { "name"=>"name_1", param=>value... }, ... ]
92
93 By including the reserved parameter "name", you create a self con‐
94 tained declaration with that name and any associated hash values.
95
96 Basic declarations, as described above, are treated as having an empty
97 parameter hash.
98
99 Positional Accessors and %FIELDS
100
101 Each accessor method is assigned the next available array index at
102 which to store its value.
103
104 The mapping between method names and array positions is stored in a
105 hash named %FIELDS in the declaring package. When a package declares
106 its first positional accessor, its %FIELDS are initialized by searching
107 its inheritance tree.
108
109 Warning: Subclassing packages that use positional accessors is somewhat
110 fragile, since you may end up with two distinct methods assigned to the
111 same position. Specific cases to avoid are:
112
113 · If you inherit from more than one class with positional accessors,
114 the positions used by the two sets of methods will overlap.
115
116 · If your superclass adds additional positional accessors after you
117 declare your first, they will overlap yours.
118
120 new - Constructor
121
122 For each method name passed, returns a subroutine with the following
123 characteristics:
124
125 · Has a reference to a sample item to copy. This defaults to a refer‐
126 ence to an empty array, but you may override this with the
127 "'defaults' =" array_ref> method parameter.
128
129 · If called as a class method, makes a new array containing values
130 from the sample item, and blesses it into that class.
131
132 · If called on an array-based instance, makes a copy of it and
133 blesses the copy into the same class as the original instance.
134
135 · If passed a list of method-value pairs, calls each named method
136 with the associated value as an argument.
137
138 · Returns the new instance.
139
140 Sample declaration and usage:
141
142 package MyObject;
143 use Class::MakeMethods::Composite::Array (
144 new => 'new',
145 );
146 ...
147
148 # Bare constructor
149 my $empty = MyObject->new();
150
151 # Constructor with initial sequence of method calls
152 my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
153
154 # Copy with overriding sequence of method calls
155 my $copy = $obj->new( bar => 'Bob' );
156
157 new_with_values - Constructor
158
159 For each method name passed, returns a subroutine with the following
160 characteristics:
161
162 · May be called as a class method, or (equivalently) on any existing
163 object of that class.
164
165 · Creates an array, blesses it into the class, and returns the new
166 instance.
167
168 · If no arguments are provided, the returned array will be empty. If
169 passed a single array-ref argument, copies its contents into the
170 new array. If called with multiple arguments, copies them into the
171 new array. (Note that this is a "shallow" copy, not a "deep"
172 clone.)
173
174 Sample declaration and usage:
175
176 package MyObject;
177 use Class::MakeMethods::Composite::Array (
178 new => 'new',
179 );
180 ...
181
182 # Bare constructor
183 my $empty = MyObject->new();
184
185 # Constructor with initial sequence of method calls
186 my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
187
188 # Copy with overriding sequence of method calls
189 my $copy = $obj->new( bar => 'Bob' );
190
191 scalar - Instance Accessor
192
193 For each method name passed, uses a closure to generate a subroutine
194 with the following characteristics:
195
196 · Must be called on an array-based instance.
197
198 · Determines the array position associated with the method name, and
199 uses that as an index into each instance to access the related
200 value. This defaults to the next available slot in %FIELDS, but you
201 may override this with the "'array_index' =" number> method parame‐
202 ter, or by pre-filling the contents of %FIELDS.
203
204 · If called without any arguments returns the current value (or
205 undef).
206
207 · If called with an argument, stores that as the value, and returns
208 it,
209
210 · If called with multiple arguments, stores a reference to a new
211 array with those arguments as contents, and returns that array ref‐
212 erence.
213
214 Sample declaration and usage:
215
216 package MyObject;
217 use Class::MakeMethods::Composite::Array (
218 scalar => 'foo',
219 );
220 ...
221
222 # Store value
223 $obj->foo('Foozle');
224
225 # Retrieve value
226 print $obj->foo;
227
228 array - Instance Ref Accessor
229
230 For each method name passed, uses a closure to generate a subroutine
231 with the following characteristics:
232
233 · Must be called on an array-based instance.
234
235 · Determines the array position associated with the method name, and
236 uses that as an index into each instance to access the related
237 value. This defaults to the next available slot in %FIELDS, but you
238 may override this with the "'array_index' =" number> method parame‐
239 ter, or by pre-filling the contents of %FIELDS.
240
241 · The value for each instance will be a reference to an array (or
242 undef).
243
244 · If called without any arguments, returns the current array-ref
245 value (or undef).
246
247 · If called with a single non-ref argument, uses that argument as an
248 index to retrieve from the referenced array, and returns that value
249 (or undef).
250
251 · If called with a single array ref argument, uses that list to
252 return a slice of the referenced array.
253
254 · If called with a list of argument pairs, each with a non-ref index
255 and an associated value, stores the value at the given index in the
256 referenced array. If the instance's value was previously undefined,
257 a new array is autovivified. The current value in each position
258 will be overwritten, and later arguments with the same index will
259 override earlier ones. Returns the current array-ref value.
260
261 · If called with a list of argument pairs, each with the first item
262 being a reference to an array of up to two numbers, loops over each
263 pair and uses those numbers to splice the value array.
264
265 The first controlling number is the position at which the splice
266 will begin. Zero will start before the first item in the list. Neg‐
267 ative numbers count backwards from the end of the array.
268
269 The second number is the number of items to be removed from the
270 list. If it is omitted, or undefined, or zero, no items are
271 removed. If it is a positive integer, that many items will be
272 returned.
273
274 If both numbers are omitted, or are both undefined, they default to
275 containing the entire value array.
276
277 If the second argument is undef, no values will be inserted; if it
278 is a non-reference value, that one value will be inserted; if it is
279 an array-ref, its values will be copied.
280
281 The method returns the items that removed from the array, if any.
282
283 Sample declaration and usage:
284
285 package MyObject;
286 use Class::MakeMethods::Composite::Array (
287 array => 'bar',
288 );
289 ...
290
291 # Clear and set contents of list
292 print $obj->bar([ 'Spume', 'Frost' ] );
293
294 # Set values by position
295 $obj->bar(0 => 'Foozle', 1 => 'Bang!');
296
297 # Positions may be overwritten, and in any order
298 $obj->bar(2 => 'And Mash', 1 => 'Blah!');
299
300 # Retrieve value by position
301 print $obj->bar(1);
302
303 # Direct access to referenced array
304 print scalar @{ $obj->bar() };
305
306 There are also calling conventions for slice and splice operations:
307
308 # Retrieve slice of values by position
309 print join(', ', $obj->bar( undef, [0, 2] ) );
310
311 # Insert an item at position in the array
312 $obj->bar([3], 'Potatoes' );
313
314 # Remove 1 item from position 3 in the array
315 $obj->bar([3, 1], undef );
316
317 # Set a new value at position 2, and return the old value
318 print $obj->bar([2, 1], 'Froth' );
319
320 hash - Instance Ref Accessor
321
322 For each method name passed, uses a closure to generate a subroutine
323 with the following characteristics:
324
325 · Must be called on an array-based instance.
326
327 · Determines the array position associated with the method name, and
328 uses that as an index into each instance to access the related
329 value. This defaults to the next available slot in %FIELDS, but you
330 may override this with the "'array_index' =" number> method parame‐
331 ter, or by pre-filling the contents of %FIELDS.
332
333 · The value for each instance will be a reference to a hash (or
334 undef).
335
336 · If called without any arguments, returns the contents of the hash
337 in list context, or a hash reference in scalar context (or undef).
338
339 · If called with one non-ref argument, uses that argument as an index
340 to retrieve from the referenced hash, and returns that value (or
341 undef).
342
343 · If called with one array-ref argument, uses the contents of that
344 array to retrieve a slice of the referenced hash.
345
346 · If called with one hash-ref argument, sets the contents of the ref‐
347 erenced hash to match that provided.
348
349 · If called with a list of key-value pairs, stores the value under
350 the given key in the referenced hash. If the instance's value was
351 previously undefined, a new hash is autovivified. The current value
352 under each key will be overwritten, and later arguments with the
353 same key will override earlier ones. Returns the contents of the
354 hash in list context, or a hash reference in scalar context.
355
356 Sample declaration and usage:
357
358 package MyObject;
359 use Class::MakeMethods::Composite::Array (
360 hash => 'baz',
361 );
362 ...
363
364 # Set values by key
365 $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
366
367 # Values may be overwritten, and in any order
368 $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
369
370 # Retrieve value by key
371 print $obj->baz('foo');
372
373 # Retrive slice of values by position
374 print join(', ', $obj->baz( ['foo', 'bar'] ) );
375
376 # Direct access to referenced hash
377 print keys %{ $obj->baz() };
378
379 # Reset the hash contents to empty
380 @{ $obj->baz() } = ();
381
382 object - Instance Ref Accessor
383
384 For each method name passed, uses a closure to generate a subroutine
385 with the following characteristics:
386
387 · Must be called on an array-based instance.
388
389 · Determines the array position associated with the method name, and
390 uses that as an index into each instance to access the related
391 value. This defaults to the next available slot in %FIELDS, but you
392 may override this with the "'array_index' =" number> method parame‐
393 ter, or by pre-filling the contents of %FIELDS.
394
395 · The value for each instance will be a reference to an object (or
396 undef).
397
398 · If called without any arguments returns the current value.
399
400 · If called with an argument, stores that as the value, and returns
401 it,
402
403 Sample declaration and usage:
404
405 package MyObject;
406 use Class::MakeMethods::Composite::Hash (
407 object => 'foo',
408 );
409 ...
410
411 # Store value
412 $obj->foo( Foozle->new() );
413
414 # Retrieve value
415 print $obj->foo;
416
418 See Class::MakeMethods for general information about this distribution.
419
420 See Class::MakeMethods::Composite for more about this family of sub‐
421 classes.
422
423
424
425perl v5.8.8 2004-09-06 MakeMethods::Composite::Array(3)