blib/lib/Class/MakeMethods/Utility/ArraySplicer.pm

filter: Statementstmt 57.3% 35 / 61 Branchbran 22.2% 8 / 36 Conditioncond 30.3% 10 / 33 MC/DCmcdc 5.5% 1 / 18 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 40.5% 64 / 158 CC 30 SCAR 56.4
CC
30
Cov
41%
array_splicer
366.9
import
2.1
BEGIN
1.0
CRAP 280.3
« blib/lib/Class/MakeMethods/Utility/Ref.pm ↑ Summary blib/lib/Class/MakeMethods/Standard/Global.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Utility::ArraySplicer - Common array ops
4  
5 =head1 SYNOPSIS
6  
7 use Class::MakeMethods::Utility::ArraySplicer;
8
9 # Get one or more values
10 $value = array_splicer( $array_ref, $index );
11 @values = array_splicer( $array_ref, $index_array_ref );
12
13 # Set one or more values
14 array_splicer( $array_ref, $index => $new_value, ... );
15
16 # Splice selected values in or out
17 array_splicer( $array_ref, [ $start_index, $end_index], [ @values ]);
18  
19 =head1 DESCRIPTION
20  
21 This module provides a utility function and several associated constants which support a general purpose array-splicer interface, used by several of the Standard and Composite method generators.
22  
23 =cut
24  
25 ########################################################################
26  
27 package Class::MakeMethods::Utility::ArraySplicer;
28  
29 10 $VERSION = 1.000;
30  
31 10 @EXPORT_OK = qw(
32 array_splicer
33 array_set array_clear array_push array_pop array_unshift array_shift
34 );
35 13 ▶sub import { require Exporter and goto &Exporter::import } # lazy Exporter
Subroutine import: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
require Exporter and goto \&Exporter::import 13 0 13
36  
37 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 ########################################################################
40  
41 =head2 array_splicer
42  
43 This is a general-purpose array accessor function. Depending on the arguments passed to it, it will get, set, slice, splice, or otherwise modify your array.
44  
45 =over 4
46  
47 =item *
48  
49 If called without any arguments, returns the contents of the array in list context, or an array reference in scalar context (or undef).
50  
51 # Get all values
52 $value_ref = array_splicer( $array_ref );
53 @values = array_splicer( $array_ref );
54  
55 =item *
56  
57 If called with a single numeric argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef).
58  
59 # Get one value
60 $value = array_splicer( $array_ref, $index );
61  
62 =item *
63  
64 If called with a single array ref argument, sets the contents of the array to match the contents of the provided one.
65  
66 # Set contents of array
67 array_splicer( $array_ref, [ $value1, $value2, ... ] );
68  
69 # Reset the array contents to empty
70 array_splicer( $array_ref, [] );
71  
72 =item *
73  
74 If called with a two arguments, the first undefined and the second an array ref argument, uses that array's contents as a list of indexes to return a slice of the referenced array.
75  
76 # Get slice of values
77 @values = array_splicer( $array_ref, undef, [ $index1, $index2, ... ] );
78  
79 =item *
80  
81 If called with a list of argument pairs, each with a numeric index and an associated value, stores the value at the given index in the referenced array. 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.
82  
83 # Set one or more values by index
84 array_splicer( $array_ref, $index1 => $value1, $index2 => $value2, ... );
85  
86 =item *
87  
88 If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array.
89  
90 # Splice selected values in or out
91 array_splicer( $array_ref, [ $start_index, $count], [ @values ]);
92  
93 The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array.
94  
95 The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned.
96  
97 If both numbers are omitted, or are both undefined, they default to containing the entire value array.
98  
99 If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied.
100  
101 The method returns the items that removed from the array, if any.
102  
103 Here are some examples of common splicing operations.
104  
105 # Insert an item at position in the array
106 $obj->bar([3], 'Potatoes' );
107
108 # Remove 1 item from position 3 in the array
109 $obj->bar([3, 1], undef );
110
111 # Set a new value at position 2, and return the old value
112 print $obj->bar([2, 1], 'Froth' );
113  
114 # Unshift an item onto the front of the list
115 array_splicer( $array_ref, [0], 'Bubbles' );
116  
117 # Shift the first item off of the front of the list
118 print array_splicer( $array_ref, [0, 1], undef );
119  
120 # Push an item onto the end of the list
121 array_splicer( $array_ref, [undef], 'Bubbles' );
122  
123 # Pop the last item off of the end of the list
124 print array_splicer( $array_ref, [undef, 1], undef );
125  
126 =back
127  
128 =cut
129  
130 8 ▶sub array_splicer {
Subroutine array_splicer: called CC=29 SCAR=59.0
Pod documented
131 8 my $value_ref = shift;
132
133 # RETRIEVE VALUES
134 8 ▶ if ( scalar(@_) == 0 ) {
Branch: 6 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 8 8
elsif (scalar @_ == 1 and length $_[0] and not ref $_[0] and not $_[0] =~ /\D/) { } 5 3 8
elsif (scalar @_ == 1 and ref $_[0] eq 'ARRAY') { } 0 3 3
elsif (not scalar(@_) % 2 || grep({not length $_ && !ref($_) && !($_ =~ /\D/);} map({$_[$_];} grep({not $_ % 2;} 0 .. $#_)))) { } 3 0 3
elsif (!scalar(@_) == 2 and not defined $_[0] and ref $_[1] eq 'ARRAY') { } 0 0 0
elsif (!scalar(@_) % 2 and ref $_[0] eq 'ARRAY') { } 0 0 0
135 0 ▶ return wantarray ? @$value_ref : $value_ref;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
136
137 # FETCH BY INDEX
138 ▶ } elsif ( scalar(@_) == 1 and length($_[0]) and ! ref($_[0]) and $_[0] !~ /\D/) {
Condition66%6/9
scalar @_ == 1 and length $_[0] and not ref $_[0] and not $_[0] =~ /\D/
!ll&&!rl&&r
305
scalar @_ == 1 and length $_[0] and not ref $_[0]
!ll&&!rl&&r
305
scalar @_ == 1 and length $_[0]
!ll&&!rl&&r
305
MC/DC25%1/4
scalar @_ == 1 and length $_[0] and not ref $_[0] and not $_[0] =~ /\D/
scalar @_ == 1 length $_[0] not ref $_[0] not $_[0] =~ /\D/
Truth table40%2/5
A and B and not(C) and not(D)
A= scalar @_ == 1B= length $_[0]C= not ref $_[0]D= not $_[0] =~ /\D/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
139 5 $value_ref->[ $_[0] ]
140
141 # SET CONTENTS
142 ▶ } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) {
Condition33%1/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
300
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
143 0 @$value_ref = @{ $_[0] };
144 0 ▶ return wantarray ? @$value_ref : $value_ref;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
145
146 # ASSIGN BY INDEX
147 3 ▶ } elsif ( ! ( scalar(@_) % 2 ) and ! grep { ! ( length($_) and ! ref($_) and $_ !~ /\D/ ) } map { $_[$_] } grep { ! ( $_ % 2 ) } ( 0 .. $#_ ) ) {
Condition33%3/9
scalar(@_) % 2 || grep({not length $_ && !ref($_) && !($_ =~ /\D/);} map({$_[$_];} grep({not $_ % 2;} 0 .. $#_)))
l!l&&r!l&&!r
003
length $_ && !ref($_) && !($_ =~ /\D/)
!ll&&!rl&&r
003
length $_ and not ref $_
!ll&&!rl&&r
003
MC/DC0%0/2
scalar(@_) % 2 || grep({not length $_ && !ref($_) && !($_ =~ /\D/);} map({$_[$_];} grep({not $_ % 2;} 0 .. $#_)))
scalar(@_) % 2 grep({not length $_ && !ref($_) && !($_ =~ /\D/);} map({$_[$_];} grep({not $_ % 2;} 0 .. $#_)))
MC/DC0%0/3
length $_ && !ref($_) && !($_ =~ /\D/)
length $_ not ref $_ !($_ =~ /\D/)
Truth table33%1/3
A || B
A= scalar(@_) % 2B= grep({not length $_ && !ref($_) && !($_ =~ /\D/);} map({$_[$_];} grep({not $_ % 2;} 0 .. $#_)))
ABresult
1X1
011
000
Truth table25%1/4
A and not(B) && not(C)
A= length $_B= not ref $_C= !($_ =~ /\D/)
ABCresult
0XX0
10X0
1100
1111
148 3 ▶ while ( scalar(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 3 3 6
149 3 my $key = shift();
150 3 $value_ref->[ $key ] = shift();
151 }
152 3 $value_ref;
153  
154 # SLICE
155 ▶ } elsif ( ! scalar(@_) == 2 and ! defined $_[0] and ref $_[1] eq 'ARRAY' ) {
Condition0%0/6
!scalar(@_) == 2 and not defined $_[0] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
000
!scalar(@_) == 2 and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
!scalar(@_) == 2 and not defined $_[0] and ref $_[1] eq "ARRAY"
!scalar(@_) == 2 not defined $_[0] ref $_[1] eq "ARRAY"
Truth table0%0/4
not(A) and not(B) and C
A= !scalar(@_) == 2B= not defined $_[0]C= ref $_[1] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
156 0 @{$value_ref}[ @{ $_[1] } ]
157
158 # SPLICE
159 ▶ } elsif ( ! scalar(@_) % 2 and ref $_[0] eq 'ARRAY' ) {
Condition0%0/3
!scalar(@_) % 2 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
!scalar(@_) % 2 and ref $_[0] eq "ARRAY"
!scalar(@_) % 2 ref $_[0] eq "ARRAY"
Truth table0%0/3
not(A) and B
A= !scalar(@_) % 2B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
160 0 my @results;
161 0 ▶ while ( scalar(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
162 0 my $key = shift();
163 0 my $value = shift();
164 0 ▶ my @values = ! ( $value ) ? () : ! ref ( $value ) ? $value : @$value;
Branch: 2 branches
BranchTrueFalseTotal
!$value ? : 0 0 0
!ref($value) ? : 0 0 0
165 0 my $key_v = $key->[0];
166 0 my $key_c = $key->[1];
167 0 ▶ if ( defined $key_v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key_v) { } 0 0 0
168 0 ▶ if ( $key_c ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key_c) { } 0 0 0
169 # straightforward two-value splice
170 } else {
171 # insert at position
172 0 $key_c = 0;
173 }
174 } else {
175 0 ▶ if ( ! defined $key_c ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $key_c) { } 0 0 0
elsif ($key_c) { } 0 0 0
176 # target the entire list
177 0 $key_v = 0;
178 0 $key_c = scalar @$value_ref;
179 } elsif ( $key_c ) {
180 # take count items off the end
181 0 $key_v = - $key_c
182 } else {
183 # insert at the end
184 0 $key_v = scalar @$value_ref;
185 0 $key_c = 0;
186 }
187 }
188 0 push @results, splice @$value_ref, $key_v, $key_c, @values
189 }
190 0 ▶ ( ! wantarray and scalar @results == 1 ) ? $results[0] : @results;
Branch: 1 branch
BranchTrueFalseTotal
!wantarray && scalar @results == 1 ? : 0 0 0
Condition0%0/3
not wantarray and scalar @results == 1
!ll&&!rl&&r
000
MC/DC0%0/2
not wantarray and scalar @results == 1
not wantarray scalar @results == 1
Truth table0%0/3
not(A) and B
A= not wantarrayB= scalar @results == 1
ABresult
0X0
100
111
191
192 } else {
193 0 Carp::confess 'Unexpected arguments to array accessor: ' . join(', ', map "'$_'", @_ );
194 }
195 }
196  
197 ########################################################################
198  
199 =head2 Constants
200  
201 There are also constants symbols to facilitate some common combinations of splicing arguments:
202  
203 # Reset the array contents to empty
204 array_splicer( $array_ref, array_clear );
205
206 # Set the array contents to provided values
207 array_splicer( $array_ref, array_splice, [ 2, 3 ] );
208
209 # Unshift an item onto the front of the list
210 array_splicer( $array_ref, array_unshift, 'Bubbles' );
211
212 # Shift it back off again
213 print array_splicer( $array_ref, array_shift );
214
215 # Push an item onto the end of the list
216 array_splicer( $array_ref, array_push, 'Bubbles' );
217
218 # Pop it back off again
219 print array_splicer( $array_ref, array_pop );
220  
221 =cut
222  
223 10 ▶use constant array_splice => undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 10 ▶use constant array_clear => ( [] );
Subroutine BEGIN: called CC=1 SCAR=0.0
225  
226 10 ▶use constant array_push => [undef];
Subroutine BEGIN: called CC=1 SCAR=0.0
227 10 ▶use constant array_pop => ( [undef, 1], undef );
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 10 ▶use constant array_unshift => [0];
Subroutine BEGIN: called CC=1 SCAR=0.0
230 10 ▶use constant array_shift => ( [0, 1], undef );
Subroutine BEGIN: called CC=1 SCAR=0.0
231  
232 ########################################################################
233  
234 =head1 SEE ALSO
235  
236 See L<Class::MakeMethods> for general information about this distribution.
237  
238 See L<Class::MakeMethods::Standard::Hash> and numerous other classes for
239 examples of usage.
240  
241 =cut
242  
243 10 1;
« blib/lib/Class/MakeMethods/Utility/Ref.pm ↑ Summary blib/lib/Class/MakeMethods/Standard/Global.pm »