1MakeMethods::Basic::HasUhs(e3r)Contributed Perl DocumentMaatkieoMnethods::Basic::Hash(3)
2
3
4

NAME

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

SYNOPSIS

9         package MyObject;
10         use Class::MakeMethods::Basic::Hash (
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::Hash subclass of MakeMethods provides a basic constructor
37       and accessors for blessed-hash 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

METHOD GENERATOR TYPES

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

SEE ALSO

221       See Class::MakeMethods for general information about this distribution.
222
223       See Class::MakeMethods::Basic for more about this family of subclasses.
224
225       See Class::MakeMethods::Basic::Array for equivalent functionality based
226       on blessed arrays. If all access to your object is through constructors
227       and accessors declared using this package, and your class will not be
228       extensively subclassed, consider switching to Basic::Array to minimize
229       resource consumption.
230
231
232
233perl v5.8.8                       2004-09-06       MakeMethods::Basic::Hash(3)
Impressum