blib/lib/autobox/Lookup.pm

filter: Statementstmt 28.9% 20 / 69 Branchbran 2.7% 1 / 36 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 38.4% 5 / 13 Podpod n/a totaltotal 18.4% 27 / 146 CC 25 SCAR 49.7
CC
25
Cov
18%
_recursive_lookup
306.0
__ANON__
12.0
values
12.0
CRAP 144.1
↑ Summary
1 package autobox::Lookup;
2  
3 # ABSTRACT: easy data lookup
4 5 { our $VERSION = '0.002'; }
5  
6 5 ▶use base qw(autobox);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶sub import {
Subroutine import: called CC=2 SCAR=7.1
9 5 my $class = shift;
10  
11 5 ▶ $autobox::Lookup::Subs::sep = shift() || '.';
Condition50%1/2
shift() || '.'
l!l
05
MC/DC0%0/1
shift() || '.'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
12  
13 5 $class->SUPER::import(
14 HASH => 'autobox::Lookup::Subs',
15 ARRAY => 'autobox::Lookup::Subs',
16 SCALAR => 'autobox::Lookup::Subs',
17 );
18 }
19  
20 package autobox::Lookup::Subs;
21  
22 5 ▶use List::Util qw/reduce/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 5 *_lookup = *_recursive_lookup;
26  
27 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=12.3
28 5 eval { require Mojo::Collection };
29 5 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 0 5 5
30 0 Mojo::Collection->import();
31 0 ▶ *collection = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
32 0 my $data = shift;
33 0 ▶ if (ref($data) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 0 0 0
elsif (ref $data eq 'HASH') { } 0 0 0
34 0 Mojo::Collection->new($data->@*)
35 }
36 elsif (ref($data) eq 'HASH') {
37 0 Mojo::Collection->new(values $data->%*)
38 }
39 0 };
40 } else {
41 0 ▶ *collection = sub { die "collection is not implemented unless Mojo::Collection is installed\n" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 5 }
43 }
44  
45 0 ▶sub _recursive_lookup {
Subroutine _recursive_lookup: not called CC=17 SCAR=57.2
46 0 my ($data, $key) = @_;
47  
48 0 ▶ return \undef unless defined $data && ref($data);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data and ref $data 0 0 0
Condition0%0/3
defined $data and ref $data
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data and ref $data
defined $data ref $data
Truth table0%0/3
A and B
A= defined $dataB= ref $data
ABresult
0X0
100
111
49 0 ▶ return \$data unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 0 0
50  
51 0 my ($first, @rest) = split /\./, $key;
52  
53 0 ▶ if ($key =~ /\,/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($key =~ /\,/) { } 0 0 0
elsif (ref $data eq 'ARRAY' and $first =~ /^\d+$/) { } 0 0 0
elsif (ref $data eq 'ARRAY' and $first eq '[]') { } 0 0 0
elsif (ref $data eq 'HASH' and $first eq '[]') { } 0 0 0
elsif (ref $data eq 'HASH' and exists $data->{$first}) { } 0 0 0
54 0 return do { my $r = [ map { ${_lookup($data, $_)} } split /\,/, $key ]; \$r }
55 }
56 ▶ elsif (ref($data) eq 'ARRAY' && $first =~ /^\d+$/) {
Condition0%0/3
ref $data eq "ARRAY" and $first =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data eq "ARRAY" and $first =~ /^\d+$/
ref $data eq "ARRAY" $first =~ /^\d+$/
Truth table0%0/3
A and B
A= ref $data eq "ARRAY"B= $first =~ /^\d+$/
ABresult
0X0
100
111
57 0 ▶ return @rest ? _lookup($data->[$first], join('.', @rest)) : \($data->[$first]);
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 0 0 0
58 }
59 ▶ elsif (ref($data) eq 'ARRAY' && $first eq '[]') {
Condition0%0/3
ref $data eq "ARRAY" and $first eq "[]"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data eq "ARRAY" and $first eq "[]"
ref $data eq "ARRAY" $first eq "[]"
Truth table0%0/3
A and B
A= ref $data eq "ARRAY"B= $first eq "[]"
ABresult
0X0
100
111
60 0 ▶ return @rest ? do { my $t = [ map { ${_lookup($_, join('.', @rest))} } @$data ]; \$t } : \$data;
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 0 0 0
61 }
62 ▶ elsif (ref($data) eq 'HASH' && $first eq '[]') {
Condition0%0/3
ref $data eq "HASH" and $first eq "[]"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data eq "HASH" and $first eq "[]"
ref $data eq "HASH" $first eq "[]"
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= $first eq "[]"
ABresult
0X0
100
111
63 return @rest ?
64 0 do { my $t = [ map { ${_lookup($data->{$_}, join('.', @rest))} } sort keys %$data ]; \$t } :
65 0 ▶ do { my $t = [ map { $data->{$_} } sort keys %$data ]; \$t; };
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 0 0 0
66 }
67 ▶ elsif (ref($data) eq 'HASH' && exists $data->{$first}) {
Condition0%0/3
ref $data eq "HASH" and exists $data->{$first}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $data eq "HASH" and exists $data->{$first}
ref $data eq "HASH" exists $data->{$first}
Truth table0%0/3
A and B
A= ref $data eq "HASH"B= exists $data->{$first}
ABresult
0X0
100
111
68 0 ▶ return @rest ? _lookup($data->{$first}, join('.', @rest)) : \($data->{$first});
Branch: 1 branch
BranchTrueFalseTotal
@rest ? : 0 0 0
69 }
70 0 return \undef;
71 }
72  
73 0 ▶sub lookup { shift()->get(@_) }
Subroutine lookup: not called CC=1 SCAR=6.9
74  
75 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
76 0 my $h = shift;
77 0 my $k = shift;
78  
79 0 my $l = _lookup($h, $k);
80  
81 0 return $$l
82 }
83  
84 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
85 0 my ($h, $k, $v) = @_;
86  
87 0 my $l = _lookup($h, $k);
88 0 $$l = $v;
89 0 return $h
90 }
91  
92 0 ▶sub values {
Subroutine values: not called CC=3 SCAR=24.8
93 0 ▶ if (ref $_[0] eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (ref $_[0] eq 'ARRAY') { } 0 0 0
94 0 return [ values $_[0]->%* ]
95 }
96 elsif (ref $_[0] eq "ARRAY") {
97 0 return $_[0]
98 }
99 }
100  
101 0 ▶sub keys {
Subroutine keys: not called CC=3 SCAR=24.8
102 0 ▶ if (ref $_[0] eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (ref $_[0] eq 'ARRAY') { } 0 0 0
103 0 return [ keys $_[0]->%* ]
104 }
105 elsif (ref $_[0] eq "ARRAY") {
106 0 return [ 0..$#{$_[0]} ]
107 }
108 }
109  
110 5 1;
↑ Summary