blib/lib/Data/SPath.pm

filter: Statementstmt 86.5% 109 / 126 Branchbran 81.5% 31 / 38 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 80.0% 20 / 25 Podpod n/a totaltotal 80.5% 161 / 200 CC 25 SCAR 16.2
CC
25
Cov
82%
_spath
11.2
_tokenize
7.0
_key_on_non_hash
6.0
CRAP 5.0
↑ Summary
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::SPath;
4 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 $Data::SPath::VERSION = '0.0004';
6 }
7 #ABSTRACT: lookup on nested data with simple path notation
8  
9 3 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use feature qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Scalar::Util qw/reftype blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Text::Balanced qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
14 extract_delimited
15 extract_bracketed
16 extract_multiple
17 3 /;
18  
19 3 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 exports => [ spath => \&_build_spath ]
21 3 };
22  
23 3 my @Error_Handlers = qw(
24 method_miss
25 key_miss
26 index_miss
27 key_on_non_hash
28 args_on_non_method
29 );
30  
31  
32 3 ▶sub _build_spath {
Subroutine _build_spath: called CC=1 SCAR=0.0
33 3 my ( $class, $name, $args ) = @_;
34  
35 109 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
36 109 my ( $data, $path, $opts ) = @_;
37 109 for ( @Error_Handlers ) {
38 545 ▶ unless ( exists $opts->{ $_ } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{$_}) 540 5 545
39 540 ▶ if ( exists $args->{ $_ } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{$_}) { } 45 495 540
40 45 $opts->{ $_ } = $args->{ $_ };
41 }
42 else {
43 495 $opts->{ $_ } = \&{ "_$_" };
44 }
45 }
46 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 545 ▶ unless ( ref( $opts->{ $_ } ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $opts->{$_} eq "CODE") 0 545 545
48 0 croak "$_ must be set to a code reference";
49 }
50 }
51 109 return _spath( $data, $path, $opts );
52 3 };
53 }
54  
55 # taken from Data::DPath
56 179 ▶sub _unescape {
Subroutine _unescape: called CC=2 SCAR=7.0
57 179 my ( $str ) = @_;
58 179 ▶ return unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 179 179
59 179 $str =~ s/(?<!\\)\\(["'])/$1/g; # '"$
60 179 $str =~ s/\\{2}/\\/g;
61 179 return $str;
62 }
63  
64 # Modified from Data::DPath. Added /s modifier to allow new lines in keys (why
65 # not?)
66 # this originally only supported double quote
67 41 ▶sub _unquote {
Subroutine _unquote: called CC=1 SCAR=0.0
68 41 my ($str) = @_;
69 41 $str =~ s/^(['"])(.*)\1$/$2/sg;
70 41 return $str;
71 }
72  
73 165 ▶sub _quoted { shift =~ m,^/["'], }
Subroutine _quoted: called CC=1 SCAR=0.0
74  
75 0 ▶sub _method_miss {
Subroutine _method_miss: not called CC=1 SCAR=6.9
76 0 my ( $method_name, $current, $depth ) = @_;
77 0 my $reftype = reftype( $current );
78 0 croak "tried to call nonexistent method '"
79 . $method_name
80 . "' on object with type $reftype at spath path element "
81 . $depth;
82 }
83  
84 0 ▶sub _key_miss {
Subroutine _key_miss: not called CC=1 SCAR=6.9
85 0 my ( $key, $current, $depth ) = @_;
86 0 croak "tried to access nonexistent key '"
87 . $key
88 . "' in hash at spath path element "
89 . $depth;
90 }
91  
92 0 ▶sub _index_miss {
Subroutine _index_miss: not called CC=1 SCAR=6.9
93 0 my ( $index, $current, $depth ) = @_;
94 0 croak "tried to access nonexistent index '"
95 . $index
96 . "' in array at spath path element "
97 . $depth;
98 }
99  
100 0 ▶sub _key_on_non_hash {
Subroutine _key_on_non_hash: not called CC=2 SCAR=17.9
101 0 my ( $key, $current, $depth ) = @_;
102 0 ▶ my $reftype = reftype( $current ) || '(non reference)';
Condition0%0/2
builtin::reftype($current) || '(non reference)'
l!l
00
MC/DC0%0/1
builtin::reftype($current) || '(non reference)'
builtin::reftype($current)
Truth table0%0/2
A
A= builtin::reftype($current)
Aresult
11
00
103 0 croak "tried to access key '"
104 . $key
105 . "' on a non-hash type "
106 . $reftype
107 . " at spath path element "
108 . $depth;
109 }
110  
111 0 ▶sub _args_on_non_method {
Subroutine _args_on_non_method: not called CC=2 SCAR=17.9
112 0 my ( $key, $current, $args, $depth ) = @_;
113 0 ▶ my $reftype = reftype( $current ) || '(non reference)';
Condition0%0/2
builtin::reftype($current) || '(non reference)'
l!l
00
MC/DC0%0/1
builtin::reftype($current) || '(non reference)'
builtin::reftype($current)
Truth table0%0/2
A
A= builtin::reftype($current)
Aresult
11
00
114 0 croak "tried to pass arguments '"
115 . $args
116 . "' to a non-method '"
117 . $key
118 . "' of type "
119 . $reftype
120 . "at spath path element "
121 . $depth;
122 }
123  
124  
125  
126 109 ▶sub _tokenize {
Subroutine _tokenize: called CC=7 SCAR=19.5
127 109 my ( $path ) = @_;
128  
129 109 my $remaining_path = $path;
130 109 my $extracted;
131 my @tokens;
132  
133 109 ▶ while ( $remaining_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_path) 165 109 274
134 165 my ( $prefix, $args );
135 165 my $key;
136  
137 165 ▶ if ( _quoted( $remaining_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_quoted($remaining_path)) { } 31 134 165
138 31 ( $key, $remaining_path ) = extract_delimited( $remaining_path, q|'"|, '/' );
139 31 ( $args, $remaining_path ) = extract_bracketed( $remaining_path, q|('")| );
140 31 $key = _unescape _unquote $key;
141  
142 }
143 else {
144 # must extract arguments first to keep extract_delimited from getting
145 # quoted structures with / in them
146 134 ▶ if ( $remaining_path =~ m,^/[^/]+\(, ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_path =~ m[^/[^/]+\(]) 7 127 134
147 7 ( $extracted, $remaining_path, $prefix ) = extract_bracketed( $remaining_path, q|('")|, '[^(]*' );
148 7 ▶ if ( defined $prefix or defined $remaining_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix or defined $remaining_path) { } 7 0 7
Condition33%1/3
defined $prefix or defined $remaining_path
l!l&&r!l&&!r
700
MC/DC0%0/2
defined $prefix or defined $remaining_path
defined $prefix defined $remaining_path
Truth table33%1/3
A or B
A= defined $prefixB= defined $remaining_path
ABresult
1X1
011
000
149 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 7 $remaining_path = $prefix . $remaining_path;
151 7 $args = $extracted;
152 }
153 else {
154 0 $remaining_path = $extracted;
155 }
156 }
157 134 ( $extracted, $remaining_path ) = extract_delimited( $remaining_path, '/' );
158 134 ▶ if ( not $extracted ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $extracted) { } 78 56 134
159 78 ( $extracted, $remaining_path ) = ( $remaining_path, undef );
160 }
161 else {
162 56 $remaining_path = ( chop $extracted ) . $remaining_path;
163 }
164 134 ( $key ) = $extracted =~ m,^/(.*),gs;
165 134 $key = _unescape $key;
166 }
167  
168 165 push @tokens, [ $key, $args ];
169 }
170 109 return \@tokens;
171 }
172  
173 5 ▶sub _tokenize_args {
Subroutine _tokenize_args: called CC=2 SCAR=6.9
174 5 my $args = shift;
175 5 ( $args ) = $args =~ /^\((.*)\)$/;
176 14 ▶ return map { _unescape( $_ =~ /^['"]/ ? _unquote( $_ ) : $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /^['"]/ ? : 10 4 14
177 extract_multiple( $args, [
178 # quoted structures
179 23 ▶ sub { extract_delimited( $_[0], q|'"| ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
180 # handle unquoted bare words
181 5 qr/\s*(\w+)/s,
182 qr/\s*([^,]+)(.*)/s
183 ], undef, 1 );
184 }
185  
186 109 ▶sub _spath {
Subroutine _spath: called CC=11 SCAR=24.2
187 109 my ( $data, $path, $opts ) = @_;
188  
189 109 my $current = $data;
190 109 my $depth = 0;
191 109 my $wantlist = wantarray;
192  
193 109 my $tokens = _tokenize( $path );
194  
195 109 for my $token ( @{ $tokens } ) {
196 165 $depth++;
197 165 my ( $key, $args ) = @{ $token };
198  
199 165 ▶ if ( blessed $current ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($current)) { } 14 151 165
200  
201 14 my @args;
202 14 ▶ @args = _tokenize_args( $args )
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 5 9 14
203 if defined $args;
204  
205 14 ▶ return $opts->{method_miss}->( $key, $current, $depth )
Branch: 1 branch
BranchTrueFalseTotal
unless my $method = $current->can($key) 2 12 14
206 unless my $method = $current->can( $key );
207  
208 12 ▶ if ( $wantlist ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantlist) { } 0 12 12
209 0 my @current = $current->$method( @args );
210 0 ▶ $current = @current > 1 ? \@current : $current[0];
Branch: 1 branch
BranchTrueFalseTotal
@current > 1 ? : 0 0 0
211 }
212 else {
213 12 $current = $current->$method( @args );
214 }
215 }
216 else {
217  
218 151 ▶ return $opts->{args_on_non_method}->( $key, $current, $args, $depth )
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 2 149 151
219 if defined $args;
220  
221 149 given ( ref $current ) {
222 149 when( 'HASH' ) {
223  
224 return $opts->{key_miss}->( $key, $current, $depth )
225 142 ▶ unless exists $current->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $current->{$key} 2 140 142
226  
227 140 $current = $current->{ $key };
228 }
229 7 when ( 'ARRAY' ) {
230  
231 5 ▶ return $opts->{key_on_non_hash}->( $key, $current, $depth )
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^\d+$/ 0 5 5
232 unless $key =~ /^\d+$/;
233 return $opts->{index_miss}->( $key, $current, $depth )
234 5 ▶ if $#{ $current } < $key;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$current;} < $key 2 3 5
235  
236 3 $current = $current->[ $key ];
237 }
238 2 default {
239 2 return $opts->{key_on_non_hash}->( $key, $current, $depth );
240 }
241 }
242 }
243 }
244 99 return $current;
245 }
246  
247  
248 3 1;
249  
250  
251 __END__
↑ Summary