blib/lib/PICA/Data.pm

filter: Statementstmt 93.6% 326 / 348 Branchbran 82.6% 129 / 156 Conditioncond 84.0% 21 / 25 MC/DCmcdc 73.3% 11 / 15 Subroutinesub 96.6% 57 / 59 Podpod 80.0% 24 / 30 totaltotal 89.7% 568 / 633 CC 100 SCAR 20.0
CC
100
Cov
90%
pica_update
20.3
pica_holdings
14.0
write
12.0
CRAP 7.4
« blib/lib/PICA/Writer/Import.pm ↑ Summary blib/lib/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.09';
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 sub new {
30 8 ▶ my ($class, $data) = @_;
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
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") 0 7 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 sub pica_data {
43 6 ▶ PICA::Data->new(@_);
Subroutine pica_data: called CC=1 SCAR=0.0
Pod documented
44 }
45  
46 sub pica_field {
47 7 ▶ PICA::Data::Field->new(@_);
Subroutine pica_field: called CC=1 SCAR=0.0
Pod documented
48 }
49  
50 sub pica_match {
51 59 ▶ my ($record, $path, %args) = @_;
Subroutine pica_match: called CC=3 SCAR=11.0
Pod documented
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 sub pica_values {
60 11 ▶ my ($record, $path) = @_;
Subroutine pica_values: called CC=3 SCAR=11.0
Pod documented
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 sub pica_fields {
69 1466 ▶ my $record = shift;
Subroutine pica_fields: called CC=5 SCAR=16.1
Pod documented
70 1466 ▶ croak "missing record. Did you mean pica_field instead of pica_fields?"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $record 0 1466 1466
71 unless ref $record;
72  
73 1466 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 1411 55 1466
74  
75 1466 ▶ return [@$record] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1311 155 1466
76  
77 155 my $matcher = eval {pica_field_matcher(@_)};
78 155 ▶ return [] unless $matcher;
Branch: 1 branch
BranchTrueFalseTotal
unless $matcher 1 154 155
79  
80 154 return [grep {$matcher->($_)} @$record];
81 }
82  
83 sub pica_append {
84 3 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{record} : shift;
Subroutine pica_append: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 3 0 3
85 3 push @$fields, pica_field(@_);
86 }
87  
88 sub pica_remove {
89 1 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{record} : shift;
Subroutine pica_remove: called CC=2 SCAR=7.0
Pod documented
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 sub pica_update {
97 7 ▶ my $fields = reftype $_[0] eq 'HASH' ? shift->{record} : shift;
Subroutine pica_update: called CC=20 SCAR=30.1
Pod documented
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") 1 5 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 sub pica_subfields {
150 5 ▶ my @sf;
Subroutine pica_subfields: called CC=3 SCAR=11.0
Pod documented
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 sub pica_value {
159 75 ▶ my ($record, $path) = @_;
Subroutine pica_value: called CC=7 SCAR=19.7
Pod documented
160  
161 75 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 75 0 75
162 75 ▶ $path = eval {PICA::Path->new($path)} unless ref $path;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path 75 0 75
163 75 ▶ return unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 75 75
164  
165 75 foreach my $field (@$record) {
166 172 ▶ next unless $path->match_field($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $path->match_field($field) 152 20 172
167 20 my @values = $path->match_subfields($field);
168 20 ▶ return $values[0] if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 18 2 20
169 }
170  
171 57 return;
172 }
173  
174 sub pica_items {
175 177 ▶ my $holdings = pica_holdings(@_);
Subroutine pica_items: called CC=6 SCAR=17.9
Pod documented
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 sub pica_sort {
199 40 ▶ my ($record) = $_[0];
Subroutine pica_sort: called CC=2 SCAR=6.9
Pod documented
200  
201 40 my $sorted = pica_title($record);
202  
203 40 for my $holding (@{pica_holdings($record)}) {
204 1 push @{$sorted->{record}}, @{$holding->{record}},;
205 }
206  
207 40 return $sorted;
208 }
209  
210 sub sort_fields {
211 574 ▶ sort {sprintf("%s/%02d", @$a) cmp sprintf("%s/%02d", @$b)} @{$_[0]};
Subroutine sort_fields: called CC=1 SCAR=0.0
Pod undocumented
212 }
213  
214 sub cmp_level2 {
215 27336 ▶ my ($occA, $occB) = map {1 * $_->[1]} @_;
Subroutine cmp_level2: called CC=2 SCAR=6.9
Pod undocumented
216 27336 ▶ return $occA == $occB ? $_[0]->[0] cmp $_[1]->[0] : $occA <=> $occB;
Branch: 1 branch
BranchTrueFalseTotal
$occA == $occB ? : 24212 3124 27336
217 }
218  
219 sub pica_title {
220 66 ▶ my ($fields) = @_;
Subroutine pica_title: called CC=2 SCAR=6.9
Pod documented
221  
222 66 my $record = {record => [sort_fields(pica_fields($_[0], "0.../*"))]};
223  
224 66 my $ppn = pica_value($record, '003@0');
225 66 ▶ $record->{_id} = $ppn if defined $ppn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ppn 10 56 66
226  
227 66 return bless $record, 'PICA::Data';
228 }
229  
230 sub pica_holdings {
231  
232 # ignore level 0 fields
233 27334 my @fields = grep {$_->[0] =~ /^[12]/}
234 247 ▶ @{reftype $_[0] eq 'HASH' ? $_[0]->{record} : $_[0]};
Subroutine pica_holdings: called CC=14 SCAR=26.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 217 30 247
235  
236 247 my @holdings;
237  
238 # level 2 fields without preceding level 1
239 247 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 187 60 247
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 247 ▶ while (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 508 247 755
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 247 bless $_, 'PICA::Data' for @holdings;
284  
285 247 return \@holdings;
286 }
287  
288 sub pica_split {
289 28 ▶ my ($record, $level) = @_;
Subroutine pica_split: called CC=7 SCAR=19.5
Pod documented
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 sub pica_string {
324 46 ▶ my ($pica, $type, %options) = @_;
Subroutine pica_string: called CC=3 SCAR=11.0
Pod documented
325 46 my $string = "";
326 46 ▶ $type ||= 'plain';
Condition100%2/2
$type ||= "plain"
l!l
1432
MC/DC100%1/1
$type ||= "plain"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
327 46 $options{fh} = \$string;
328 46 ▶ $options{start} //= 0;
Condition50%1/2
$options{"start"} //= 0
l!l
046
MC/DC0%0/1
$options{"start"} //= 0
$options{"start"}
Truth table50%1/2
A
A= $options{"start"}
Aresult
11
00
329 46 pica_writer($type => %options)->write($pica)->end;
330 46 return decode('UTF-8', $string);
331 }
332  
333 sub pica_id {
334 2 ▶ return $_[0]->{_id} if reftype $_[0] eq 'HASH';
Subroutine pica_id: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($_[0]) eq "HASH" 2 0 2
335 }
336  
337 sub pica_empty {
338 2 ▶ my $fields = reftype $_[0] eq 'HASH' ? $_[0]->{record} : $_[0];
Subroutine pica_empty: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($_[0]) eq 'HASH' ? : 1 1 2
339 2 return !@$fields;
340 }
341  
342 sub pica_annotation {
343 47 ▶ return PICA::Data::Field::annotation(@_);
Subroutine pica_annotation: called CC=1 SCAR=0.0
Pod documented
344 }
345  
346 16 *parse_subfield_schedule = *PICA::Schema::parse_subfield_schedule;
347  
348 sub pica_sort_subfields {
349 3 ▶ my ($field, $schedule) = @_;
Subroutine pica_sort_subfields: called CC=7 SCAR=19.5
Pod documented
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::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
400 16 ▶use PICA::Parser::PPXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
401 16 ▶use PICA::Parser::PIXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
402 16 ▶use PICA::Parser::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
403 16 ▶use PICA::Writer::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
404 16 ▶use PICA::Writer::Plus;
Subroutine BEGIN: called CC=1 SCAR=0.0
405 16 ▶use PICA::Writer::Plain;
Subroutine BEGIN: called CC=1 SCAR=0.0
406 16 ▶use PICA::Writer::Import;
Subroutine BEGIN: called CC=1 SCAR=0.0
407 16 ▶use PICA::Writer::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
408 16 ▶use PICA::Writer::PPXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
409 16 ▶use PICA::Writer::PIXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
410 16 ▶use PICA::Writer::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
411 16 ▶use PICA::Writer::Generic;
Subroutine BEGIN: called CC=1 SCAR=0.0
412  
413 sub pica_parser {
414 100 ▶ _pica_module('PICA::Parser', @_);
Subroutine pica_parser: called CC=1 SCAR=0.0
Pod documented
415 }
416  
417 sub pica_writer {
418 46 ▶ if (lc $_[0] eq 'generic') {
Subroutine pica_writer: called CC=2 SCAR=9.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (lc $_[0] eq "generic") 0 46 46
419 0 shift;
420 0 return PICA::Writer::Generic->new(@_);
421 }
422 46 _pica_module('PICA::Writer', @_);
423 }
424  
425 sub pica_path {
426 1 ▶ PICA::Path->new(@_);
Subroutine pica_path: called CC=1 SCAR=0.0
Pod documented
427 }
428  
429 sub pica_guess {
430 14 ▶ my ($pica) = @_;
Subroutine pica_guess: called CC=8 SCAR=20.8
Pod documented
431  
432 14 my $format = '';
433 14 my %count = (
434 '' => 0,
435 'Plain' => ($pica =~ tr/$//),
436 'Binary' => ($pica =~ tr/\x{1F}//),
437 'XML' => ($pica =~ tr/<//),
438 'JSON' => ($pica =~ tr/[{[]//),
439 'NL' => ($pica =~ tr/[\r\n]//),
440 'IS3' => ($pica =~ tr/\x{1D}//),
441 );
442 $count{$_} > $count{$format} and $format = $_
443 14 ▶ for qw(Plain Binary XML JSON);
Branch: 1 branch
BranchTrueFalseTotal
$count{$_} > $count{$format} and $format = $_ 13 43 56
444  
445 14 ▶ if ($format eq 'Binary') {
Branch: 2 branches
BranchTrueFalseTotal
if ($format eq 'Binary') { } 4 10 14
elsif ($format eq 'XML') { } 1 9 10
446 4 ▶ $format = 'Plus' if $count{NL} > $count{IS3};
Branch: 1 branch
BranchTrueFalseTotal
if $count{"NL"} > $count{"IS3"} 2 2 4
447 }
448 elsif ($format eq 'XML') {
449 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
450 }
451  
452 14 ▶ $format ? "PICA::Parser::$format" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$format ? : 13 1 14
453 }
454  
455 sub _pica_module {
456 146 ▶ my $base = shift;
Subroutine _pica_module: called CC=10 SCAR=23.1
457 146 ▶ my $type = lc(shift) // '';
Condition50%1/2
lc shift() // ""
l!l
1460
MC/DC0%0/1
lc shift() // ""
lc shift()
Truth table50%1/2
A
A= lc shift()
Aresult
11
00
458  
459 146 ▶ if ($type =~ /^(pica)?plus|norm(alized)$/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type =~ /^(pica)?plus|norm(alized)$/u) { } 5 141 146
elsif ($type eq 'binary') { } 4 137 141
elsif ($type =~ /^(pica)?plain$/u) { } 114 23 137
elsif ($type eq 'import') { } 0 23 23
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
460 5 "${base}::Plus"->new(@_);
461 }
462 elsif ($type eq 'binary') {
463 4 "${base}::Binary"->new(@_);
464 }
465 elsif ($type =~ /^(pica)?plain$/) {
466 114 "${base}::Plain"->new(@_);
467 }
468 elsif ($type eq 'import') {
469 0 "${base}::Import"->new(@_);
470 }
471 elsif ($type =~ /^(pica)?xml$/) {
472 9 "${base}::XML"->new(@_);
473 }
474 elsif ($type =~ /^(pica)?ppxml$/) {
475 3 "${base}::PPXML"->new(@_);
476 }
477 elsif ($type =~ /^pixml$/) {
478 3 "${base}::PIXML"->new(@_);
479 }
480 elsif ($type =~ /^(nd)?json$/) {
481 7 "${base}::JSON"->new(@_);
482 }
483 else {
484 1 croak "unknown PICA parser type: $type";
485 }
486 }
487  
488 sub write {
489 0 ▶ my $pica = shift;
Subroutine write: not called CC=3 SCAR=24.8
Pod documented
490 0 my $writer = $_[0];
491 0 ▶ $writer = pica_writer(@_ ? @_ : 'plain') unless blessed $writer;
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($writer) 0 0 0
@_ ? : 0 0 0
492 0 $writer->write($pica);
493 }
494  
495 sub TO_JSON {
496 2 ▶ my $record = shift;
Subroutine TO_JSON: called CC=3 SCAR=11.4
Pod undocumented
497 2 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 2 0 2
498 2 ▶ return [map {blessed $_ ? $_->TO_JSON : $_} @$record];
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 2 0 2
499 }
500  
501 sub pica_xml_struct {
502 0 ▶ my ($xml, %options) = @_;
Subroutine pica_xml_struct: not called CC=3 SCAR=24.8
Pod documented
503 0 my $record;
504  
505 0 foreach my $f (@{$xml->[2]}) {
506 0 ▶ next unless $f->[0] eq 'datafield';
Branch: 1 branch
BranchTrueFalseTotal
unless $f->[0] eq "datafield" 0 0 0
507 push @$record,
508 [
509 0 map ({$f->[1]->{$_}} qw(tag occurrence)),
510 0 map ({$_->[1]->{code} => $_->[2]->[0]} @{$f->[2]})
511 ];
512 }
513  
514 0 my ($id) = map {$_->[-1]} grep {$_->[0] =~ '003@'} @$record;
515 0 return bless {_id => $id, record => $record}, 'PICA::Data';
516 }
517  
518 16 1;
519 __END__
« blib/lib/PICA/Writer/Import.pm ↑ Summary blib/lib/PICA/Parser/Base.pm »