blib/lib/Class/MakeMethods/Evaled/Hash.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod 100% 4 / 4 totaltotal 81.8% 18 / 22 CC 1 SCAR 2.9
CC
1
Cov
83%
array
2.0
hash
2.0
BEGIN
1.0
CRAP 1.3
« blib/lib/Class/MakeMethods/Template/Inheritable.pm ↑ Summary blib/lib/Class/MakeMethods/Emulator/AccessorFast.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Evaled::Hash - Typical hash methods
4  
5  
6 =head1 SYNOPSIS
7  
8 package MyObject;
9 use Class::MakeMethods::Evaled::Hash (
10 new => 'new',
11 scalar => [ 'foo', 'bar' ],
12 array => 'my_list',
13 hash => 'my_index',
14 );
15 ...
16
17 # Constructor
18 my $obj = MyObject->new( foo => 'Foozle' );
19
20 # Scalar Accessor
21 print $obj->foo();
22
23 $obj->bar('Barbados');
24 print $obj->bar();
25
26 # Array accessor
27 $obj->my_list(0 => 'Foozle', 1 => 'Bang!');
28 print $obj->my_list(1);
29
30 # Hash accessor
31 $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
32 print $obj->my_index('foo');
33  
34  
35 =head1 DESCRIPTION
36  
37 The Evaled::Hash subclass of MakeMethods provides a simple constructor and accessors for blessed-hash object instances.
38  
39 =head2 Calling Conventions
40  
41 When you C<use> this package, the method names you provide
42 as arguments cause subroutines to be generated and installed in
43 your module.
44  
45 See L<Class::MakeMethods::Standard/"Calling Conventions"> for a summary, or L<Class::MakeMethods/"USAGE"> for full details.
46  
47 =head2 Declaration Syntax
48  
49 To declare methods, pass in pairs of a method-type name followed
50 by one or more method names. Valid method-type names for this
51 package are listed in L<"METHOD GENERATOR TYPES">.
52  
53 See L<Class::MakeMethods::Standard/"Declaration Syntax"> for more
54 syntax information.
55  
56  
57 =cut
58  
59 package Class::MakeMethods::Evaled::Hash;
60  
61 1 $VERSION = 1.000;
62 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use Class::MakeMethods::Evaled '-isasubclass';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 ########################################################################
66  
67 =head1 METHOD GENERATOR TYPES
68  
69 =head2 new - Constructor
70  
71 For each method name passed, returns a subroutine with the following characteristics:
72  
73 =over 4
74  
75 =item *
76  
77 If called as a class method, makes a new hash and blesses it into that class.
78  
79 =item *
80  
81 If called on a hash-based instance, makes a copy of it and blesses the copy into the same class as the original instance.
82  
83 =item *
84  
85 If passed a list of key-value pairs, appends them to the new hash. These arguments override any copied values, and later arguments with the same name will override earlier ones.
86  
87 =item *
88  
89 Returns the new instance.
90  
91 =back
92  
93 Sample declaration and usage:
94  
95 package MyObject;
96 use Class::MakeMethods::Evaled::Hash (
97 new => 'new',
98 );
99 ...
100
101 # Bare constructor
102 my $empty = MyObject->new();
103
104 # Constructor with initial values
105 my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
106
107 # Copy with overriding value
108 my $copy = $obj->new( bar => 'Bob' );
109  
110 =cut
111  
112 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
113 1 (shift)->evaled_methods( q{
114 sub __NAME__ {
115 my $callee = shift;
116 if ( ref $callee ) {
117 bless { %$callee, @_ }, ref $callee;
118 } else {
119 bless { @_ }, $callee;
120 }
121 }
122 }, @_ )
123 }
124  
125 ########################################################################
126  
127 =head2 scalar - Instance Accessor
128  
129 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
130  
131 =over 4
132  
133 =item *
134  
135 Must be called on a hash-based instance.
136  
137 =item *
138  
139 Uses the method name as a hash key to access the related value for each instance.
140  
141 =item *
142  
143 If called without any arguments returns the current value.
144  
145 =item *
146  
147 If called with an argument, stores that as the value, and returns it,
148  
149 =back
150  
151 Sample declaration and usage:
152  
153 package MyObject;
154 use Class::MakeMethods::Evaled::Hash (
155 scalar => 'foo',
156 );
157 ...
158
159 # Store value
160 $obj->foo('Foozle');
161
162 # Retrieve value
163 print $obj->foo;
164  
165 =cut
166  
167 2 ▶sub scalar {
Subroutine scalar: called CC=1 SCAR=0.0
Pod documented
168 2 (shift)->evaled_methods( q{
169 sub __NAME__ {
170 my $self = shift;
171 if ( scalar @_ ) {
172 $self->{'__NAME__'} = shift;
173 } else {
174 $self->{'__NAME__'};
175 }
176 }
177 }, @_ )
178 }
179  
180 ########################################################################
181  
182 =head2 array - Instance Ref Accessor
183  
184 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
185  
186 =over 4
187  
188 =item *
189  
190 Must be called on a hash-based instance.
191  
192 =item *
193  
194 Uses the method name as a hash key to access the related value for each instance.
195  
196 =item *
197  
198 The value for each instance will be a reference to an array (or undef).
199  
200 =item *
201  
202 If called without any arguments, returns the current array-ref value (or undef).
203  
204 =item *
205  
206 If called with one argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef).
207  
208 =item *
209  
210 If called with a list of index-value pairs, stores the value at the given index in the referenced array. If the instance's value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value.
211  
212 =back
213  
214 Sample declaration and usage:
215
216 package MyObject;
217 use Class::MakeMethods::Evaled::Hash (
218 array => 'bar',
219 );
220 ...
221
222 # Set values by position
223 $obj->bar(0 => 'Foozle', 1 => 'Bang!');
224
225 # Positions may be overwritten, and in any order
226 $obj->bar(2 => 'And Mash', 1 => 'Blah!');
227
228 # Retrieve value by position
229 print $obj->bar(1);
230
231 # Direct access to referenced array
232 print scalar @{ $obj->bar() };
233
234 # Reset the array contents to empty
235 @{ $obj->bar() } = ();
236  
237 =cut
238  
239 0 ▶sub array {
Subroutine array: not called CC=1 SCAR=6.9
Pod documented
240 0 (shift)->evaled_methods( q{
241 sub __NAME__ {
242 my $self = shift;
243 if ( scalar(@_) == 0 ) {
244 return $self->{'__NAME__'};
245 } elsif ( scalar(@_) == 1 ) {
246 $self->{'__NAME__'}->[ shift() ];
247 } elsif ( scalar(@_) % 2 ) {
248 Carp::croak "Odd number of items in assigment to __NAME__";
249 } else {
250 while ( scalar(@_) ) {
251 my $key = shift();
252 $self->{'__NAME__'}->[ $key ] = shift();
253 }
254 return $self->{'__NAME__'};
255 }
256 }
257 }, @_ )
258 }
259  
260 ########################################################################
261  
262 =head2 hash - Instance Ref Accessor
263  
264 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
265  
266 =over 4
267  
268 =item *
269  
270 Must be called on a hash-based instance.
271  
272 =item *
273  
274 Uses the method name as a hash key to access the related value for each instance.
275  
276 =item *
277  
278 The value for each instance will be a reference to a hash (or undef).
279  
280 =item *
281  
282 If called without any arguments, returns the current hash-ref value (or undef).
283  
284 =item *
285  
286 If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef).
287  
288 =item *
289  
290 If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the current hash-ref value.
291  
292 =back
293  
294 Sample declaration and usage:
295
296 package MyObject;
297 use Class::MakeMethods::Evaled::Hash (
298 hash => 'baz',
299 );
300 ...
301
302 # Set values by key
303 $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
304
305 # Values may be overwritten, and in any order
306 $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
307
308 # Retrieve value by key
309 print $obj->baz('foo');
310
311 # Direct access to referenced hash
312 print keys %{ $obj->baz() };
313
314 # Reset the hash contents to empty
315 @{ $obj->baz() } = ();
316  
317 =cut
318  
319 0 ▶sub hash {
Subroutine hash: not called CC=1 SCAR=6.9
Pod documented
320 0 (shift)->evaled_methods( q{
321 sub __NAME__ {
322 my $self = shift;
323 if ( scalar(@_) == 0 ) {
324 return $self->{'__NAME__'};
325 } elsif ( scalar(@_) == 1 ) {
326 $self->{'__NAME__'}->{ shift() };
327 } elsif ( scalar(@_) % 2 ) {
328 Carp::croak "Odd number of items in assigment to '__NAME__'";
329 } else {
330 while ( scalar(@_) ) {
331 $self->{'__NAME__'}->{ shift() } = shift();
332 }
333 return $self->{'__NAME__'};
334 }
335 }
336 }, @_ )
337 }
338  
339 ########################################################################
340  
341 =head1 SEE ALSO
342  
343 See L<Class::MakeMethods> for general information about this distribution.
344  
345 See L<Class::MakeMethods::Evaled> for more about this family of subclasses.
346  
347 =cut
348  
349 1 1;
« blib/lib/Class/MakeMethods/Template/Inheritable.pm ↑ Summary blib/lib/Class/MakeMethods/Emulator/AccessorFast.pm »