blib/lib/Class/MakeMethods/Basic/Array.pm

filter: Statementstmt 62.0% 49 / 79 Branchbran 40.0% 12 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 8 / 12 Podpod 100% 4 / 4 totaltotal 58.4% 73 / 125 CC 16 SCAR 25.5
CC
16
Cov
56%
__ANON__
30.0
__ANON__
30.0
_array_index
5.0
CRAP 12.9
« blib/lib/Class/MakeMethods/Emulator/MethodMaker.pm ↑ Summary blib/lib/Class/MakeMethods/Template/Ref.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Basic::Array - Basic array methods
4  
5 =head1 SYNOPSIS
6  
7 package MyObject;
8 use Class::MakeMethods::Basic::Array (
9 new => 'new',
10 scalar => [ 'foo', 'bar' ],
11 array => 'my_list',
12 hash => 'my_index',
13 );
14 ...
15
16 # Constructor
17 my $obj = MyObject->new( foo => 'Foozle' );
18
19 # Scalar Accessor
20 print $obj->foo();
21
22 $obj->bar('Barbados');
23 print $obj->bar();
24
25 # Array accessor
26 $obj->my_list(0 => 'Foozle', 1 => 'Bang!');
27 print $obj->my_list(1);
28
29 # Hash accessor
30 $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
31 print $obj->my_index('foo');
32  
33  
34 =head1 DESCRIPTION
35  
36 The Basic::Array subclass of MakeMethods provides a basic
37 constructor and accessors for blessed-array 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::Basic/"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::Basic/"Declaration Syntax"> for more
54 syntax information.
55  
56 =cut
57  
58 package Class::MakeMethods::Basic::Array;
59  
60 1 $VERSION = 1.000;
61 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use Class::MakeMethods '-isasubclass';
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 ########################################################################
65  
66 =head2 About Positional Accessors
67  
68 Each accessor method claims the next available spot in the array
69 to store its value in.
70  
71 The mapping between method names and array positions is stored in
72 a hash named %FIELDS in the target package. When the first positional
73 accessor is defined for a package, its %FIELDS are initialized by
74 searching its inheritance tree.
75  
76 B<Caution>: Subclassing packages that use positional accessors is
77 somewhat fragile, since you may end up with two distinct methods
78 assigned to the same position. Specific cases to avoid are:
79  
80 =over 4
81  
82 =item *
83  
84 If you inherit from more than one class with positional accessors,
85 the positions used by the two sets of methods will overlap.
86  
87 =item *
88  
89 If your superclass adds additional positional accessors after you
90 declare your first, they will overlap yours.
91  
92 =back
93  
94 =cut
95  
96 3 ▶sub _array_index {
Subroutine _array_index: called CC=5 SCAR=16.2
97 3 my $class = shift;
98 3 my $name = shift;
99 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 3 local $^W = 0;
101 3 ▶ if ( ! scalar %{$class . "::FIELDS"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar %{$class . '::FIELDS';}) 2 1 3
102 2 my @classes = @{$class . "::ISA"};
103 2 my @fields;
104 2 ▶ while ( @classes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@classes) 1 2 3
105 1 my $superclass = shift @classes;
106 1 ▶ if ( scalar %{$superclass . "::FIELDS"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %{$superclass . '::FIELDS';}) { } 1 0 1
107 1 push @fields, %{$superclass . "::FIELDS"};
108 } else {
109 0 unshift @classes, @{$superclass . "::ISA"}
110 }
111 }
112 2 %{$class . "::FIELDS"} = @fields
113 }
114 3 my $field_hash = \%{$class . "::FIELDS"};
115 3 ▶ $field_hash->{$name} or $field_hash->{$name} = scalar keys %$field_hash
Branch: 1 branch
BranchTrueFalseTotal
$field_hash->{$name} or $field_hash->{$name} = scalar keys %$field_hash 0 3 3
116 }
117  
118 ########################################################################
119  
120 =head1 METHOD GENERATOR TYPES
121  
122 =head2 new - Constructor
123  
124 For each method name passed, returns a subroutine with the following characteristics:
125  
126 =over 4
127  
128 =item *
129  
130 If called as a class method, makes a new array and blesses it into that class.
131  
132 =item *
133  
134 If called on an array-based instance, makes a copy of it and blesses the copy into the same class as the original instance.
135  
136 =item *
137  
138 If passed a list of method-value pairs, calls each named method with the associated value as an argument.
139  
140 =item *
141  
142 Returns the new instance.
143  
144 =back
145  
146 Sample declaration and usage:
147  
148 package MyObject;
149 use Class::MakeMethods::Basic::Array (
150 new => 'new',
151 );
152 ...
153
154 # Bare constructor
155 my $empty = MyObject->new();
156
157 # Constructor with initial sequence of method calls
158 my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
159
160 # Copy with overriding sequence of method calls
161 my $copy = $obj->new( bar => 'Bob' );
162  
163 =cut
164  
165 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
166 1 my $class = shift;
167 map {
168 1 my $name = $_;
169 4 ▶ $name => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
170 4 my $callee = shift;
171 4 ▶ my $self = ref($callee) ? bless( [@$callee], ref($callee) )
Branch: 1 branch
BranchTrueFalseTotal
ref $callee ? : 1 3 4
172 : bless( [], $callee );
173 4 ▶ while ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 6 4 10
174 6 my $method = shift;
175 6 $self->$method( shift );
176 }
177 4 return $self;
178 }
179 1 } @_;
180 }
181  
182 ########################################################################
183  
184 =head2 scalar - Instance Accessor
185  
186 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
187  
188 =over 4
189  
190 =item *
191  
192 Must be called on an array-based instance.
193  
194 =item *
195  
196 Determines the array position associated with the method name, and uses that as an index into each instance to access the related value.
197  
198 =item *
199  
200 If called without any arguments returns the current value (or undef).
201  
202 =item *
203  
204 If called with an argument, stores that as the value, and returns it,
205  
206 =back
207  
208 Sample declaration and usage:
209  
210 package MyObject;
211 use Class::MakeMethods::Basic::Array (
212 scalar => 'foo',
213 );
214 ...
215
216 # Store value
217 $obj->foo('Foozle');
218
219 # Retrieve value
220 print $obj->foo;
221  
222 =cut
223  
224 2 ▶sub scalar {
Subroutine scalar: called CC=1 SCAR=0.0
Pod documented
225 2 my $class = shift;
226 map {
227 2 my $name = $_;
228 3 my $index = _array_index( $class->_context('TargetClass'), $name );
229 21 ▶ $name => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
230 21 my $self = shift;
231 21 ▶ if ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 8 13 21
232 8 $self->[$index] = shift;
233 } else {
234 13 $self->[$index];
235 }
236 }
237 3 } @_;
238 }
239  
240 ########################################################################
241  
242 =head2 array - Instance Ref Accessor
243  
244 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
245  
246 =over 4
247  
248 =item *
249  
250 Must be called on an array-based instance.
251  
252 =item *
253  
254 Determines the array position associated with the method name, and uses that as an index into each instance to access the related value.
255  
256 =item *
257  
258 The value for each instance will be a reference to an array (or undef).
259  
260 =item *
261  
262 If called without any arguments, returns the current array-ref value (or undef).
263  
264 =item *
265  
266 If called with one argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced array is returned.
267  
268 =item *
269  
270 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.
271  
272 =back
273  
274 Sample declaration and usage:
275  
276 package MyObject;
277 use Class::MakeMethods::Basic::Array (
278 array => 'bar',
279 );
280 ...
281
282 # Set values by position
283 $obj->bar(0 => 'Foozle', 1 => 'Bang!');
284
285 # Positions may be overwritten, and in any order
286 $obj->bar(2 => 'And Mash', 1 => 'Blah!');
287
288 # Retrieve value by position
289 print $obj->bar(1);
290
291 # Retrieve slice of values by position
292 print join(', ', $obj->bar( [0, 2] ) );
293
294 # Direct access to referenced array
295 print scalar @{ $obj->bar() };
296
297 # Reset the array contents to empty
298 @{ $obj->bar() } = ();
299  
300 =cut
301  
302 0 ▶sub array {
Subroutine array: not called CC=1 SCAR=6.9
Pod documented
303 0 my $class = shift;
304 map {
305 0 my $name = $_;
306 0 my $index = _array_index( $class->_context('TargetClass'), $name );
307 0 ▶ $name => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
308 0 my $self = shift;
309 0 ▶ if ( scalar(@_) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 0 0
elsif (scalar @_ == 1) { } 0 0 0
elsif (scalar(@_) % 2) { } 0 0 0
310 0 return $self->[$index];
311 } elsif ( scalar(@_) == 1 ) {
312 0 return $self->[$index]->[ shift() ];
313 } elsif ( scalar(@_) % 2 ) {
314 0 Carp::croak "Odd number of items in assigment to $name";
315 } else {
316 0 ▶ while ( scalar(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
317 0 my $k = shift();
318 0 $self->[$index]->[ $k ] = shift();
319 }
320 0 return $self->[$index];
321 }
322 }
323 0 } @_;
324 }
325  
326 ########################################################################
327  
328 =head2 hash - Instance Ref Accessor
329  
330 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
331  
332 =over 4
333  
334 =item *
335  
336 Must be called on an array-based instance.
337  
338 =item *
339  
340 Determines the array position associated with the method name, and uses that as an index into each instance to access the related value.
341  
342 =item *
343  
344 The value for each instance will be a reference to a hash (or undef).
345  
346 =item *
347  
348 If called without any arguments, returns the current hash-ref value (or undef).
349  
350 =item *
351  
352 If called with one argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the single argument is an array ref, then a slice of the referenced hash is returned.
353  
354 =item *
355  
356 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.
357  
358 =back
359  
360 Sample declaration and usage:
361  
362 package MyObject;
363 use Class::MakeMethods::Basic::Array (
364 hash => 'baz',
365 );
366 ...
367
368 # Set values by key
369 $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
370
371 # Values may be overwritten, and in any order
372 $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
373
374 # Retrieve value by key
375 print $obj->baz('foo');
376
377 # Retrieve slice of values by position
378 print join(', ', $obj->baz( ['foo', 'bar'] ) );
379
380 # Direct access to referenced hash
381 print keys %{ $obj->baz() };
382
383 # Reset the hash contents to empty
384 @{ $obj->baz() } = ();
385  
386 =cut
387  
388 0 ▶sub hash {
Subroutine hash: not called CC=1 SCAR=6.9
Pod documented
389 0 my $class = shift;
390 map {
391 0 my $name = $_;
392 0 my $index = _array_index( $class->_context('TargetClass'), $name );
393 0 ▶ $name => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
394 0 my $self = shift;
395 0 ▶ if ( scalar(@_) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 0 0
elsif (scalar @_ == 1) { } 0 0 0
elsif (scalar(@_) % 2) { } 0 0 0
396 0 return $self->[$index];
397 } elsif ( scalar(@_) == 1 ) {
398 0 return $self->[$index]->{ shift() };
399 } elsif ( scalar(@_) % 2 ) {
400 0 Carp::croak "Odd number of items in assigment to $name";
401 } else {
402 0 ▶ while ( scalar(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
403 0 my $k = shift();
404 0 $self->[$index]->{ $k } = shift();
405 }
406 0 return $self->[$index];
407 }
408 }
409 0 } @_;
410 }
411  
412 ########################################################################
413  
414 =head1 SEE ALSO
415  
416 See L<Class::MakeMethods> for general information about this distribution.
417  
418 See L<Class::MakeMethods::Basic> for more about this family of subclasses.
419  
420 =cut
421  
422 1 1;
« blib/lib/Class/MakeMethods/Emulator/MethodMaker.pm ↑ Summary blib/lib/Class/MakeMethods/Template/Ref.pm »