| 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
| |||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | return wantarray ? @_ : pop @_ | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
@_ or not @lastError
MC/DC100%2/2
@_ or not @lastError
@_
not @lastError
Truth table100%3/3
A or not(B)
A= @_B= not @lastError
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 67 | 24 | my $ref= shift @_; | |||||||||||||||||||||||||||||||||
| 68 | 24 | ▶ | return $ref if ! $ref; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 69 | 24 | ▶ | while( @_ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 70 | 79 | my $key= shift @_; | |||||||||||||||||||||||||||||||||
| 71 | 79 | ▶ | if( ! defined $key ) { | ||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||
| 72 | return _Error( $ref, \$key, "undef() on non-scalar-ref" ) | ||||||||||||||||||||||||||||||||||
| 73 | 14 | ▶ | if ! eval { my $x= $$ref; 1 }; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||
| 76 | && isa( $ref, 'CODE' ) | ||||||||||||||||||||||||||||||||||
| 77 | ) { | ||||||||||||||||||||||||||||||||||
| 78 | 4 | ▶ | if( @_ && ! defined $_[0] ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ and not defined $_[0]
MC/DC50%1/2
@_ and not defined $_[0]
@_
not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_B= not defined $_[0]
| |||||||||||||||||||||||||||||||||||
| 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
}
}
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
}
}
| |||||||||||||||||||||||||||||||||||
| 84 | 23 | && eval { my $x= $ref->[0]; 1 } | |||||||||||||||||||||||||||||||||
| 85 | ) { | ||||||||||||||||||||||||||||||||||
| 86 | 22 | ▶ | return _Error( $ref, \$key, "Index out of range" ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key < -@$ref or $ref->$#* < $key
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 115 | 16 | my $sv= \shift @_; | |||||||||||||||||||||||||||||||||
| 116 | 16 | ▶ | return $$sv if ! $$sv; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 117 | 14 | ▶ | while( @_ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 39 | my $key= shift @_; | |||||||||||||||||||||||||||||||||
| 119 | 39 | ▶ | if( ! defined $key ) { | ||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||||||||
| 122 | && isa( $$sv, 'CODE' ) | ||||||||||||||||||||||||||||||||||
| 123 | ) { | ||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | if( @_ && ! defined $_[0] ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ and not defined $_[0]
MC/DC0%0/2
@_ and not defined $_[0]
@_
not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_B= not defined $_[0]
| |||||||||||||||||||||||||||||||||||
| 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
}
}
not defined $$sv or eval {
do {
my $x = $$sv->{0};
1
}
}
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
}
}
| |||||||||||||||||||||||||||||||||||
| 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
}
}
not defined $$sv or eval {
do {
my $x = $$sv->[0];
1
}
}
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
}
}
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||