1MakeMethods::Basic::ArrUasye(r3)Contributed Perl DocumenMtaakteiMoenthods::Basic::Array(3)
2
3
4
6 Class::MakeMethods::Basic::Array - Basic array methods
7
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
36 The Basic::Array subclass of MakeMethods provides a basic constructor
37 and accessors for blessed-array object instances.
38
39 Calling Conventions
40 When you "use" this package, the method names you provide as arguments
41 cause subroutines to be generated and installed in your module.
42
43 See "Calling Conventions" in Class::MakeMethods::Basic for a summary,
44 or "USAGE" in Class::MakeMethods for full details.
45
46 Declaration Syntax
47 To declare methods, pass in pairs of a method-type name followed by one
48 or more method names. Valid method-type names for this package are
49 listed in "METHOD GENERATOR TYPES".
50
51 See "Declaration Syntax" in Class::MakeMethods::Basic for more syntax
52 information.
53
54 About Positional Accessors
55 Each accessor method claims the next available spot in the array to
56 store its value in.
57
58 The mapping between method names and array positions is stored in a
59 hash named %FIELDS in the target package. When the first positional
60 accessor is defined for a package, its %FIELDS are initialized by
61 searching its inheritance tree.
62
63 Caution: 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
74 new - Constructor
75 For each method name passed, returns a subroutine with the following
76 characteristics:
77
78 • If called as a class method, makes a new array and blesses it into
79 that class.
80
81 • If called on an array-based instance, makes a copy of it and
82 blesses the copy into the same class as the original instance.
83
84 • If passed a list of method-value pairs, calls each named method
85 with the associated value as an argument.
86
87 • Returns the new instance.
88
89 Sample declaration and usage:
90
91 package MyObject;
92 use Class::MakeMethods::Basic::Array (
93 new => 'new',
94 );
95 ...
96
97 # Bare constructor
98 my $empty = MyObject->new();
99
100 # Constructor with initial sequence of method calls
101 my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
102
103 # Copy with overriding sequence of method calls
104 my $copy = $obj->new( bar => 'Bob' );
105
106 scalar - Instance Accessor
107 For each method name passed, uses a closure to generate a subroutine
108 with the following characteristics:
109
110 • Must be called on an array-based instance.
111
112 • Determines the array position associated with the method name, and
113 uses that as an index into each instance to access the related
114 value.
115
116 • If called without any arguments returns the current value (or
117 undef).
118
119 • If called with an argument, stores that as the value, and returns
120 it,
121
122 Sample declaration and usage:
123
124 package MyObject;
125 use Class::MakeMethods::Basic::Array (
126 scalar => 'foo',
127 );
128 ...
129
130 # Store value
131 $obj->foo('Foozle');
132
133 # Retrieve value
134 print $obj->foo;
135
136 array - Instance Ref Accessor
137 For each method name passed, uses a closure to generate a subroutine
138 with the following characteristics:
139
140 • Must be called on an array-based instance.
141
142 • Determines the array position associated with the method name, and
143 uses that as an index into each instance to access the related
144 value.
145
146 • The value for each instance will be a reference to an array (or
147 undef).
148
149 • If called without any arguments, returns the current array-ref
150 value (or undef).
151
152 • If called with one argument, uses that argument as an index to
153 retrieve from the referenced array, and returns that value (or
154 undef). If the single argument is an array ref, then a slice of the
155 referenced array is returned.
156
157 • If called with a list of index-value pairs, stores the value at the
158 given index in the referenced array. If the instance's value was
159 previously undefined, a new array is autovivified. The current
160 value in each position will be overwritten, and later arguments
161 with the same index will override earlier ones. Returns the current
162 array-ref value.
163
164 Sample declaration and usage:
165
166 package MyObject;
167 use Class::MakeMethods::Basic::Array (
168 array => 'bar',
169 );
170 ...
171
172 # Set values by position
173 $obj->bar(0 => 'Foozle', 1 => 'Bang!');
174
175 # Positions may be overwritten, and in any order
176 $obj->bar(2 => 'And Mash', 1 => 'Blah!');
177
178 # Retrieve value by position
179 print $obj->bar(1);
180
181 # Retrieve slice of values by position
182 print join(', ', $obj->bar( [0, 2] ) );
183
184 # Direct access to referenced array
185 print scalar @{ $obj->bar() };
186
187 # Reset the array contents to empty
188 @{ $obj->bar() } = ();
189
190 hash - Instance Ref Accessor
191 For each method name passed, uses a closure to generate a subroutine
192 with the following characteristics:
193
194 • Must be called on an array-based instance.
195
196 • Determines the array position associated with the method name, and
197 uses that as an index into each instance to access the related
198 value.
199
200 • The value for each instance will be a reference to a hash (or
201 undef).
202
203 • If called without any arguments, returns the current hash-ref value
204 (or undef).
205
206 • If called with one argument, uses that argument as an index to
207 retrieve from the referenced hash, and returns that value (or
208 undef). If the single argument is an array ref, then a slice of the
209 referenced hash is returned.
210
211 • If called with a list of key-value pairs, stores the value under
212 the given key in the referenced hash. If the instance's value was
213 previously undefined, a new hash is autovivified. The current value
214 under each key will be overwritten, and later arguments with the
215 same key will override earlier ones. Returns the current hash-ref
216 value.
217
218 Sample declaration and usage:
219
220 package MyObject;
221 use Class::MakeMethods::Basic::Array (
222 hash => 'baz',
223 );
224 ...
225
226 # Set values by key
227 $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
228
229 # Values may be overwritten, and in any order
230 $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
231
232 # Retrieve value by key
233 print $obj->baz('foo');
234
235 # Retrieve slice of values by position
236 print join(', ', $obj->baz( ['foo', 'bar'] ) );
237
238 # Direct access to referenced hash
239 print keys %{ $obj->baz() };
240
241 # Reset the hash contents to empty
242 @{ $obj->baz() } = ();
243
245 See Class::MakeMethods for general information about this distribution.
246
247 See Class::MakeMethods::Basic for more about this family of subclasses.
248
249
250
251perl v5.36.0 2023-01-20 MakeMethods::Basic::Array(3)