PICA/Data.pm

filter: Statementstmt 94.1% 337 / 358 Branchbran 83.5% 132 / 158 Conditioncond 84.0% 21 / 25 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 96.7% 60 / 62 Podpod 80.0% 24 / 30 totaltotal 90.2% 585 / 648 CC 101 SCAR 20.0
CC
101
Cov
91%
pica_update
20.3
pica_holdings
14.0
write
12.0
CRAP 7.4
« PICA/Parser/Import.pm ↑ Summary PICA/Parser/Base.pm »
1 package PICA::Data;
2 16 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '2.12';
5  
6 16 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 our @EXPORT_OK
8 16 = qw(pica_data pica_parser pica_writer pica_path pica_xml_struct
9 pica_match pica_values pica_value pica_fields pica_subfields
10 pica_title pica_holdings pica_items pica_field
11 pica_split pica_annotation pica_sort pica_guess clean_pica pica_string pica_id
12 pica_sort_subfields parse_subfield_schedule
13 pica_diff pica_patch pica_empty);
14 16 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
15  
16 16 our $ILN_PATH = PICA::Path->new('101@a');
17 16 our $EPN_PATH = PICA::Path->new('203@/*$0');
18  
19 16 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use List::Util qw(first any);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use PICA::Path qw(pica_field_matcher);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 16 ▶use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
30 8 my ($class, $data) = @_;
31  
32 8 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 7 1 8
33 7 ▶ my $parser = pica_guess($data)
Branch: 1 branch
BranchTrueFalseTotal
my $parser = pica_guess($data) or croak("Could not guess PICA serialization format") 7 0 7
34 or croak "Could not guess PICA serialization format";
35 7 return $parser->new(\$data)->next;
36 }
37 else {
38 1 return bless {record => []}, 'PICA::Data';
39 }
40 }
41  
42 6 ▶sub pica_data {
Subroutine pica_data: called CC=1 SCAR=0.0
Pod documented
43 6 PICA::Data->new(@_);
44 }
45  
46 7 ▶sub pica_field {
Subroutine pica_field: called CC=1 SCAR=0.0
Pod documented
47 7 PICA::Data::Field->new(@_);
48 }
49  
50 59 ▶sub pica_match {
Subroutine pica_match: called CC=3 SCAR=11.0
Pod documented
51 59 my ($record, $path, %args) = @_;
52  
53 59 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 59 0 59
54 59 ▶ return unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 4 55 59
55  
56 55 return $path->match_record($record, %args);
57 }
58  
59 11 ▶sub pica_values {
Subroutine pica_values: called CC=3 SCAR=11.0
Pod documented
60 11 my ($record, $path) = @_;
61  
62 11 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 10 1 11
63 11 ▶ return unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 1 10 11
64  
65 10 return $path->record_subfields($record);
66 }
67  
68 1470 ▶sub pica_fields {
Subroutine pica_fields: called CC=5 SCAR=16.1
Pod documented
69 1470 my $record = shift;
70 1470 ▶ croak "missing record. Did you mean pica_field instead of pica_fields?"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $record 0 1470 1470
71 unless ref $record;
72  
73 1470 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 1415 55 1470
74  
75 1470 ▶ return [@$record] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1313 157 1470
76  
77 157 my $matcher = eval {pica_field_matcher(@_)};
78 157 ▶ return [] unless $matcher;
Branch: 1 branch
BranchTrueFalseTotal
unless $matcher 1 156 157
79  
80 156 return [grep {$matcher->($_)} @$record];
81 }
82  
83 3 ▶sub pica_append {
Subroutine pica_append: called CC=2 SCAR=7.2
Pod documented
84 3 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{record} : shift;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 3 0 3
85 3 push @$fields, pica_field(@_);
86 }
87  
88 1 ▶sub pica_remove {
Subroutine pica_remove: called CC=2 SCAR=7.0
Pod documented
89 1 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{record} : shift;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 1 0 1
90 1 my $matcher = pica_field_matcher(@_);
91  
92 # modify in_place
93 1 splice @$fields, 0, @$fields, grep {!$matcher->($_)} @$fields;
94 }
95  
96 7 ▶sub pica_update {
Subroutine pica_update: called CC=20 SCAR=30.1
Pod documented
97 7 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{record} : shift;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 7 0 7
98 7 ▶ if (@_ == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 6 1 7
elsif (@_) { } 1 0 1
99 6 my $path = PICA::Path->new(shift);
100 6 ▶ my $sfregex = $path->{subfield} or croak "missing subfields";
Branch: 1 branch
BranchTrueFalseTotal
my $sfregex = $path->{"subfield"} or croak("missing subfields") 5 1 6
101 5 ▶ my $value = shift // '';
Condition100%2/2
shift() // ""
l!l
41
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
102  
103 5 ▶ for (my $i = 0; $i < @$fields; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$fields) 5 5 10
104 5 ▶ if ($path->match_field($fields->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->match_field($fields->[$i])) 5 0 5
105 5 my $f = $fields->[$i];
106 5 ▶ if ($value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value ne '') { } 3 2 5
107  
108 # replace subfield value
109 3 my $append = $path->subfields =~ /^[A-Za-z0-9]$/;
110 3 ▶ for (my $j = 2; $j < @$f; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$f) 5 3 8
111 5 ▶ if ($f->[$j] =~ $sfregex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->[$j] =~ /$sfregex/u) 3 2 5
112 3 $f->[$j + 1] = $value;
113 3 $append = 0;
114 }
115 }
116 3 ▶ push @$f, $path->subfields, $value if $append;
Branch: 1 branch
BranchTrueFalseTotal
if $append 1 2 3
117 }
118 else {
119 # remove subfield
120  
121 2 my @sf;
122 2 ▶ for (my $j = 2; $j < @$f; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @$f) 3 2 5
123 3 ▶ push @sf, $f->[$j], $f->[$j + 1]
Branch: 1 branch
BranchTrueFalseTotal
unless $f->[$j] =~ /$sfregex/u 1 2 3
124 if $f->[$j] !~ $sfregex;
125 }
126 2 ▶ my $sfnum = @$f % 2 ? @$f - 3 : @$f - 2;
Branch: 1 branch
BranchTrueFalseTotal
@$f % 2 ? : 0 2 2
127 2 ▶ if (@sf && @sf < $sfnum) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sf and @sf < $sfnum) { } 1 1 2
Condition66%2/3
@sf and @sf < $sfnum
!ll&&!rl&&r
101
MC/DC50%1/2
@sf and @sf < $sfnum
@sf @sf < $sfnum
Truth table66%2/3
A and B
A= @sfB= @sf < $sfnum
ABresult
0X0
100
111
128 1 splice @$f, 2, $sfnum, @sf;
129 }
130 else {
131 # field is empty, so remove it
132 1 splice @$fields, $i--, 1;
133 }
134 }
135 }
136 }
137 }
138 elsif (@_) {
139 1 my $value = pica_field(@_);
140 1 ▶ my $path = PICA::Path->new($value->[0] . '/' . ($value->[1] // '0'));
Condition50%1/2
$value->[1] // 0
l!l
01
MC/DC0%0/1
$value->[1] // 0
$value->[1]
Truth table50%1/2
A
A= $value->[1]
Aresult
11
00
141 1 ▶ for (my $i = 0; $i < @$fields; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$fields) 3 1 4
142 3 ▶ if ($path->match_field($fields->[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->match_field($fields->[$i])) 1 2 3
143 1 $fields->[$i] = $value;
144 }
145 }
146 }
147 }
148  
149 5 ▶sub pica_subfields {
Subroutine pica_subfields: called CC=3 SCAR=11.0
Pod documented
150 5 my @sf;
151 5 for (@{pica_fields(@_)}) {
152 8 ▶ my $l = @$_ % 2 ? $#$_ - 1 : $#$_;
Branch: 1 branch
BranchTrueFalseTotal
@$_ % 2 ? : 0 8 8
153 8 push @sf, @{$_}[2 .. $l];
154 }
155 5 return Hash::MultiValue->new(@sf);
156 }
157  
158 78 ▶sub pica_value {
Subroutine pica_value: called CC=7 SCAR=19.7
Pod documented
159 78 my ($record, $path) = @_;
160  
161 78 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 78 0 78
162 78 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 78 0 78
163 78 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 78 78
164  
165 78 foreach my $field (@$record) {
166 182 ▶ next unless $path->match_field($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $path->match_field($field) 161 21 182
167 21 my @values = $path->match_subfields($field);
168 21 ▶ return $values[0] if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 19 2 21
169 }
170  
171 59 return;
172 }
173  
174 177 ▶sub pica_items {
Subroutine pica_items: called CC=6 SCAR=17.9
Pod documented
175 177 my $holdings = pica_holdings(@_);
176  
177 177 my @items;
178 177 foreach (@$holdings) {
179 231 my @fields = grep {$_->[0] =~ /^2/} @{$_->{record}};
180  
181 231 ▶ while (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 1421 231 1652
182 1421 my (@record, $epn);
183 1421 my $occ = 1 * $fields[0]->[1];
184 1421 ▶ while (@fields && 1 * $fields[0]->[1] == $occ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields and 1 * $fields[0][1] == $occ) 11422 1421 12843
Condition100%3/3
@fields and 1 * $fields[0][1] == $occ
!ll&&!rl&&r
231119011422
MC/DC100%2/2
@fields and 1 * $fields[0][1] == $occ
@fields 1 * $fields[0][1] == $occ
Truth table100%3/3
A and B
A= @fieldsB= 1 * $fields[0][1] == $occ
ABresult
0X0
100
111
185 11422 ▶ if ($fields[0]->[0] eq '203@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields[0][0] eq "203\@") 1416 10006 11422
186 1416 ($epn) = $EPN_PATH->match_subfields($fields[0]);
187 }
188 11422 push @record, shift @fields;
189 }
190 1421 push @items, bless {record => \@record, _id => $epn},
191 'PICA::Data';
192 }
193 }
194  
195 177 return \@items;
196 }
197  
198 42 ▶sub pica_sort {
Subroutine pica_sort: called CC=2 SCAR=6.9
Pod documented
199 42 my ($record) = $_[0];
200  
201 42 my $sorted = pica_title($record);
202  
203 42 for my $holding (@{pica_holdings($record)}) {
204 1 push @{$sorted->{record}}, @{$holding->{record}},;
205 }
206  
207 42 return $sorted;
208 }
209  
210 576 ▶sub sort_fields {
Subroutine sort_fields: called CC=1 SCAR=0.0
Pod undocumented
211 576 sort {sprintf("%s/%02d", @$a) cmp sprintf("%s/%02d", @$b)} @{$_[0]};
212 }
213  
214 27336 ▶sub cmp_level2 {
Subroutine cmp_level2: called CC=2 SCAR=6.9
Pod undocumented
215 27336 my ($occA, $occB) = map {1 * $_->[1]} @_;
216 27336 ▶ return $occA == $occB ? $_[0]->[0] cmp $_[1]->[0] : $occA <=> $occB;
Branch: 1 branch
BranchTrueFalseTotal
$occA == $occB ? : 24212 3124 27336
217 }
218  
219 68 ▶sub pica_title {
Subroutine pica_title: called CC=2 SCAR=6.9
Pod documented
220 68 my ($fields) = @_;
221  
222 68 my $record = {record => [sort_fields(pica_fields($_[0], "0.../*"))]};
223  
224 68 my $ppn = pica_value($record, '003@0');
225 68 ▶ $record->{_id} = $ppn if defined $ppn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ppn 10 58 68
226  
227 68 return bless $record, 'PICA::Data';
228 }
229  
230 249 ▶sub pica_holdings {
Subroutine pica_holdings: called CC=14 SCAR=26.4
Pod documented
231  
232 # ignore level 0 fields
233 27337 my @fields = grep {$_->[0] =~ /^[12]/}
234 249 ▶ @{reftype $_[0] eq 'HASH' ? $_[0]->{record} : $_[0]};
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 219 30 249
235  
236 249 my @holdings;
237  
238 # level 2 fields without preceding level 1
239 249 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 187 62 249
240 187 my @item;
241 187 ▶ while (@fields && $fields[0]->[0] =~ /^2/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields and $fields[0][0] =~ /^2/u) 22 187 209
Condition100%3/3
@fields and $fields[0][0] =~ /^2/u
!ll&&!rl&&r
917822
MC/DC100%2/2
@fields and $fields[0][0] =~ /^2/u
@fields $fields[0][0] =~ /^2/u
Truth table100%3/3
A and B
A= @fieldsB= $fields[0][0] =~ /^2/u
ABresult
0X0
100
111
242 22 push @item, shift @fields;
243 }
244 187 ▶ if (@item) {
Branch: 1 branch
BranchTrueFalseTotal
if (@item) 9 178 187
245 9 push @holdings, {record => [sort {cmp_level2($a, $b)} @item]};
246 }
247 }
248  
249 249 ▶ while (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 508 249 757
250 508 my $iln;
251 508 my (@level1, @level2);
252  
253 # consecutive level 1 fields (possibly split by multiple 101@)
254 508 ▶ while (@fields && $fields[0]->[0] =~ /^1/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields and $fields[0][0] =~ /^1/u) 1345 508 1853
Condition100%3/3
@fields and $fields[0][0] =~ /^1/u
!ll&&!rl&&r
15071345
MC/DC100%2/2
@fields and $fields[0][0] =~ /^1/u
@fields $fields[0][0] =~ /^1/u
Truth table100%3/3
A and B
A= @fieldsB= $fields[0][0] =~ /^1/u
ABresult
0X0
100
111
255 1345 my $field = shift @fields;
256  
257 1345 ▶ if ($field->[0] eq '101@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[0] eq "101\@") 507 838 1345
258 507 ▶ if (defined $iln) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iln) 0 507 507
259 0 push @holdings,
260 {record => [sort_fields(\@level1)], _id => $iln};
261 0 @level1 = ();
262 }
263 507 ($iln) = $ILN_PATH->match_subfields($field);
264 }
265  
266 1345 push @level1, $field;
267 }
268  
269 #@level1 = sort_fields(\@level1) if @level1;
270  
271 508 ▶ while (@fields && $fields[0]->[0] =~ /^2/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields and $fields[0][0] =~ /^2/u) 25641 508 26149
Condition100%3/3
@fields and $fields[0][0] =~ /^2/u
!ll&&!rl&&r
17833025641
MC/DC100%2/2
@fields and $fields[0][0] =~ /^2/u
@fields $fields[0][0] =~ /^2/u
Truth table100%3/3
A and B
A= @fieldsB= $fields[0][0] =~ /^2/u
ABresult
0X0
100
111
272 25641 push @level2, shift @fields;
273 }
274  
275 push @holdings,
276 {
277 record =>
278 508 [sort_fields(\@level1), sort {cmp_level2($a, $b)} @level2],
279 _id => $iln
280 };
281 }
282  
283 249 bless $_, 'PICA::Data' for @holdings;
284  
285 249 return \@holdings;
286 }
287  
288 28 ▶sub pica_split {
Subroutine pica_split: called CC=7 SCAR=19.5
Pod documented
289 28 my ($record, $level) = @_;
290  
291 28 my @records;
292 28 ▶ @records = pica_title($record) unless $level > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $level > 0 26 2 28
293 28 ▶ return @records if $level eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $level eq 0 1 27 28
294  
295 27 my @ppn = @{pica_fields($record, '003@')};
296  
297 27 for my $hold (@{pica_holdings($record)}) {
298 228 ▶ if ($level eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($level eq '1') { } 56 172 228
299 56 unshift @{$hold->{record}}, @ppn;
300 56 push @records, $hold;
301 }
302 else {
303 172 my $items = pica_items($hold);
304 172 ▶ if ($level eq '2') {
Branch: 1 branch
BranchTrueFalseTotal
if ($level eq '2') { } 56 116 172
305 56 my @iln = @{pica_fields($hold, '101@')};
306 56 for my $item (@$items) {
307 353 unshift @{$item->{record}}, @ppn, @iln;
308 353 push @records, $item;
309 }
310 }
311 else {
312 # limit holding record to level1 fields
313 $hold->{record}
314 116 = [grep {substr($_->[0], 0, 1) eq 1} @{$hold->{record}}];
315 116 push @records, $hold, @$items;
316 }
317 }
318 }
319  
320 27 return grep {@{pica_fields($_)} > 0} @records;
321 }
322  
323 47 ▶sub pica_string {
Subroutine pica_string: called CC=3 SCAR=11.0
Pod documented
324 47 my ($pica, $type, %options) = @_;
325 47 my $string = "";
326 47 ▶ $type ||= 'plain';
Condition100%2/2
$type ||= "plain"
l!l
1532
MC/DC100%1/1
$type ||= "plain"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
327 47 $options{fh} = \$string;
328 47 ▶ $options{start} //= 0;
Condition50%1/2
$options{"start"} //= 0
l!l
047
MC/DC0%0/1
$options{"start"} //= 0
$options{"start"}
Truth table50%1/2
A
A= $options{"start"}
Aresult
11
00
329 47 pica_writer($type => %options)->write($pica)->end;
330 47 return decode('UTF-8', $string);
331 }
332  
333 2 ▶sub pica_id {
Subroutine pica_id: called CC=2 SCAR=7.6
Pod undocumented
334 2 ▶ return $_[0]->{_id} if reftype $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($_[0]) eq "HASH" 2 0 2
335 }
336  
337 2 ▶sub pica_empty {
Subroutine pica_empty: called CC=2 SCAR=6.9
Pod undocumented
338 2 ▶ my $fields = reftype $_[0] eq 'HASH' ? $_[0]->{record} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 1 1 2
339 2 return !@$fields;
340 }
341  
342 47 ▶sub pica_annotation {
Subroutine pica_annotation: called CC=1 SCAR=0.0
Pod documented
343 47 return PICA::Data::Field::annotation(@_);
344 }
345  
346 16 *parse_subfield_schedule = *PICA::Schema::parse_subfield_schedule;
347  
348 3 ▶sub pica_sort_subfields {
Subroutine pica_sort_subfields: called CC=7 SCAR=19.5
Pod documented
349 3 my ($field, $schedule) = @_;
350 3 ▶ $schedule = parse_subfield_schedule($schedule) unless ref $schedule;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schedule 3 0 3
351  
352 9 my @spec = grep {exists $_->{order}} map {
353 3 {%{$schedule->{$_}}, code => $_}
354 } keys %$schedule;
355  
356 3 my $sf = pica_subfields([$field]);
357 3 splice @$field, 2;
358  
359 3 for my $sfdef (sort {$a->{order} cmp $b->{order}} @spec) {
360 9 my $code = $sfdef->{code};
361 9 my @values = $sf->get_all($code);
362 9 ▶ next unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 3 6 9
363  
364 6 ▶ if ($sfdef->{repeatable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sfdef->{'repeatable'}) { } 1 5 6
365 1 push @$field, $code, $_ for @values;
366 }
367 else {
368 5 push @$field, $code, $values[0];
369 }
370 }
371  
372 3 ▶ return @$field > 2 ? $field : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$field > 2 ? : 2 1 3
373 }
374  
375 16 *fields = *pica_fields;
376 16 *subfields = *pica_subfields;
377 16 *title = *pica_title;
378 16 *holdings = *pica_holdings;
379 16 *items = *pica_items;
380 16 *sort = *pica_sort;
381 16 *split = *pica_split;
382 16 *match = *pica_match;
383 16 *value = *pica_value;
384 16 *values = *pica_values;
385 16 *string = *pica_string;
386 16 *id = *pica_id;
387 16 *empty = *pica_empty;
388 16 *diff = *pica_diff = *PICA::Patch::pica_diff;
389 16 *patch = *pica_patch = *PICA::Patch::pica_patch;
390 16 *append = *pica_append;
391 16 *remove = *pica_remove;
392 16 *update = *pica_update;
393  
394 16 ▶use PICA::Data::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
395 16 ▶use PICA::Patch;
Subroutine BEGIN: called CC=1 SCAR=0.0
396 16 ▶use PICA::Parser::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
397 16 ▶use PICA::Parser::Plus;
Subroutine BEGIN: called CC=1 SCAR=0.0
398 16 ▶use PICA::Parser::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
399 16 ▶use PICA::Parser::Patch;
Subroutine BEGIN: called CC=1 SCAR=0.0
400 16 ▶use PICA::Parser::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
401 16 ▶use PICA::Parser::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
402 16 ▶use PICA::Parser::PPXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
403 16 ▶use PICA::Parser::PIXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
404 16 ▶use PICA::Parser::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
405 16 ▶use PICA::Writer::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
406 16 ▶use PICA::Writer::Plus;
Subroutine BEGIN: called CC=1 SCAR=0.0
407 16 ▶use PICA::Writer::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
408 16 ▶use PICA::Writer::Patch;
Subroutine BEGIN: called CC=1 SCAR=0.0
409 16 ▶use PICA::Writer::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
410 16 ▶use PICA::Writer::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
411 16 ▶use PICA::Writer::PPXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 16 ▶use PICA::Writer::PIXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
413 16 ▶use PICA::Writer::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 16 ▶use PICA::Writer::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
415  
416 108 ▶sub pica_parser {
Subroutine pica_parser: called CC=1 SCAR=0.0
Pod documented
417 108 _pica_module('PICA::Parser', @_);
418 }
419  
420 47 ▶sub pica_writer {
Subroutine pica_writer: called CC=2 SCAR=9.2
Pod documented
421 47 ▶ if (lc $_[0] eq 'generic') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $_[0] eq "generic") 0 47 47
422 0 shift;
423 0 return PICA::Writer::Generic->new(@_);
424 }
425 47 _pica_module('PICA::Writer', @_);
426 }
427  
428 1 ▶sub pica_path {
Subroutine pica_path: called CC=1 SCAR=0.0
Pod documented
429 1 PICA::Path->new(@_);
430 }
431  
432 14 ▶sub pica_guess {
Subroutine pica_guess: called CC=8 SCAR=20.8
Pod documented
433 14 my ($pica) = @_;
434  
435 14 my $format = '';
436 14 my %count = (
437 '' => 0,
438 'Plain' => ($pica =~ tr/$//),
439 'Binary' => ($pica =~ tr/\x{1F}//),
440 'XML' => ($pica =~ tr/<//),
441 'JSON' => ($pica =~ tr/[{[]//),
442 'NL' => ($pica =~ tr/[\r\n]//),
443 'IS3' => ($pica =~ tr/\x{1D}//),
444 );
445 $count{$_} > $count{$format} and $format = $_
446 14 ▶ for qw(Plain Binary XML JSON);
Branch: 1 branch
BranchTrueFalseTotal
$count{$_} > $count{$format} and $format = $_ 13 43 56
447  
448 14 ▶ if ($format eq 'Binary') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'Binary') { } 4 10 14
elsif ($format eq 'XML') { } 1 9 10
449 4 ▶ $format = 'Plus' if $count{NL} > $count{IS3};
Branch: 1 branch
BranchTrueFalseTotal
if $count{"NL"} > $count{"IS3"} 2 2 4
450 }
451 elsif ($format eq 'XML') {
452 1 ▶ $format = 'PPXML' if $pica =~ qr{xmlns/ppxml-1\.0};
Branch: 1 branch
BranchTrueFalseTotal
if $pica =~ qr"xmlns/ppxml-1\.0"u 0 1 1
453 }
454  
455 14 ▶ $format ? "PICA::Parser::$format" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$format ? : 13 1 14
456 }
457  
458 155 ▶sub _pica_module {
Subroutine _pica_module: called CC=11 SCAR=24.0
459 155 my $base = shift;
460 155 ▶ my $type = lc(shift) // '';
Condition50%1/2
lc shift() // ""
l!l
1550
MC/DC0%0/1
lc shift() // ""
lc shift()
Truth table50%1/2
A
A= lc shift()
Aresult
11
00
461  
462 155 ▶ if ($type =~ /^(pica)?plus|norm(alized)$/) {
Branch: 9 branches
BranchTrueFalseTotal
if ($type =~ /^(pica)?plus|norm(alized)$/u) { } 5 150 155
elsif ($type eq 'binary') { } 4 146 150
elsif ($type =~ /^(pica)?plain$/u) { } 118 28 146
elsif ($type =~ /^patch$/u) { } 2 26 28
elsif ($type eq 'import') { } 3 23 26
elsif ($type =~ /^(pica)?xml$/u) { } 9 14 23
elsif ($type =~ /^(pica)?ppxml$/u) { } 3 11 14
elsif ($type =~ /^pixml$/u) { } 3 8 11
elsif ($type =~ /^(nd)?json$/u) { } 7 1 8
463 5 "${base}::Plus"->new(@_);
464 }
465 elsif ($type eq 'binary') {
466 4 "${base}::Binary"->new(@_);
467 }
468 elsif ($type =~ /^(pica)?plain$/) {
469 118 "${base}::Plain"->new(@_);
470 }
471 elsif ($type =~ /^patch$/) {
472 2 "${base}::Patch"->new(@_);
473 }
474 elsif ($type eq 'import') {
475 3 "${base}::Import"->new(@_);
476 }
477 elsif ($type =~ /^(pica)?xml$/) {
478 9 "${base}::XML"->new(@_);
479 }
480 elsif ($type =~ /^(pica)?ppxml$/) {
481 3 "${base}::PPXML"->new(@_);
482 }
483 elsif ($type =~ /^pixml$/) {
484 3 "${base}::PIXML"->new(@_);
485 }
486 elsif ($type =~ /^(nd)?json$/) {
487 7 "${base}::JSON"->new(@_);
488 }
489 else {
490 1 croak "unknown PICA parser type: $type";
491 }
492 }
493  
494 0 ▶sub write {
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
495 0 my $pica = shift;
496 0 my $writer = $_[0];
497 0 ▶ $writer = pica_writer(@_ ? @_ : 'plain') unless blessed $writer;
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($writer) 0 0 0
@_ ? : 0 0 0
498 0 $writer->write($pica);
499 }
500  
501 2 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=3 SCAR=11.4
Pod undocumented
502 2 my $record = shift;
503 2 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 2 0 2
504 2 ▶ return [map {blessed $_ ? $_->TO_JSON : $_} @$record];
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 2 0 2
505 }
506  
507 0 ▶sub pica_xml_struct {
Subroutine pica_xml_struct: not called CC=3 SCAR=24.8
Pod documented
508 0 my ($xml, %options) = @_;
509 0 my $record;
510  
511 0 foreach my $f (@{$xml->[2]}) {
512 0 ▶ next unless $f->[0] eq 'datafield';
Branch: 1 branch
BranchTrueFalseTotal
unless $f->[0] eq "datafield" 0 0 0
513 push @$record,
514 [
515 0 map ({$f->[1]->{$_}} qw(tag occurrence)),
516 0 map ({$_->[1]->{code} => $_->[2]->[0]} @{$f->[2]})
517 ];
518 }
519  
520 0 my ($id) = map {$_->[-1]} grep {$_->[0] =~ '003@'} @$record;
521 0 return bless {_id => $id, record => $record}, 'PICA::Data';
522 }
523  
524 16 1;
525 __END__
« PICA/Parser/Import.pm ↑ Summary PICA/Parser/Base.pm »