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