blib/lib/Data/Diver.pm

filter: Statementstmt 92.0% 69 / 75 Branchbran 86.9% 40 / 46 Conditioncond 72.7% 24 / 33 MC/DCmcdc 60.0% 12 / 20 Subroutinesub 100% 10 / 10 Podpod 100% 6 / 6 totaltotal 84.7% 161 / 190 CC 35 SCAR 26.2
CC
35
Cov
86%
DiveRef
18.5
Dive
17.3
DiveDie
5.0
CRAP 13.7
↑ Summary
1 package Data::Diver;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 1 require Exporter;
5 1 ▶use vars qw( $VERSION @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION= 1.01_01;
8 1 @EXPORT_OK= qw( Dive DiveRef DiveVal DiveError DiveDie DiveClear );
9 1 *import= \&Exporter::import;
10 1 *isa= \&UNIVERSAL::isa;
11 }
12
13
14 # To figure out if an item supports being treated as a particular
15 # type of reference (hash ref, array ref, or scalar ref) we use:
16 # eval { my $x= DEREF_EXPR; 1 }
17 # Note that we are careful to not put 'DEREF_EXPR' into an "lvalue
18 # context" (to prevent autovivification) and to also avoid trying to
19 # convert the value into a number or boolean or such. The "; 1" is
20 # so that the eval always returns a true value unless something die()s.
21
22 # Using 'ARRAY' eq ref($ref) is just a horrid alternative, as it would
23 # prevent these routines from being used on blessed data structures.
24
25 # Using UNIVERSAL::isa($ref,'ARRAY') is a better alternative, but it
26 # still fails for more advanced cases of overloading or pathological
27 # cases of blessing into very-poorly-named packages. We use this for
28 # testing for CODE references, since eval { $ref->() } would actually
29 # run the code.
30
31
32 1 my @lastError;
33
34
35 sub _Error
36 9 ▶{
Subroutine _Error: called CC=1 SCAR=0.0
37 9 @lastError= @_[2,0,1];
38 9 return;
39 }
40
41
42 sub DiveError
43 27 ▶{
Subroutine DiveError: called CC=1 SCAR=0.0
Pod documented
44 27 return @lastError;
45 }
46
47
48 sub DiveClear
49 1 ▶{
Subroutine DiveClear: called CC=1 SCAR=0.0
Pod documented
50 1 @lastError= ();
51 }
52
53
54 sub DiveDie
55 10 ▶{
Subroutine DiveDie: called CC=5 SCAR=16.1
Pod documented
56 10 ▶ @_= Dive( @_ ) if 1 < @_;
Branch: 1 branch
BranchTrueFalseTotal
if 1 < @_ 3 7 10
57 10 ▶ return wantarray ? @_ : pop @_
Branch: 2 branches
BranchTrueFalseTotal
if @_ or not @lastError 8 2 10
wantarray ? : 4 4 8
Condition100%3/3
@_ or not @lastError
l!l&&r!l&&!r
622
MC/DC100%2/2
@_ or not @lastError
@_ not @lastError
Truth table100%3/3
A or not(B)
A= @_B= not @lastError
ABresult
1X1
011
000
58 if @_ || ! @lastError;
59 2 my( $errDesc, $ref, $svKey )= @lastError;
60 2 die "$errDesc using $$svKey on $ref (from Data::Diver).\n";
61 }
62
63
64 sub Dive
65 24 ▶{
Subroutine Dive: called CC=17 SCAR=28.5
Pod documented
66 24 ▶ return if ! @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 24 24
67 24 my $ref= shift @_;
68 24 ▶ return $ref if ! $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 24 24
69 24 ▶ while( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 79 15 94
70 79 my $key= shift @_;
71 79 ▶ if( ! defined $key ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $key) { } 14 65 79
elsif (eval { do { my $x = $key->[0]; 1 } } and Data::Diver::isa($ref, 'CODE')) { } 4 61 65
elsif ($key =~ /^-?\d+$/ and eval { do { my $x = $ref->[0]; 1 } }) { } 22 39 61
elsif (eval { do { exists $ref->{$key} } }) { } 34 5 39
elsif (eval { do { my $x = $ref->{$key}; 1 } }) { } 2 3 5
72 return _Error( $ref, \$key, "undef() on non-scalar-ref" )
73 14 ▶ if ! eval { my $x= $$ref; 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { my $x = $$ref; 1 } } 2 12 14
74 12 $ref= $$ref;
75 65 ▶ } elsif( eval { my $x= $key->[0]; 1 }
Condition66%2/3
eval { do { my $x = $key->[0]; 1 } } and Data::Diver::isa($ref, "CODE")
!ll&&!rl&&r
6104
MC/DC50%1/2
eval { do { my $x = $key->[0]; 1 } } and Data::Diver::isa($ref, "CODE")
eval { do { my $x = $key->[0]; 1 } } Data::Diver::isa($ref, "CODE")
Truth table66%2/3
A and B
A= eval { do { my $x = $key->[0]; 1 } }B= Data::Diver::isa($ref, "CODE")
ABresult
0X0
100
111
76 && isa( $ref, 'CODE' )
77 ) {
78 4 ▶ if( @_ && ! defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not defined $_[0]) { } 2 2 4
Condition66%2/3
@_ and not defined $_[0]
!ll&&!rl&&r
022
MC/DC50%1/2
@_ and not defined $_[0]
@_ not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_B= not defined $_[0]
ABresult
0X0
100
111
79 2 $ref= \ $ref->( @$key );
80 } else {
81 2 $ref= [ $ref->( @$key ) ];
82 }
83 ▶ } elsif( $key =~ /^-?\d+$/
Condition100%3/3
$key =~ /^-?\d+$/ and eval { do { my $x = $ref->[0]; 1 } }
!ll&&!rl&&r
38122
MC/DC100%2/2
$key =~ /^-?\d+$/ and eval { do { my $x = $ref->[0]; 1 } }
$key =~ /^-?\d+$/ eval { do { my $x = $ref->[0]; 1 } }
Truth table100%3/3
A and B
A= $key =~ /^-?\d+$/B= eval { do { my $x = $ref->[0]; 1 } }
ABresult
0X0
100
111
84 23 && eval { my $x= $ref->[0]; 1 }
85 ) {
86 22 ▶ return _Error( $ref, \$key, "Index out of range" )
Branch: 1 branch
BranchTrueFalseTotal
if $key < -@$ref or $ref->$#* < $key 2 20 22
Condition100%3/3
$key < -@$ref or $ref->$#* < $key
l!l&&r!l&&!r
1120
MC/DC100%2/2
$key < -@$ref or $ref->$#* < $key
$key < -@$ref $ref->$#* < $key
Truth table100%3/3
A or B
A= $key < -@$refB= $ref->$#* < $key
ABresult
1X1
011
000
87 if $key < -@$ref
88 || $#$ref < $key;
89 20 $ref= $ref->[$key];
90 39 } elsif( eval { exists $ref->{$key} } ) {
91 0 ▶ if( eval { my $x= $$key; 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { my $x = $$key; 1 } }) { } 0 34 34
92 0 $ref= $ref->{$$key};
93 } else {
94 34 $ref= $ref->{$key};
95 }
96 5 } elsif( eval { my $x= $ref->{$key}; 1 } ) {
97 2 return _Error( $ref, \$key, "Key not present in hash" );
98 } else {
99 3 return _Error( $ref, \$key, "Not a valid type of reference" );
100 }
101 }
102 15 return $ref;
103 }
104
105
106 2 ▶sub DiveVal :lvalue
Subroutine DiveVal: called CC=1 SCAR=0.0
Pod documented
107 {
108 2 ${ DiveRef( @_ ) };
109 }
110
111
112 sub DiveRef
113 17 ▶{
Subroutine DiveRef: called CC=15 SCAR=29.2
Pod documented
114 17 ▶ return if ! @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 16 17
115 16 my $sv= \shift @_;
116 16 ▶ return $$sv if ! $$sv;
Branch: 1 branch
BranchTrueFalseTotal
unless $$sv 2 14 16
117 14 ▶ while( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 39 11 50
118 39 my $key= shift @_;
119 39 ▶ if( ! defined $key ) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $key) { } 7 32 39
elsif (eval { do { my $x = $key->[0]; 1 } } and Data::Diver::isa($$sv, 'CODE')) { } 0 32 32
elsif (eval { do { my $x = $$key; 1 } } and !defined($$sv) || eval { do { my $x = $$sv->{'0'}; 1 } }) { } 10 22 32
elsif ($key =~ /^-?\d+$/ and !defined($$sv) || eval { do { my $x = $$sv->[0]; 1 } }) { } 5 17 22
120 7 $sv= \$$$sv;
121 0 ▶ } elsif( eval { my $x= $key->[0]; 1 }
Condition33%1/3
eval { do { my $x = $key->[0]; 1 } } and Data::Diver::isa($$sv, "CODE")
!ll&&!rl&&r
3200
MC/DC0%0/2
eval { do { my $x = $key->[0]; 1 } } and Data::Diver::isa($$sv, "CODE")
eval { do { my $x = $key->[0]; 1 } } Data::Diver::isa($$sv, "CODE")
Truth table33%1/3
A and B
A= eval { do { my $x = $key->[0]; 1 } }B= Data::Diver::isa($$sv, "CODE")
ABresult
0X0
100
111
122 && isa( $$sv, 'CODE' )
123 ) {
124 0 ▶ if( @_ && ! defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not defined $_[0]) { } 0 0 0
Condition0%0/3
@_ and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and not defined $_[0]
@_ not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_B= not defined $_[0]
ABresult
0X0
100
111
125 0 $sv= \ $$sv->( @$key );
126 } else {
127 0 $sv= \[ $$sv->( @$key ) ];
128 }
129 32 ▶ } elsif( eval { my $x= $$key; 1 }
Condition100%6/6
eval { do { my $x = $$key; 1 } } and !defined($$sv) || eval { do { my $x = $$sv->{0}; 1 } }
!ll&&!rl&&r
21110
not defined $$sv or eval { do { my $x = $$sv->{0}; 1 } }
l!l&&r!l&&!r
461
MC/DC100%3/3
eval { do { my $x = $$key; 1 } } and !defined($$sv) || eval { do { my $x = $$sv->{0}; 1 } }
eval { do { my $x = $$key; 1 } } not defined $$sv eval { do { my $x = $$sv->{0}; 1 } }
Truth table100%4/4
A and not(B) or C
A= eval { do { my $x = $$key; 1 } }B= not defined $$svC= eval { do { my $x = $$sv->{0}; 1 } }
ABCresult
0XX0
1000
11X1
1011
130 and ! defined($$sv)
131 7 || eval { my $x= $$sv->{0}; 1 }
132 ) {
133 10 $sv= \$$sv->{$$key};
134 ▶ } elsif( $key =~ /^-?\d+$/
Condition66%4/6
$key =~ /^-?\d+$/ and !defined($$sv) || eval { do { my $x = $$sv->[0]; 1 } }
!ll&&!rl&&r
1705
not defined $$sv or eval { do { my $x = $$sv->[0]; 1 } }
l!l&&r!l&&!r
140
MC/DC33%1/3
$key =~ /^-?\d+$/ and !defined($$sv) || eval { do { my $x = $$sv->[0]; 1 } }
$key =~ /^-?\d+$/ not defined $$sv eval { do { my $x = $$sv->[0]; 1 } }
Truth table75%3/4
A and not(B) or C
A= $key =~ /^-?\d+$/B= not defined $$svC= eval { do { my $x = $$sv->[0]; 1 } }
ABCresult
0XX0
1000
11X1
1011
135 and ! defined($$sv)
136 4 || eval { my $x= $$sv->[0]; 1 }
137 ) {
138 5 $sv= \$$sv->[$key];
139 } else {
140 17 $sv= \$$sv->{$key};
141 }
142 }
143 11 return $sv;
144 }
145
146
147 1 'Data::Diver';
148
149 __END__
↑ Summary