1MakeMethods::Composite:U:sHearshC(o3n)tributed Perl DocuMmaeknetMaettihoonds::Composite::Hash(3)
2
3
4

NAME

6       Class::MakeMethods::Composite::Hash - Composite hash methods
7

SYNOPSIS

9         package MyObject;
10         use Class::MakeMethods::Composite::Hash (
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 Composite::Hash suclass of MakeMethods provides a basic constructor
32       and accessors for blessed-hash object instances.
33
34       Class::MakeMethods Calling Interface
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 given an empty parameter
97       hash.
98

METHOD GENERATOR TYPES

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

SEE ALSO

373       See Class::MakeMethods for general information about this distribution.
374
375       See Class::MakeMethods::Composite for more about this family of sub‐
376       classes.
377
378
379
380perl v5.8.8                       2004-09-06   MakeMethods::Composite::Hash(3)
Impressum