1MakeMethods::Basic::ArrUasye(r3)Contributed Perl DocumenMtaakteiMoenthods::Basic::Array(3)
2
3
4

NAME

6       Class::MakeMethods::Basic::Array - Basic array methods
7

SYNOPSIS

9         package MyObject;
10         use Class::MakeMethods::Basic::Array (
11           new => 'new',
12           scalar => [ 'foo', 'bar' ],
13           array => 'my_list',
14           hash => 'my_index',
15         );
16         ...
17
18         # Constructor
19         my $obj = MyObject->new( foo => 'Foozle' );
20
21         # Scalar Accessor
22         print $obj->foo();
23
24         $obj->bar('Barbados');
25         print $obj->bar();
26
27         # Array accessor
28         $obj->my_list(0 => 'Foozle', 1 => 'Bang!');
29         print $obj->my_list(1);
30
31         # Hash accessor
32         $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
33         print $obj->my_index('foo');
34

DESCRIPTION

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

METHOD GENERATOR TYPES

77       new - Constructor
78
79       For each method name passed, returns a subroutine with the following
80       characteristics:
81
82       ·   If called as a class method, makes a new array and blesses it into
83           that class.
84
85       ·   If called on an array-based instance, makes a copy of it and
86           blesses the copy into the same class as the original instance.
87
88       ·   If passed a list of method-value pairs, calls each named method
89           with the associated value as an argument.
90
91       ·   Returns the new instance.
92
93       Sample declaration and usage:
94
95         package MyObject;
96         use Class::MakeMethods::Basic::Array (
97           new => 'new',
98         );
99         ...
100
101         # Bare constructor
102         my $empty = MyObject->new();
103
104         # Constructor with initial sequence of method calls
105         my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
106
107         # Copy with overriding sequence of method calls
108         my $copy = $obj->new( bar => 'Bob' );
109
110       scalar - Instance Accessor
111
112       For each method name passed, uses a closure to generate a subroutine
113       with the following characteristics:
114
115       ·   Must be called on an array-based instance.
116
117       ·   Determines the array position associated with the method name, and
118           uses that as an index into each instance to access the related
119           value.
120
121       ·   If called without any arguments returns the current value (or
122           undef).
123
124       ·   If called with an argument, stores that as the value, and returns
125           it,
126
127       Sample declaration and usage:
128
129         package MyObject;
130         use Class::MakeMethods::Basic::Array (
131           scalar => 'foo',
132         );
133         ...
134
135         # Store value
136         $obj->foo('Foozle');
137
138         # Retrieve value
139         print $obj->foo;
140
141       array - Instance Ref Accessor
142
143       For each method name passed, uses a closure to generate a subroutine
144       with the following characteristics:
145
146       ·   Must be called on an array-based instance.
147
148       ·   Determines the array position associated with the method name, and
149           uses that as an index into each instance to access the related
150           value.
151
152       ·   The value for each instance will be a reference to an array (or
153           undef).
154
155       ·   If called without any arguments, returns the current array-ref
156           value (or undef).
157
158       ·   If called with one argument, uses that argument as an index to
159           retrieve from the referenced array, and returns that value (or
160           undef). If the single argument is an array ref, then a slice of the
161           referenced array is returned.
162
163       ·   If called with a list of index-value pairs, stores the value at the
164           given index in the referenced array. If the instance's value was
165           previously undefined, a new array is autovivified. The current
166           value in each position will be overwritten, and later arguments
167           with the same index will override earlier ones. Returns the current
168           array-ref value.
169
170       Sample declaration and usage:
171
172         package MyObject;
173         use Class::MakeMethods::Basic::Array (
174           array => 'bar',
175         );
176         ...
177
178         # Set values by position
179         $obj->bar(0 => 'Foozle', 1 => 'Bang!');
180
181         # Positions may be overwritten, and in any order
182         $obj->bar(2 => 'And Mash', 1 => 'Blah!');
183
184         # Retrieve value by position
185         print $obj->bar(1);
186
187         # Retrieve slice of values by position
188         print join(', ', $obj->bar( [0, 2] ) );
189
190         # Direct access to referenced array
191         print scalar @{ $obj->bar() };
192
193         # Reset the array contents to empty
194         @{ $obj->bar() } = ();
195
196       hash - Instance Ref Accessor
197
198       For each method name passed, uses a closure to generate a subroutine
199       with the following characteristics:
200
201       ·   Must be called on an array-based instance.
202
203       ·   Determines the array position associated with the method name, and
204           uses that as an index into each instance to access the related
205           value.
206
207       ·   The value for each instance will be a reference to a hash (or
208           undef).
209
210       ·   If called without any arguments, returns the current hash-ref value
211           (or undef).
212
213       ·   If called with one argument, uses that argument as an index to
214           retrieve from the referenced hash, and returns that value (or
215           undef). If the single argument is an array ref, then a slice of the
216           referenced hash is returned.
217
218       ·   If called with a list of key-value pairs, stores the value under
219           the given key in the referenced hash. If the instance's value was
220           previously undefined, a new hash is autovivified. The current value
221           under each key will be overwritten, and later arguments with the
222           same key will override earlier ones. Returns the current hash-ref
223           value.
224
225       Sample declaration and usage:
226
227         package MyObject;
228         use Class::MakeMethods::Basic::Array (
229           hash => 'baz',
230         );
231         ...
232
233         # Set values by key
234         $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
235
236         # Values may be overwritten, and in any order
237         $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
238
239         # Retrieve value by key
240         print $obj->baz('foo');
241
242         # Retrieve slice of values by position
243         print join(', ', $obj->baz( ['foo', 'bar'] ) );
244
245         # Direct access to referenced hash
246         print keys %{ $obj->baz() };
247
248         # Reset the hash contents to empty
249         @{ $obj->baz() } = ();
250

SEE ALSO

252       See Class::MakeMethods for general information about this distribution.
253
254       See Class::MakeMethods::Basic for more about this family of subclasses.
255
256
257
258perl v5.8.8                       2004-09-06      MakeMethods::Basic::Array(3)
Impressum