1MakeMethods::Standard::UAsrerrayC(o3n)tributed Perl DocuMmaeknetMaettihoonds::Standard::Array(3)
2
3
4

NAME

6       Class::MakeMethods::Standard::Array - Methods for Array objects
7

SYNOPSIS

9         package MyObject;
10         use Class::MakeMethods::Standard::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

DESCRIPTION

31       The Standard::Array suclass of MakeMethods provides a basic constructor
32       and accessors for blessed-array object instances.
33
34       Calling Conventions
35
36       When you "use" this package, the method names you provide as arguments
37       cause subroutines to be generated and installed in your module.
38
39       See "Calling Conventions" in Class::MakeMethods::Standard for more
40       information.
41
42       Declaration Syntax
43
44       To declare methods, pass in pairs of a method-type name followed by one
45       or more method names.
46
47       Valid method-type names for this package are listed in "METHOD GENERA‐
48       TOR TYPES".
49
50       See "Declaration Syntax" in Class::MakeMethods::Standard and "Parameter
51       Syntax" in Class::MakeMethods::Standard for more information.
52
53       Positional Accessors and %FIELDS
54
55       Each accessor method is assigned the next available array index at
56       which to store its value.
57
58       The mapping between method names and array positions is stored in a
59       hash named %FIELDS in the declaring package. When a package declares
60       its first positional accessor, its %FIELDS are initialized by searching
61       its inheritance tree.
62
63       Warning: Subclassing packages that use positional accessors is somewhat
64       fragile, since you may end up with two distinct methods assigned to the
65       same position. Specific cases to avoid are:
66
67       ·   If you inherit from more than one class with positional accessors,
68           the positions used by the two sets of methods will overlap.
69
70       ·   If your superclass adds additional positional accessors after you
71           declare your first, they will overlap yours.
72

METHOD GENERATOR TYPES

74       new - Constructor
75
76       For each method name passed, returns a subroutine with the following
77       characteristics:
78
79       ·   Has a reference to a sample item to copy. This defaults to a refer‐
80           ence to an empty array, but you may override this with the
81           "'defaults' =" array_ref> method parameter.
82
83       ·   If called as a class method, makes a new array containing values
84           from the sample item, and blesses it into that class.
85
86       ·   If called on an array-based instance, makes a copy of it and
87           blesses the copy into the same class as the original instance.
88
89       ·   If passed a list of method-value pairs, calls each named method
90           with the associated value as an argument.
91
92       ·   Returns the new instance.
93
94       Sample declaration and usage:
95
96         package MyObject;
97         use Class::MakeMethods::Standard::Array (
98           new => 'new',
99         );
100         ...
101
102         # Bare constructor
103         my $empty = MyObject->new();
104
105         # Constructor with initial sequence of method calls
106         my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
107
108         # Copy with overriding sequence of method calls
109         my $copy = $obj->new( bar => 'Bob' );
110
111       scalar - Instance Accessor
112
113       For each method name passed, uses a closure to generate a subroutine
114       with the following characteristics:
115
116       ·   Must be called on an array-based instance.
117
118       ·   Determines the array position associated with the method name, and
119           uses that as an index into each instance to access the related
120           value. This defaults to the next available slot in %FIELDS, but you
121           may override this with the "'array_index' =" number> method parame‐
122           ter, or by pre-filling the contents of %FIELDS.
123
124       ·   If called without any arguments returns the current value (or
125           undef).
126
127       ·   If called with an argument, stores that as the value, and returns
128           it,
129
130       Sample declaration and usage:
131
132         package MyObject;
133         use Class::MakeMethods::Standard::Array (
134           scalar => 'foo',
135         );
136         ...
137
138         # Store value
139         $obj->foo('Foozle');
140
141         # Retrieve value
142         print $obj->foo;
143
144       array - Instance Ref Accessor
145
146       For each method name passed, uses a closure to generate a subroutine
147       with the following characteristics:
148
149       ·   Must be called on an array-based instance.
150
151       ·   Determines the array position associated with the method name, and
152           uses that as an index into each instance to access the related
153           value. This defaults to the next available slot in %FIELDS, but you
154           may override this with the "'array_index' =" number> method parame‐
155           ter, or by pre-filling the contents of %FIELDS.
156
157       ·   The value for each instance will be a reference to an array (or
158           undef).
159
160       ·   If called without any arguments, returns the current array-ref
161           value (or undef).
162
163       ·   If called with a single non-ref argument, uses that argument as an
164           index to retrieve from the referenced array, and returns that value
165           (or undef).
166
167       ·   If called with a single array ref argument, uses that list to
168           return a slice of the referenced array.
169
170       ·   If called with a list of argument pairs, each with a non-ref index
171           and an associated value, stores the value at the given index in the
172           referenced array. If the instance's value was previously undefined,
173           a new array is autovivified. The current value in each position
174           will be overwritten, and later arguments with the same index will
175           override earlier ones. Returns the current array-ref value.
176
177       ·   If called with a list of argument pairs, each with the first item
178           being a reference to an array of up to two numbers, loops over each
179           pair and uses those numbers to splice the value array.
180
181           The first controlling number is the position at which the splice
182           will begin. Zero will start before the first item in the list. Neg‐
183           ative numbers count backwards from the end of the array.
184
185           The second number is the number of items to be removed from the
186           list. If it is omitted, or undefined, or zero, no items are
187           removed. If it is a positive integer, that many items will be
188           returned.
189
190           If both numbers are omitted, or are both undefined, they default to
191           containing the entire value array.
192
193           If the second argument is undef, no values will be inserted; if it
194           is a non-reference value, that one value will be inserted; if it is
195           an array-ref, its values will be copied.
196
197           The method returns the items that removed from the array, if any.
198
199       Sample declaration and usage:
200
201         package MyObject;
202         use Class::MakeMethods::Standard::Array (
203           array => 'bar',
204         );
205         ...
206
207         # Clear and set contents of list
208         print $obj->bar([ 'Spume', 'Frost' ] );
209
210         # Set values by position
211         $obj->bar(0 => 'Foozle', 1 => 'Bang!');
212
213         # Positions may be overwritten, and in any order
214         $obj->bar(2 => 'And Mash', 1 => 'Blah!');
215
216         # Retrieve value by position
217         print $obj->bar(1);
218
219         # Direct access to referenced array
220         print scalar @{ $obj->bar() };
221
222       There are also calling conventions for slice and splice operations:
223
224         # Retrieve slice of values by position
225         print join(', ', $obj->bar( undef, [0, 2] ) );
226
227         # Insert an item at position in the array
228         $obj->bar([3], 'Potatoes' );
229
230         # Remove 1 item from position 3 in the array
231         $obj->bar([3, 1], undef );
232
233         # Set a new value at position 2, and return the old value
234         print $obj->bar([2, 1], 'Froth' );
235
236       hash - Instance Ref Accessor
237
238       For each method name passed, uses a closure to generate a subroutine
239       with the following characteristics:
240
241       ·   Must be called on an array-based instance.
242
243       ·   Determines the array position associated with the method name, and
244           uses that as an index into each instance to access the related
245           value. This defaults to the next available slot in %FIELDS, but you
246           may override this with the "'array_index' =" number> method parame‐
247           ter, or by pre-filling the contents of %FIELDS.
248
249       ·   The value for each instance will be a reference to a hash (or
250           undef).
251
252       ·   If called without any arguments, returns the contents of the hash
253           in list context, or a hash reference in scalar context (or undef).
254
255       ·   If called with one argument, uses that argument as an index to
256           retrieve from the referenced hash, and returns that value (or
257           undef). If the single argument is an array ref, then a slice of the
258           referenced hash is returned.
259
260       ·   If called with a list of key-value pairs, stores the value under
261           the given key in the referenced hash. If the instance's value was
262           previously undefined, a new hash is autovivified. The current value
263           under each key will be overwritten, and later arguments with the
264           same key will override earlier ones. Returns the contents of the
265           hash in list context, or a hash reference in scalar context.
266
267       Sample declaration and usage:
268
269         package MyObject;
270         use Class::MakeMethods::Standard::Array (
271           hash => 'baz',
272         );
273         ...
274
275         # Set values by key
276         $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
277
278         # Values may be overwritten, and in any order
279         $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
280
281         # Retrieve value by key
282         print $obj->baz('foo');
283
284         # Retrive slice of values by position
285         print join(', ', $obj->baz( ['foo', 'bar'] ) );
286
287         # Direct access to referenced hash
288         print keys %{ $obj->baz() };
289
290         # Reset the hash contents to empty
291         @{ $obj->baz() } = ();
292
293       object - Instance Ref Accessor
294
295       For each method name passed, uses a closure to generate a subroutine
296       with the following characteristics:
297
298       ·   Must be called on an array-based instance.
299
300       ·   Determines the array position associated with the method name, and
301           uses that as an index into each instance to access the related
302           value. This defaults to the next available slot in %FIELDS, but you
303           may override this with the "'array_index' =" number> method parame‐
304           ter, or by pre-filling the contents of %FIELDS.
305
306       ·   The value for each instance will be a reference to an object (or
307           undef).
308
309       ·   If called without any arguments returns the current value.
310
311       ·   If called with an argument, stores that as the value, and returns
312           it,
313
314       Sample declaration and usage:
315
316         package MyObject;
317         use Class::MakeMethods::Standard::Hash (
318           object => 'foo',
319         );
320         ...
321
322         # Store value
323         $obj->foo( Foozle->new() );
324
325         # Retrieve value
326         print $obj->foo;
327

SEE ALSO

329       See Class::MakeMethods for general information about this distribution.
330
331       See Class::MakeMethods::Standard for more about this family of sub‐
332       classes.
333
334       See Class::MakeMethods::Standard::Hash for equivalent functionality
335       based on blessed hashes. If your module will be extensively subclassed,
336       consider switching to Standard::Hash to avoid the subclassing concerns
337       described above.
338
339
340
341perl v5.8.8                       2004-09-06   MakeMethods::Standard::Array(3)
Impressum