PICA/Path.pm

filter: Statementstmt 99.4% 187 / 188 Branchbran 91.6% 121 / 132 Conditioncond 75.6% 28 / 37 MC/DCmcdc 56.5% 13 / 23 Subroutinesub 100% 22 / 22 Podpod 71.4% 10 / 14 totaltotal 91.5% 381 / 416 CC 83 SCAR 25.2
CC
83
Cov
94%
parse
25.1
match
14.0
match_subfields
13.0
CRAP 12.4
« PICA/Schema.pm ↑ Summary PICA/Schema/Builder.pm »
1 package PICA::Path;
2 17 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 our $VERSION = '2.12';
6  
7 17 require Exporter;
8 17 our @ISA = qw(Exporter);
9 17 our @EXPORT_OK = qw(pica_field_matcher);
10  
11 17 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 417 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
18 417 my $class = shift;
19 417 ▶ my $self = parse(@_) or confess "invalid pica path";
Branch: 1 branch
BranchTrueFalseTotal
my $self = parse(@_) or confess("invalid pica path") 406 11 417
20 406 bless $self, $class;
21 }
22  
23 17 our $TAG = '(?<tag>[012.][0-9.][0-9.][A-Z@.])';
24 17 our $SUBFIELDS = '(?<subfields>[A-Za-z0-9]+|\*)';
25 17 our $POSITION = '(?<position>(\d+)?(-(\d+)?)?)';
26 17 our $OCCURRENCE = '(?<occurrence>(\d+-\d+|[0-9.]{1,3}|\*))';
27  
28 417 ▶sub parse {
Subroutine parse: called CC=25 SCAR=32.2
Pod undocumented
29 417 my ($path) = @_;
30  
31 417 my $pattern = qr{^
32 $TAG
33 (\[$OCCURRENCE\] | \/$OCCURRENCE)?
34 ([\$.]?$SUBFIELDS)?
35 (\/$POSITION)?
36 $}x;
37  
38 417 ▶ return if $path !~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /$pattern/u 9 408 417
39  
40 408 my $field = $+{tag};
41 408 my $occurrence = $+{occurrence};
42 408 my $subfield = $+{subfields};
43 408 my $position = $+{position};
44  
45 408 ▶ if ($subfield eq '*') {
Branch: 3 branches
BranchTrueFalseTotal
if ($subfield eq '*') { } 16 392 408
elsif (defined $subfield) { } 170 222 392
elsif (defined $position) { } 3 219 222
46 16 $subfield = qr{[A-Za-z0-9]};
47 }
48 elsif (defined $subfield) {
49 170 $subfield = qr{[$subfield]};
50 }
51 elsif (defined $position) {
52 3 $subfield = qr{[A-Za-z0-9]};
53 }
54  
55 408 ▶ if ($occurrence eq '*' or (!$occurrence && $field =~ /^[2.]/)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($occurrence eq '*' or not $occurrence and $field =~ /^[2.]/u) { } 104 304 408
elsif ($occurrence =~ /^0*$/u) { } 289 15 304
elsif ($occurrence =~ /-/u) { } 9 6 15
elsif (defined $occurrence) { } 6 0 6
Condition100%6/6
$occurrence eq "*" or not $occurrence and $field =~ /^[2.]/u
l!l&&r!l&&!r
977304
not $occurrence and $field =~ /^[2.]/u
!ll&&!rl&&r
162887
MC/DC100%3/3
$occurrence eq "*" or not $occurrence and $field =~ /^[2.]/u
$occurrence eq "*" not $occurrence $field =~ /^[2.]/u
Truth table100%4/4
A or not(B) and C
A= $occurrence eq "*"B= not $occurrenceC= $field =~ /^[2.]/u
ABCresult
1XX1
0111
00X0
0100
56 104 $occurrence = '*';
57 }
58 elsif ($occurrence =~ /^0*$/) {
59 289 $occurrence = undef;
60 }
61 elsif ($occurrence =~ /-/) {
62 9 my ($from, $to) = map {1 * $_} split '-', $occurrence;
63 9 ▶ if ($from eq $to) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from eq $to) { } 1 8 9
elsif ($from < $to) { } 7 1 8
64 1 $occurrence = qr{$from};
65 }
66 elsif ($from < $to) {
67 7 $occurrence = [$from, $to];
68 }
69 else {
70 1 return;
71 }
72 }
73 elsif (defined $occurrence) {
74 6 $occurrence = qr{$occurrence};
75 }
76  
77 407 my @pos;
78 407 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) 17 390 407
79 17 $position =~ $POSITION;
80  
81 17 my ($from, $dash, $to, $length) = ($2, $3, $4, 0);
82  
83 17 ▶ if ($dash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dash) 14 3 17
84 14 ▶ return unless defined($from // $to); # /-
Branch: 1 branch
BranchTrueFalseTotal
unless defined($from // $to) 1 13 14
Condition100%3/3
$from // $to
l!l&&r!l&&!r
1211
MC/DC100%2/2
$from // $to
$from $to
Truth table100%3/3
A // B
A= $fromB= $to
ABresult
1X1
011
000
85 }
86  
87 16 ▶ if (defined $to) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $to) { } 7 9 16
88 7 ▶ if (!$from and $dash) { # /-X
Branch: 1 branch
BranchTrueFalseTotal
if (not $from and $dash) 4 3 7
Condition66%2/3
not $from and $dash
!ll&&!rl&&r
304
MC/DC50%1/2
not $from and $dash
not $from $dash
Truth table66%2/3
not(A) and B
A= not $fromB= $dash
ABresult
0X0
100
111
89 4 $from = 0;
90 }
91 7 $length = $to - $from + 1;
92 }
93 else {
94 9 ▶ if ($dash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dash) { } 6 3 9
95 6 $length = undef;
96 }
97 else {
98 3 $length = 1;
99 }
100 }
101  
102 16 ▶ if (!defined $length or $length >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $length or $length >= 1) 16 0 16
Condition66%2/3
not defined $length or $length >= 1
l!l&&r!l&&!r
6100
MC/DC0%0/2
not defined $length or $length >= 1
not defined $length $length >= 1
Truth table66%2/3
not(A) or B
A= not defined $lengthB= $length >= 1
ABresult
1X1
011
000
103 16 ▶ unless (!$from and !defined $length) { # /0-
Branch: 1 branch
BranchTrueFalseTotal
if ($from or defined $length) 13 3 16
Condition100%3/3
$from or defined $length
l!l&&r!l&&!r
943
MC/DC100%2/2
$from or defined $length
$from defined $length
Truth table100%3/3
A or B
A= $fromB= defined $length
ABresult
1X1
011
000
104 13 @pos = ($from, $length);
105 }
106 }
107 }
108  
109 return {
110 406 field => qr{$field},
111 occurrence => $occurrence,
112 subfield => $subfield,
113 position => \@pos
114 };
115 }
116  
117 63 ▶sub match {
Subroutine match: called CC=14 SCAR=26.4
Pod documented
118 63 my ($self, $record, %args) = @_;
119  
120 63 my %default_args = (
121 force_array => 0,
122 join => '',
123 nested_arrays => 0,
124 pluck => 0,
125 split => 0,
126 value => undef,
127 );
128 63 %args = (%default_args, %args);
129 63 ▶ if ($args{nested_arrays}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"nested_arrays"}) 11 52 63
130 11 $args{split} = 1;
131 }
132  
133 # check if path exists
134 63 ▶ if ($args{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"value"}) 3 60 63
135 3 my $value = $self->record_subfields($record);
136 3 ▶ return $value ? $args{value} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 1 3
137 }
138  
139 # gather values from matched subfields
140 60 my @matches;
141 60 for my $field (@{$self->record_fields($record)}) {
142 131 ▶ next unless defined $field;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 131 131
143 my @matched_subfields
144 = $args{pluck}
145 131 ▶ ? $self->match_subfields($field, pluck => 1)
Branch: 1 branch
BranchTrueFalseTotal
$args{'pluck'} ? : 7 124 131
146 : $self->match_subfields($field);
147 131 ▶ next unless grep {defined $_} @matched_subfields;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @matched_subfields 18 113 131
148 113 ▶ if ($args{split}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'split'}) { } 65 48 113
149 65 ▶ if ($args{nested_arrays}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'nested_arrays'}) { } 33 32 65
150 33 push @matches, \@matched_subfields;
151 }
152 else {
153 32 push @matches, @matched_subfields;
154 }
155 }
156 else {
157 48 push @matches, join($args{join}, @matched_subfields);
158 }
159  
160 }
161 60 ▶ if (@matches) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches) 58 2 60
162  
163 # return matched fields as array reference
164 58 ▶ if ($args{split}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'split'}) { } 26 32 58
165 26 ▶ return $args{force_array} ? [\@matches] : \@matches;
Branch: 1 branch
BranchTrueFalseTotal
$args{'force_array'} ? : 8 18 26
166 }
167  
168 # ... or string
169 else {
170 return $args{force_array}
171 ? \@matches
172 32 ▶ : join($args{join}, @matches);
Branch: 1 branch
BranchTrueFalseTotal
$args{'force_array'} ? : 5 27 32
173 }
174 }
175 2 return;
176  
177 }
178  
179 17 *match_record = *match;
180  
181 25417 ▶sub match_field {
Subroutine match_field: called CC=9 SCAR=22.0
Pod documented
182 25417 my ($self, $field) = @_;
183  
184 25417 ▶ return if $field->[0] !~ $self->{field};
Branch: 1 branch
BranchTrueFalseTotal
unless $field->[0] =~ /$self->{"field"}/u 24948 469 25417
185  
186 469 my $spec = $self->{occurrence};
187  
188 469 ▶ if ($spec ne '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec ne "*") 189 280 469
189 189 my $occ = $field->[1];
190 189 ▶ if (!$spec) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $spec) { } 178 11 189
elsif (ref $spec eq 'ARRAY') { } 5 6 11
191 178 ▶ return if $occ > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $occ > 0 2 176 178
192 }
193 elsif (ref $spec eq 'ARRAY') {
194 5 ▶ return if $occ < $spec->[0] or $occ > $spec->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $occ < $spec->[0] or $occ > $spec->[1] 3 2 5
Condition100%3/3
$occ < $spec->[0] or $occ > $spec->[1]
l!l&&r!l&&!r
122
MC/DC100%2/2
$occ < $spec->[0] or $occ > $spec->[1]
$occ < $spec->[0] $occ > $spec->[1]
Truth table100%3/3
A or B
A= $occ < $spec->[0]B= $occ > $spec->[1]
ABresult
1X1
011
000
195 }
196 else {
197 6 ▶ return if $occ !~ $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $occ =~ /$spec/u 2 4 6
198 }
199 }
200  
201 462 return $field;
202 }
203  
204 2098 ▶sub match_subfields {
Subroutine match_subfields: called CC=13 SCAR=25.7
Pod documented
205 2098 my ($self, $field, %args) = @_;
206  
207 2098 my $subfield_regex = $self->{subfield};
208 2098 my $from = $self->{position}->[0];
209 2098 my $length = $self->{position}->[1];
210  
211 2098 my @values;
212  
213 2098 ▶ if ($args{pluck}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'pluck'}) { } 7 2091 2098
214  
215 # Treat the subfields as a hash index
216 7 my $subfield_href = {};
217 7 ▶ for (my $i = 2; $i < @{$field}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 14 7 21
218 14 push @{$subfield_href->{$field->[$i]}}, $field->[$i + 1];
219 }
220  
221 7 my $subfields = $self->{subfield};
222 7 $subfields =~ s{.*\[(.+)\].*}{$1}g;
223 7 for my $subfield (split('', $subfields)) {
224 14 ▶ my $value = $subfield_href->{$subfield} // [undef];
Condition100%2/2
$subfield_href->{$subfield} // [undef]
l!l
131
MC/DC100%1/1
$subfield_href->{$subfield} // [undef]
$subfield_href->{$subfield}
Truth table100%2/2
A
A= $subfield_href->{$subfield}
Aresult
11
00
225 14 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) { } 2 12 14
226 push @values, map {
227 2 ▶ $length
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 2 0 2
228 ? substr($_, $from, $length)
229 : substr($_, $from)
230 2 } @{$value};
231 }
232 else {
233 12 push @values, @{$value};
234 }
235  
236 }
237 }
238 else {
239 2091 ▶ for (my $i = 2; $i < @$field; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$field) 3257 2091 5348
240 3257 ▶ if ($field->[$i] =~ $subfield_regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] =~ /$subfield_regex/u) 2138 1119 3257
241 2138 my $value = $field->[$i + 1];
242 2138 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 9 2129 2138
243 9 ▶ $value
Branch: 1 branch
BranchTrueFalseTotal
$length ? : 7 2 9
244 = $length
245 ? substr($value, $from, $length)
246 : substr($value, $from);
247 9 ▶ next if '' eq ($value // '');
Branch: 1 branch
BranchTrueFalseTotal
if "" eq ($value // "") 1 8 9
Condition50%1/2
$value // ""
l!l
90
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
248 }
249 2137 push @values, $value;
250 }
251 }
252 }
253  
254 2098 return @values;
255 }
256  
257 60 ▶sub record_fields {
Subroutine record_fields: called CC=2 SCAR=7.0
Pod undocumented
258 60 my ($self, $record) = @_;
259  
260 60 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 60 0 60
261 60 return [grep {$self->match_field($_)} @$record];
262 }
263  
264 13 ▶sub record_subfields {
Subroutine record_subfields: called CC=3 SCAR=11.0
Pod undocumented
265 13 my ($self, $record) = @_;
266  
267 13 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 13 0 13
268  
269 13 my @values;
270  
271 13 foreach my $field (grep {$self->match_field($_)} @$record) {
272 20 push @values, $self->match_subfields($field);
273 }
274  
275 13 return @values;
276 }
277  
278 135 ▶sub unescape {
Subroutine unescape: called CC=2 SCAR=6.9
Pod undocumented
279 135 my $exp = shift;
280 135 ▶ $exp =~ s/^\(\?[^:]*:(.*)\)$/$1/ if defined $exp;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exp 106 29 135
281 135 return $exp;
282 }
283  
284 50 ▶sub stringify {
Subroutine stringify: called CC=10 SCAR=23.3
Pod documented
285 50 my ($self, $short) = @_;
286  
287 50 my $str = $self->fields;
288  
289 50 my $occurrence = $self->occurrences;
290 50 ▶ if (defined $occurrence) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $occurrence) 22 28 50
291 22 ▶ $str .= "/$occurrence" unless $str =~ /^[2.]/ and $occurrence eq '*';
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /^[2.]/u and $occurrence eq "*" 10 12 22
Condition66%2/3
$str =~ /^[2.]/u and $occurrence eq "*"
!ll&&!rl&&r
10012
MC/DC50%1/2
$str =~ /^[2.]/u and $occurrence eq "*"
$str =~ /^[2.]/u $occurrence eq "*"
Truth table66%2/3
A and B
A= $str =~ /^[2.]/uB= $occurrence eq "*"
ABresult
0X0
100
111
292 }
293  
294 50 my $subfields = $self->subfields;
295 50 ▶ if (defined $subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subfields) 27 23 50
296 27 ▶ unless ($short and $subfields !~ /^\$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($short and not $subfields =~ /^\$/u) 26 1 27
Condition66%2/3
$short and not $subfields =~ /^\$/u
!ll&&!rl&&r
2601
MC/DC50%1/2
$short and not $subfields =~ /^\$/u
$short not $subfields =~ /^\$/u
Truth table66%2/3
A and not(B)
A= $shortB= not $subfields =~ /^\$/u
ABresult
0X0
100
111
297 26 $str .= '$';
298 }
299 27 $str .= $subfields;
300 }
301  
302 50 my $pos = $self->positions;
303 50 ▶ if (defined $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 6 44 50
304 6 ▶ $str
Branch: 1 branch
BranchTrueFalseTotal
defined $subfields || defined $occurrence ? : 6 0 6
Condition33%1/3
defined $subfields or defined $occurrence
l!l&&r!l&&!r
600
MC/DC0%0/2
defined $subfields or defined $occurrence
defined $subfields defined $occurrence
Truth table33%1/3
A or B
A= defined $subfieldsB= defined $occurrence
ABresult
1X1
011
000
305 .= (defined $subfields || defined $occurrence)
306 ? "/$pos"
307 : "\$*/$pos";
308 }
309  
310 50 $str;
311 }
312  
313 52 ▶sub fields {
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
314 52 return unescape($_[0]->{field});
315 }
316  
317 52 ▶sub occurrences {
Subroutine occurrences: called CC=2 SCAR=6.9
Pod documented
318 52 my $occ = $_[0]->{occurrence};
319 52 ▶ return join "-", @$occ if ref $occ eq 'ARRAY'; # range
Branch: 1 branch
BranchTrueFalseTotal
if ref $occ eq "ARRAY" 4 48 52
320 48 return unescape($occ); # pattern
321 }
322  
323 59 ▶sub subfields {
Subroutine subfields: called CC=3 SCAR=11.0
Pod documented
324 59 ▶ return unless defined $_[0]->{subfield};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"subfield"} 24 35 59
325 35 my $subfields = unescape($_[0]->{subfield});
326 35 ▶ if ($subfields eq '[A-Za-z0-9]') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfields eq '[A-Za-z0-9]') { } 16 19 35
327 16 return '*';
328 }
329 else {
330 19 $subfields =~ s/\[|\]//g;
331 19 return $subfields;
332 }
333 }
334  
335 52 ▶sub positions {
Subroutine positions: called CC=8 SCAR=21.6
Pod documented
336 52 my ($self) = @_;
337  
338 52 my ($from, $length, $pos) = @{$self->{position}};
339 52 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 7 45 52
340 7 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 7 0 7
341 7 $pos = $from;
342 }
343 7 ▶ if (!defined $length) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $length) { } 2 5 7
elsif ($length > 1) { } 3 2 5
elsif ($length == 1 and not $from) { } 0 2 2
344 2 ▶ if ($from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from) 2 0 2
345 2 $pos = "$from-";
346 }
347 }
348 elsif ($length > 1) {
349 3 $pos .= '-' . ($from + $length - 1);
350 }
351 ▶ elsif ($length == 1 && !$from) {
Condition33%1/3
$length == 1 and not $from
!ll&&!rl&&r
020
MC/DC0%0/2
$length == 1 and not $from
$length == 1 not $from
Truth table33%1/3
A and not(B)
A= $length == 1B= not $from
ABresult
0X0
100
111
352 0 $pos = 0;
353 }
354 }
355  
356 52 return $pos;
357 }
358  
359 158 ▶sub pica_field_matcher {
Subroutine pica_field_matcher: called CC=2 SCAR=7.0
Pod documented
360 158 ▶ my @pathes = map {ref $_ ? $_ : PICA::Path->new($_)} @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 159 159
361 24452 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
362 24452 my $field = shift;
363 24452 ▶ any {$_->match_field($field)} @pathes;
Subroutine __ANON__: called CC=1 SCAR=0.0
364 157 };
365 }
366  
367 17 1;
368 __END__
« PICA/Schema.pm ↑ Summary PICA/Schema/Builder.pm »