blib/lib/perl5i/1/ARRAY.pm

filter: Statementstmt 16.0% 26 / 162 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 44 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 16.6% 5 / 30 Podpod 0.0% 0 / 12 totaltotal 8.2% 31 / 374 SCAR 82.8
CC
71
Cov
8%
__ANON__
506.0
__ANON__
182.0
__ANON__
72.0
CRAP 3933.1
↑ Summary blib/lib/perl5i/0/ARRAY.pm »
1 # vi: set ts=4 sw=4 ht=4 et :
2 package perl5i::1::ARRAY;
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶no if $] >= 5.018000, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use perl5i::1::autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub first {
12 0 ▶ my ( $array, $filter ) = @_;
Subroutine first: not called CC=2 SCAR=17.9
Pod undocumented
13  
14 # Deep recursion and segfault (lines 90 and 91 in first.t) if we use
15 # the same elegant approach as in grep().
16 0 ▶ if ( ref $filter eq 'Regexp' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter eq "Regexp") 0 0 0
17 0 ▶ return List::Util::first( sub { $_ ~~ $filter }, @$array );
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 }
19  
20 0 ▶ return List::Util::first( sub { $filter->() }, @$array );
Subroutine __ANON__: not called CC=1 SCAR=6.9
21  
22 }
23  
24 sub grep {
25 0 ▶ my ( $array, $filter ) = @_;
Subroutine grep: not called CC=2 SCAR=17.9
Pod undocumented
26  
27 0 my @result = CORE::grep { $_ ~~ $filter } @$array;
28  
29 0 ▶ return wantarray ? @result : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
30 }
31  
32 sub all {
33 0 ▶ require List::MoreUtils;
Subroutine all: not called CC=1 SCAR=6.9
Pod undocumented
34 0 return List::MoreUtils::all($_[1], @{$_[0]});
35 }
36  
37 sub any {
38 0 ▶ require List::MoreUtils;
Subroutine any: not called CC=1 SCAR=6.9
Pod undocumented
39 0 return List::MoreUtils::any($_[1], @{$_[0]});
40 }
41  
42 sub none {
43 0 ▶ require List::MoreUtils;
Subroutine none: not called CC=1 SCAR=6.9
Pod undocumented
44 0 return List::MoreUtils::none($_[1], @{$_[0]});
45 }
46  
47 sub true {
48 0 ▶ require List::MoreUtils;
Subroutine true: not called CC=1 SCAR=6.9
Pod undocumented
49 0 return List::MoreUtils::true($_[1], @{$_[0]});
50 }
51  
52 sub false {
53 0 ▶ require List::MoreUtils;
Subroutine false: not called CC=1 SCAR=6.9
Pod undocumented
54 0 return List::MoreUtils::false($_[1], @{$_[0]});
55 }
56  
57 sub uniq {
58 0 ▶ require List::MoreUtils;
Subroutine uniq: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my @uniq = List::MoreUtils::uniq(@{$_[0]});
60 0 ▶ return wantarray ? @uniq : \@uniq;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
61 }
62  
63 sub minmax {
64 0 ▶ require List::MoreUtils;
Subroutine minmax: not called CC=2 SCAR=17.9
Pod undocumented
65 0 my @minmax = List::MoreUtils::minmax(@{$_[0]});
66 0 ▶ return wantarray ? @minmax : \@minmax;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
67 }
68  
69 sub mesh {
70 0 ▶ require List::MoreUtils;
Subroutine mesh: not called CC=2 SCAR=17.9
Pod undocumented
71 0 my @mesh = List::MoreUtils::zip(@_);
72 0 ▶ return wantarray ? @mesh : \@mesh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
73 }
74  
75  
76 # Returns the code which will run when the object is used as a string
77 1 require overload;
78 my $overload_type = sub {
79 0 ▶ return unless ref $_[0];
Subroutine __ANON__: not called CC=8 SCAR=42.8
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
80 0 my $str = overload::Method($_[0], q[""]);
81 0 my $num = overload::Method($_[0], "0+");
82 0 ▶ return "both" if $str and $num;
Branch: 1 branch
BranchTrueFalseTotal
if $str and $num 0 0 0
Condition0%0/3
$str and $num
!ll&&!rl&&r
000
MC/DC0%0/2
$str and $num
$str $num
Truth table0%0/3
A and B
A= $strB= $num
ABresult
0X0
100
111
83 0 ▶ return "" if !$str and !$num;
Branch: 1 branch
BranchTrueFalseTotal
unless $str or $num 0 0 0
Condition0%0/3
$str or $num
l!l&&r!l&&!r
000
MC/DC0%0/2
$str or $num
$str $num
Truth table0%0/3
A or B
A= $strB= $num
ABresult
1X1
011
000
84 0 ▶ return "str" if $str;
Branch: 1 branch
BranchTrueFalseTotal
if $str 0 0 0
85 0 ▶ return "num" if $num;
Branch: 1 branch
BranchTrueFalseTotal
if $num 0 0 0
86 1 };
87  
88 1 my $are_equal;
89  
90 # Two objects, possibly different classes, both overloaded.
91 my $equal_overload = sub {
92 0 ▶ my($obj1, $obj2) = @_;
Subroutine __ANON__: not called CC=13 SCAR=52.0
93  
94 0 my $type1 = $overload_type->($obj1);
95 0 my $type2 = $overload_type->($obj2);
96  
97 # One of them is not overloaded
98 0 ▶ return if !$type1 or !$type2;
Branch: 1 branch
BranchTrueFalseTotal
unless $type1 and $type2 0 0 0
Condition0%0/3
$type1 and $type2
!ll&&!rl&&r
000
MC/DC0%0/2
$type1 and $type2
$type1 $type2
Truth table0%0/3
A and B
A= $type1B= $type2
ABresult
0X0
100
111
99  
100 0 ▶ if( $type1 eq 'both' and $type2 eq 'both' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type1 eq 'both' and $type2 eq 'both') { } 0 0 0
elsif ($type1 eq 'num' and $type2 eq 'str' or $type1 eq 'str' and $type2 eq 'num') { } 0 0 0
elsif ('num' ~~ [$type1, $type2]) { } 0 0 0
elsif ('str' ~~ [$type1, $type2]) { } 0 0 0
Condition0%0/3
$type1 eq "both" and $type2 eq "both"
!ll&&!rl&&r
000
MC/DC0%0/2
$type1 eq "both" and $type2 eq "both"
$type1 eq "both" $type2 eq "both"
Truth table0%0/3
A and B
A= $type1 eq "both"B= $type2 eq "both"
ABresult
0X0
100
111
101 0 ▶ return $obj1 == $obj2 || $obj1 eq $obj2;
Condition0%0/3
$obj1 == $obj2 || $obj1 eq $obj2
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj1 == $obj2 || $obj1 eq $obj2
$obj1 == $obj2 $obj1 eq $obj2
Truth table0%0/3
A || B
A= $obj1 == $obj2B= $obj1 eq $obj2
ABresult
1X1
011
000
102 }
103 ▶ elsif(
Condition0%0/9
$type1 eq "num" and $type2 eq "str" or $type1 eq "str" and $type2 eq "num"
l!l&&r!l&&!r
000
$type1 eq "num" and $type2 eq "str"
!ll&&!rl&&r
000
$type1 eq "str" and $type2 eq "num"
!ll&&!rl&&r
000
MC/DC0%0/4
$type1 eq "num" and $type2 eq "str" or $type1 eq "str" and $type2 eq "num"
$type1 eq "num" $type2 eq "str" $type1 eq "str" $type2 eq "num"
Truth table0%0/7
A and B or C and D
A= $type1 eq "num"B= $type2 eq "str"C= $type1 eq "str"D= $type2 eq "num"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
104 ($type1 eq 'num' and $type2 eq 'str') or
105 ($type1 eq 'str' and $type2 eq 'num')
106 )
107 {
108 # They're not both numbers, not both strings, and not both both
109 # Must be str vs num.
110 0 ▶ return $type1 eq 'num' ? $obj1+0 eq "$obj2"
Branch: 1 branch
BranchTrueFalseTotal
$type1 eq 'num' ? : 0 0 0
111 : $obj2+0 eq "$obj1";
112 }
113 elsif( 'num' ~~ [$type1, $type2] ) {
114 0 return $obj1 == $obj2;
115 }
116 elsif( 'str' ~~ [$type1, $type2] ) {
117 0 return $obj1 eq $obj2;
118 }
119 else {
120 0 die "Should never be reached";
121 }
122 1 };
123  
124  
125 # Two objects, same class
126 my $equal_objects = sub {
127 0 ▶ my($r1, $r2) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
128  
129 # No need to check both, they're the same class
130 0 my $is_overloaded = overload::Overloaded($r1);
131  
132 0 ▶ if( !$is_overloaded ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_overloaded) { } 0 0 0
133 # Neither are overloaded, they're the same class, are they the same object?
134 0 return $r1 eq $r2;
135 }
136 else {
137 0 return $equal_overload->( $r1, $r2 );
138 }
139 1 };
140  
141  
142 # One overloaded object, one plain scalar
143 # STRING != OBJ
144 # NUMBER != OBJ
145 # STRING eq OBJeq
146 # STRING eq OBJboth
147 # STRING != OBJ== (using == will throw a warning)
148 # NUMBER == OBJ==
149 # NUMBER eq OBJeq
150 # NUMBER == OBJboth
151 my $equal_overload_vs_scalar = sub {
152 0 ▶ my($obj, $scalar) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
153  
154 0 my $type = $overload_type->($obj);
155 0 ▶ return unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
156  
157 0 ▶ if( $scalar->is_number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scalar->is_number) { } 0 0 0
158 0 ▶ if( $type eq 'str' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'str') { } 0 0 0
159 0 $obj eq $scalar;
160 }
161 else {
162 0 $obj == $scalar;
163 }
164 }
165 else {
166 0 ▶ if( $type eq 'num' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'num') { } 0 0 0
167 # Can't reliably compare
168 0 return;
169 }
170 else {
171 0 $obj eq $scalar;
172 }
173 }
174 1 };
175  
176 my $equal_arrays = sub {
177 0 ▶ my ($r1, $r2) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
178 # They can only be equal if they have the same nº of elements.
179 0 ▶ return if @$r1 != @$r2;
Branch: 1 branch
BranchTrueFalseTotal
if @$r1 != @$r2 0 0 0
180  
181 0 foreach my $i (0 .. @$r1 - 1) {
182 0 ▶ return unless $are_equal->($r1->[$i], $r2->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
unless &$are_equal($r1->[$i], $r2->[$i]) 0 0 0
183 }
184  
185 0 return 1;
186 1 };
187  
188 my $equal_hashes = sub {
189 0 ▶ my ($r1, $r2) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
190 # Hashes can't be equal unless their keys are equal.
191 0 ▶ return unless ( %$r1 ~~ %$r2 );
Branch: 1 branch
BranchTrueFalseTotal
unless \%$r1 ~~ \%$r2 0 0 0
192  
193 # Compare the equality of the values for each key.
194 0 foreach my $key (keys %$r1) {
195 0 ▶ return unless $are_equal->( $r1->{$key}, $r2->{$key} );
Branch: 1 branch
BranchTrueFalseTotal
unless &$are_equal($r1->{$key}, $r2->{$key}) 0 0 0
196 }
197  
198 0 return 1;
199 1 };
200  
201  
202 $are_equal = sub {
203 0 ▶ my ($r1, $r2) = @_;
Subroutine __ANON__: not called CC=22 SCAR=62.3
204  
205 # given two scalars, decide whether they are identical or not,
206 # recursing over deep data structures. Since it uses recursion,
207 # traversal is done depth-first.
208 # Warning: complex if-then-else decision tree ahead. It's ordered on
209 # my perceived and anecdotical take on the frequency of occurrence
210 # of each reftype: most popular on top, most rare on the bottom.
211 # This way we return as early as possible.
212  
213 # undef eq undef
214 0 ▶ return 1 if !defined $r1 and !defined $r2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r1 or defined $r2 0 0 0
Condition0%0/3
defined $r1 or defined $r2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $r1 or defined $r2
defined $r1 defined $r2
Truth table0%0/3
A or B
A= defined $r1B= defined $r2
ABresult
1X1
011
000
215  
216 # One is defined, one isn't
217 0 ▶ return if defined $r1 xor defined $r2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $r1 xor defined $r2 0 0 0
Condition0%0/4
defined $r1 xor defined $r2
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
defined $r1 xor defined $r2
defined $r1 defined $r2
Truth table0%0/4
A xor B
A= defined $r1B= defined $r2
ABresult
110
101
011
000
218  
219 0 my( $ref1, $ref2 ) = (ref $r1, ref $r2);
220  
221 0 ▶ if( !$ref1 and !$ref2 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref1 || $ref2) { } 0 0 0
elsif ($ref1 eq $ref2) { } 0 0 0
elsif ($ref1 and $ref2) { } 0 0 0
Condition0%0/3
$ref1 || $ref2
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref1 || $ref2
$ref1 $ref2
Truth table0%0/3
A || B
A= $ref1B= $ref2
ABresult
1X1
011
000
222 0 my $is_num1 = $r1->is_number;
223 0 my $is_num2 = $r2->is_number;
224 0 ▶ if( $is_num1 xor $is_num2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_num1 xor $is_num2) { } 0 0 0
elsif ($is_num1) { } 0 0 0
Condition0%0/4
$is_num1 xor $is_num2
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$is_num1 xor $is_num2
$is_num1 $is_num2
Truth table0%0/4
A xor B
A= $is_num1B= $is_num2
ABresult
110
101
011
000
225 # One's looks like a number, the other doesn't.
226 # Can't be equal.
227 0 return 0;
228 }
229 elsif( $is_num1 ) {
230 # They're both numbers
231 0 return $r1 == $r2;
232 }
233 else {
234 # They're both strings
235 0 return $r1 eq $r2;
236 }
237 }
238 elsif( $ref1 eq $ref2 ) {
239 0 ▶ if ( $ref1 ~~ [qw(Regexp GLOB CODE)] ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref1 ~~ ['Regexp', 'GLOB', 'CODE']) { } 0 0 0
elsif ($ref1 eq 'ARRAY') { } 0 0 0
elsif ($ref1 eq 'HASH') { } 0 0 0
elsif ($ref1 ~~ ['SCALAR', 'REF']) { } 0 0 0
240 0 return $r1 eq $r2;
241 }
242 elsif ( $ref1 eq 'ARRAY' ) {
243 0 return $equal_arrays->( $r1, $r2 );
244 }
245 elsif ( $ref1 eq 'HASH' ) {
246 0 return $equal_hashes->( $r1, $r2 );
247 }
248 elsif ( $ref1 ~~ [qw(SCALAR REF)] ) {
249 0 return $are_equal->($$r1, $$r2);
250 }
251 else {
252 # Must be an object
253 0 return $equal_objects->( $r1, $r2 );
254 }
255 }
256 ▶ elsif( $ref1 and $ref2 ) {
Condition0%0/3
$ref1 and $ref2
!ll&&!rl&&r
000
MC/DC0%0/2
$ref1 and $ref2
$ref1 $ref2
Truth table0%0/3
A and B
A= $ref1B= $ref2
ABresult
0X0
100
111
257 # They're both refs, but not of the same type
258 0 my $is_overloaded1 = overload::Overloaded($r1);
259 0 my $is_overloaded2 = overload::Overloaded($r2);
260  
261 0 ▶ if( $is_overloaded1 and $is_overloaded2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_overloaded1 and $is_overloaded2) { } 0 0 0
Condition0%0/3
$is_overloaded1 and $is_overloaded2
!ll&&!rl&&r
000
MC/DC0%0/2
$is_overloaded1 and $is_overloaded2
$is_overloaded1 $is_overloaded2
Truth table0%0/3
A and B
A= $is_overloaded1B= $is_overloaded2
ABresult
0X0
100
111
262 # Two overloaded objects
263 0 return $equal_overload->( $r1, $r2 );
264 }
265 else {
266 # One's an overloaded object, the other is not or
267 # Two plain refs different type or
268 # non-overloaded objects of different type.
269 0 return 0;
270 }
271 }
272 else {
273 # One is a ref, one is not
274 0 ▶ my $is_overloaded = $ref1 ? overload::Overloaded($r1)
Branch: 1 branch
BranchTrueFalseTotal
$ref1 ? : 0 0 0
275 : overload::Overloaded($r2);
276  
277 0 ▶ if( $is_overloaded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_overloaded) { } 0 0 0
278 # One's an overloaded object, one's a plain scalar
279 0 ▶ return $ref1 ? $equal_overload_vs_scalar->($r1, $r2)
Branch: 1 branch
BranchTrueFalseTotal
$ref1 ? : 0 0 0
280 : $equal_overload_vs_scalar->($r2, $r1);
281 }
282 else {
283 # One's a plain ref or object, one's a plain scalar
284 0 return 0;
285 }
286 }
287 1 };
288  
289  
290 my $diff_two = sub {
291 # Compare differences between two arrays.
292 0 ▶ my ($c, $d) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
293  
294 0 my $diff = [];
295  
296 # For each element of $c, try to find if it is equal to any of the
297 # elements of $d. If not, it's unique, and has to be pushed into
298 # $diff.
299  
300 0 require List::MoreUtils;
301 0 foreach my $item (@$c) {
302 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (List::MoreUtils::any(sub { &$are_equal($item, $_); } , @$d)) 0 0 0
303 0 ▶ List::MoreUtils::any( sub { $are_equal->( $item, $_ ) }, @$d )
Subroutine __ANON__: not called CC=1 SCAR=6.9
304 )
305 {
306 0 push @$diff, $item;
307 }
308 }
309  
310 0 return $diff;
311 1 };
312  
313  
314 sub diff {
315 0 ▶ my ($base, @rest) = @_;
Subroutine diff: not called CC=6 SCAR=37.4
Pod undocumented
316 0 ▶ unless (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rest) 0 0 0
317 0 ▶ return wantarray ? @$base : $base;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
318 }
319  
320 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
321 # ssasign ... "
322 0 ▶ die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @rest 0 0 0
323  
324 0 foreach my $array (@rest) {
325 0 $base = $diff_two->($base, $array);
326 }
327  
328 0 ▶ return wantarray ? @$base : $base;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
329 }
330  
331  
332 my $intersect_two = sub {
333 # Compare differences between two arrays.
334 0 ▶ my ($c, $d) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
335  
336 0 my $intersect = [];
337  
338 # For each element of $c, try to find if it is equal to any of the
339 # elements of $d. If it is, it's shared, and has to be pushed into
340 # $intersect.
341  
342 0 require List::MoreUtils;
343 0 foreach my $item (@$c) {
344 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (List::MoreUtils::any(sub { &$are_equal($item, $_); } , @$d)) 0 0 0
345 0 ▶ List::MoreUtils::any( sub { $are_equal->( $item, $_ ) }, @$d )
Subroutine __ANON__: not called CC=1 SCAR=6.9
346 )
347 {
348 0 push @$intersect, $item;
349 }
350 }
351  
352 0 return $intersect;
353 1 };
354  
355 sub intersect {
356 0 ▶ my ($base, @rest) = @_;
Subroutine intersect: not called CC=6 SCAR=37.4
Pod undocumented
357  
358 0 ▶ unless (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@rest) 0 0 0
359 0 ▶ return wantarray ? @$base : $base;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
360 }
361  
362 # XXX If I use carp here, the exception is "bizarre copy of ARRAY in
363 # ssasign ... "
364 0 ▶ die "Arguments must be array references" if grep { ref $_ ne 'ARRAY' } @rest;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @rest 0 0 0
365  
366 0 foreach my $array (@rest) {
367 0 $base = $intersect_two->($base, $array);
368 }
369  
370 0 ▶ return wantarray ? @$base : $base;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
371 }
372  
373  
374 1 1;
↑ Summary blib/lib/perl5i/0/ARRAY.pm »