blib/lib/PICA/Schema.pm

filter: Statementstmt 86.5% 148 / 171 Branchbran 86.8% 106 / 122 Conditioncond 74.2% 52 / 70 MC/DCmcdc 61.9% 26 / 42 Subroutinesub 82.3% 14 / 17 Podpod 66.6% 6 / 9 totaltotal 81.6% 352 / 431 CC 96 SCAR 30.5
CC
96
Cov
84%
check_field
37.9
abbreviated
30.0
clean_pica
22.1
CRAP 21.0
« blib/lib/PICA/Writer/Fields.pm ↑ Summary blib/lib/PICA/Writer/XML.pm »
1 package PICA::Schema;
2 10 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 10 our $VERSION = '1.18';
5  
6 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 our @EXPORT_OK = qw(field_identifier check_value clean_pica);
8  
9 10 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use PICA::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 13 ▶ my ($class, $schema) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
15 13 bless $schema, $class;
16 }
17  
18 sub check {
19 23 ▶ my ($self, $record, %options) = @_;
Subroutine check: called CC=9 SCAR=22.0
Pod documented
20  
21 23 my @errors;
22 $record
23 23 ▶ = clean_pica($record, error => sub {push @errors, shift}, %options)
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$record = clean_pica($record, "error", sub { push @errors, shift(); } , %options) or return @errors 1 22 23
24 or return @errors;
25  
26 22 $options{counter} = {};
27  
28 22 my %field_identifiers;
29 22 for my $field (@$record) {
30 3062 my $id = $self->field_identifier($field);
31 3062 $field_identifiers{$id} = 1;
32 3062 my $error = $self->check_field($field, %options, _field_id => $id);
33 3062 ▶ if ($error and !grep {$_ eq $error} @errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and not grep({$_ eq $error;} @errors)) 88 2974 3062
Condition100%3/3
$error and not grep({$_ eq $error;} @errors)
!ll&&!rl&&r
15295988
MC/DC0%0/2
$error and not grep({$_ eq $error;} @errors)
$error not grep({$_ eq $error;} @errors)
Truth table33%1/3
A and not(B)
A= $errorB= not grep({$_ eq $error;} @errors)
ABresult
0X0
100
111
34 88 push @errors, $error;
35 }
36 }
37  
38 # check whether required fields exist
39 22 for my $id (keys %{$self->{fields}}) {
40 23 my $field = $self->{fields}{$id};
41 23 ▶ if ($field->{required} && !$field_identifiers{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"required"} and not $field_identifiers{$id}) 2 21 23
Condition100%3/3
$field->{"required"} and not $field_identifiers{$id}
!ll&&!rl&&r
1382
MC/DC100%2/2
$field->{"required"} and not $field_identifiers{$id}
$field->{"required"} not $field_identifiers{$id}
Truth table100%3/3
A and not(B)
A= $field->{"required"}B= not $field_identifiers{$id}
ABresult
0X0
100
111
42 2 ▶ push @errors,
Branch: 1 branch
BranchTrueFalseTotal
length $id > 4 ? : 1 1 2
43 PICA::Error->new(
44 [substr($id, 0, 4), length $id > 4 ? substr($id, 5) : undef],
45 required => 1
46 );
47 }
48 }
49  
50 22 return @errors;
51 }
52  
53 sub check_field {
54 3062 ▶ my ($self, $field, %opts) = @_;
Subroutine check_field: called CC=32 SCAR=36.4
Pod documented
55  
56 3062 ▶ my $id = $opts{_field_id} || $self->field_identifier($field);
Condition33%1/3
$opts{'_field_id'} || $self->field_identifier($field)
l!l&&r!l&&!r
306200
MC/DC0%0/2
$opts{'_field_id'} || $self->field_identifier($field)
$opts{'_field_id'} $self->field_identifier($field)
Truth table33%1/3
A || B
A= $opts{'_field_id'}B= $self->field_identifier($field)
ABresult
1X1
011
000
57 3062 my $spec = $self->{fields}{$id};
58  
59 3062 ▶ if ($opts{allow_deprecated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"allow_deprecated"}) 0 3062 3062
60 0 $opts{allow_deprecated_fields} = 1;
61 0 $opts{allow_deprecated_subfields} = 1;
62 }
63  
64 3062 ▶ if ($opts{ignore_unknown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"ignore_unknown"}) 1 3061 3062
65 1 $opts{ignore_unknown_fields} = 1;
66 1 $opts{ignore_unknown_subfields} = 1;
67 }
68  
69 3062 ▶ if (!$spec) { # field is not defined
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 3040 22 3062
70 3040 $spec = $self->{'deprecated-fields'}{$id};
71 3040 my $simple = [split('/', $id)];
72  
73 3040 ▶ if ($spec) { # field is deprectaed
Branch: 2 branches
BranchTrueFalseTotal
if ($spec) { } 0 3040 3040
elsif ($opts{'ignore_unknown_fields'}) { } 2 3038 3040
74 0 ▶ unless ($opts{allow_deprecated_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"allow_deprecated_fields"}) 0 0 0
75 0 return PICA::Error->new($simple, deprecated => 1);
76 }
77 }
78 elsif ($opts{ignore_unknown_fields}) {
79 2 return ();
80 }
81 else {
82 3038 return PICA::Error->new($simple);
83 }
84 }
85  
86 22 ▶ if ($opts{counter} && !$spec->{repeatable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"counter"} and not $spec->{"repeatable"}) 20 2 22
Condition66%2/3
$opts{"counter"} and not $spec->{"repeatable"}
!ll&&!rl&&r
0220
MC/DC50%1/2
$opts{"counter"} and not $spec->{"repeatable"}
$opts{"counter"} not $spec->{"repeatable"}
Truth table66%2/3
A and not(B)
A= $opts{"counter"}B= not $spec->{"repeatable"}
ABresult
0X0
100
111
87 20 my $tag_occ = join '/', grep {defined} @$field[0, 1];
88 20 ▶ if ($opts{counter}{$tag_occ}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"counter"}{$tag_occ}++) 2 18 20
89 2 return PICA::Error->new($field, repeated => 1);
90 }
91 }
92  
93 20 my $failed = check_annotation($field, %opts);
94 20 ▶ return PICA::Error->new($failed) if $failed;
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 20 20
95  
96 20 ▶ if ($opts{ignore_subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"ignore_subfields"}) 0 20 20
97 0 return ();
98 }
99  
100 20 my %errors;
101 20 ▶ if ($spec->{subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"subfields"}) 13 7 20
102 13 my $order;
103 my %sfcounter;
104 13 my (undef, undef, @subfields) = @$field;
105 13 ▶ pop @subfields if @subfields % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @subfields % 2 0 13 13
106  
107 13 ▶ while (@subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 17 13 30
108 17 my ($code, $value) = splice @subfields, 0, 2;
109 17 my $sfspec = $spec->{subfields}{$code};
110  
111 17 ▶ if (!$sfspec) { # subfield is not defined
Branch: 1 branch
BranchTrueFalseTotal
unless ($sfspec) 2 15 17
112 2 $sfspec = $spec->{'deprecated-subfields'}{$code};
113 2 ▶ if ($sfspec) { # subfield is deprecated
Branch: 2 branches
BranchTrueFalseTotal
if ($sfspec) { } 0 2 2
elsif (not $opts{'ignore_unknown_subfields'}) { } 1 1 2
114 0 ▶ unless ($opts{allow_deprecated_subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"allow_deprecated_subfields"}) 0 0 0
115 0 $errors{$code} = {deprecated => 1};
116 }
117 }
118 elsif (!$opts{ignore_unknown_subfields}) {
119 1 $errors{$code} = {};
120 }
121 }
122  
123 17 ▶ if ($sfspec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sfspec) 15 2 17
124 15 ▶ if (!$sfspec->{repeatable} && $sfcounter{$code}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $sfspec->{'repeatable'} and $sfcounter{$code}) { } 1 14 15
elsif (not $opts{'ignore_subfield_order'} and defined $sfspec->{'order'}) { } 8 8 16
Condition66%2/3
not $sfspec->{"repeatable"} and $sfcounter{$code}
!ll&&!rl&&r
0141
MC/DC50%1/2
not $sfspec->{"repeatable"} and $sfcounter{$code}
not $sfspec->{"repeatable"} $sfcounter{$code}
Truth table66%2/3
not(A) and B
A= not $sfspec->{"repeatable"}B= $sfcounter{$code}
ABresult
0X0
100
111
125 1 $errors{$code} = {repeated => 1};
126 }
127 ▶ elsif (!$opts{ignore_subfield_order}
Condition100%3/3
not $opts{"ignore_subfield_order"} and defined $sfspec->{"order"}
!ll&&!rl&&r
248
MC/DC100%2/2
not $opts{"ignore_subfield_order"} and defined $sfspec->{"order"}
not $opts{"ignore_subfield_order"} defined $sfspec->{"order"}
Truth table100%3/3
not(A) and B
A= not $opts{"ignore_subfield_order"}B= defined $sfspec->{"order"}
ABresult
0X0
100
111
128 && defined $sfspec->{order})
129 {
130 8 ▶ if (defined $order && $order > $sfspec->{order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $order and $order > $sfspec->{'order'}) { } 1 7 8
Condition100%3/3
defined $order and $order > $sfspec->{"order"}
!ll&&!rl&&r
611
MC/DC100%2/2
defined $order and $order > $sfspec->{"order"}
defined $order $order > $sfspec->{"order"}
Truth table100%3/3
A and B
A= defined $orderB= $order > $sfspec->{"order"}
ABresult
0X0
100
111
131 1 $errors{$code} = {order => $sfspec->{order}};
132 }
133 else {
134 7 $order = 1 * $sfspec->{order};
135 }
136 }
137 15 $sfcounter{$code}++;
138  
139 15 $errors{$code} = $_ for check_value($value, $sfspec, %opts);
140 }
141  
142 }
143  
144 13 foreach my $code (keys %{$spec->{subfields}}) {
145 25 ▶ if (!$sfcounter{$code} && $spec->{subfields}{$code}{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}) 1 24 25
Condition100%3/3
not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}
!ll&&!rl&&r
14101
MC/DC100%2/2
not $sfcounter{$code} and $spec->{"subfields"}{$code}{"required"}
not $sfcounter{$code} $spec->{"subfields"}{$code}{"required"}
Truth table100%3/3
not(A) and B
A= not $sfcounter{$code}B= $spec->{"subfields"}{$code}{"required"}
ABresult
0X0
100
111
146 1 $errors{$code} = {required => 1};
147 }
148 }
149 }
150  
151 20 ▶ return %errors ? PICA::Error->new($field, subfields => \%errors) : ();
Branch: 1 branch
BranchTrueFalseTotal
%errors ? : 7 13 20
152 }
153  
154 sub check_annotation {
155 3101 ▶ my ($field, %options) = @_;
Subroutine check_annotation: called CC=6 SCAR=17.9
Pod undocumented
156  
157 3101 my (undef, undef, @subfields) = @$field;
158  
159 3101 ▶ if (@subfields % 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@subfields % 2) { } 6 3095 3101
elsif ($options{'annotated'}) { } 1 3094 3095
160 return "Field annotation not allowed"
161 6 ▶ if defined $options{annotated} && !$options{annotation};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"annotated"} and not $options{"annotation"} 1 5 6
Condition66%2/3
defined $options{"annotated"} and not $options{"annotation"}
!ll&&!rl&&r
501
MC/DC50%1/2
defined $options{"annotated"} and not $options{"annotation"}
defined $options{"annotated"} not $options{"annotation"}
Truth table66%2/3
A and not(B)
A= defined $options{"annotated"}B= not $options{"annotation"}
ABresult
0X0
100
111
162  
163 5 ▶ return "Annotation must not be non-alphanumeric character"
Branch: 1 branch
BranchTrueFalseTotal
if not pop(@subfields) =~ /^[^A-Za-z0-9]\z/u 1 4 5
164 if pop(@subfields) !~ /^[^A-Za-z0-9]\z/;
165 }
166 elsif ($options{annotated}) {
167 1 return "Missing field annotation";
168 }
169 }
170  
171 sub check_value {
172 15 ▶ my ($value, $schedule, %opts) = @_;
Subroutine check_value: called CC=13 SCAR=27.3
Pod documented
173  
174 # TODO: check compatible with ECMA 262 (2015) regular expression grammar
175 15 ▶ if ($schedule->{pattern} and $value !~ /$schedule->{pattern}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($schedule->{"pattern"} and not $value =~ /$schedule->{"pattern"}/u) 1 14 15
Condition66%2/3
$schedule->{"pattern"} and not $value =~ /$schedule->{"pattern"}/u
!ll&&!rl&&r
1401
MC/DC50%1/2
$schedule->{"pattern"} and not $value =~ /$schedule->{"pattern"}/u
$schedule->{"pattern"} not $value =~ /$schedule->{"pattern"}/u
Truth table66%2/3
A and not(B)
A= $schedule->{"pattern"}B= not $value =~ /$schedule->{"pattern"}/u
ABresult
0X0
100
111
176 1 return {value => $value, pattern => $schedule->{pattern},};
177 }
178  
179 # check positions and codes
180 14 ▶ my $positions = $schedule->{positions} // {};
Condition100%2/2
$schedule->{"positions"} // {}
l!l
311
MC/DC100%1/1
$schedule->{"positions"} // {}
$schedule->{"positions"}
Truth table100%2/2
A
A= $schedule->{"positions"}
Aresult
11
00
181 14 foreach my $pos (keys %$positions) {
182 5 my @p = split '-', $pos;
183  
184 5 ▶ if (length $value < int $p[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $value < int $p[-1]) 1 4 5
185 1 return {value => $value, position => $pos,};
186 }
187  
188 4 my $def = $positions->{$pos};
189 4 ▶ if ($def->{codes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"codes"}) 3 1 4
190 3 my $codes = $def->{codes};
191 3 ▶ my $deprecated = $def->{'deprecated-codes'} // {};
Condition50%1/2
$def->{"deprecated-codes"} // {}
l!l
03
MC/DC0%0/1
$def->{"deprecated-codes"} // {}
$def->{"deprecated-codes"}
Truth table50%1/2
A
A= $def->{"deprecated-codes"}
Aresult
11
00
192 3 ▶ my $c = substr $value, $p[0] - 1,
Branch: 1 branch
BranchTrueFalseTotal
@p > 1 ? : 3 0 3
193 (@p > 1 ? $p[1] - $p[0] : 0) + 1;
194 3 ▶ if (!defined $codes->{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $codes->{$c}) 1 2 3
195 1 ▶ if (!$deprecated->{$c}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $deprecated->{$c}) { } 1 0 1
elsif (not $opts{'allow_deprecated'} || $opts{'allow_deprecated_codes'}) { } 0 0 0
196 1 return {value => $value, position => $pos};
197 }
198 ▶ elsif (!$opts{allow_deprecated}
Condition0%0/3
$opts{'allow_deprecated'} || $opts{'allow_deprecated_codes'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'allow_deprecated'} || $opts{'allow_deprecated_codes'}
$opts{'allow_deprecated'} $opts{'allow_deprecated_codes'}
Truth table0%0/3
A || B
A= $opts{'allow_deprecated'}B= $opts{'allow_deprecated_codes'}
ABresult
1X1
011
000
199 && !$opts{allow_deprecated_codes})
200 {
201 # TODO: there is no way to see that an invalid value is deprecated
202 0 return {value => $value, position => $pos};
203 }
204 }
205 }
206 }
207  
208 12 return;
209 }
210  
211 sub field_identifier {
212 3073 ▶ my $fields = ref $_[0] eq 'PICA::Schema' ? shift->{fields} : undef;
Subroutine field_identifier: called CC=13 SCAR=25.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'PICA::Schema' ? : 3064 9 3073
213 3073 my ($tag, $occ) = @{$_[0]};
214  
215 3073 ▶ $occ
Branch: 1 branch
BranchTrueFalseTotal
substr($tag, 0, 1) ne '2' && defined $occ && $occ ne '' ? : 23 3050 3073
Condition100%6/6
substr($tag, 0, 1) ne 2 and defined $occ and $occ ne ""
!ll&&!rl&&r
285419623
substr($tag, 0, 1) ne 2 and defined $occ
!ll&&!rl&&r
28513219
MC/DC100%3/3
substr($tag, 0, 1) ne 2 and defined $occ and $occ ne ""
substr($tag, 0, 1) ne 2 defined $occ $occ ne ""
Truth table100%4/4
A and B and C
A= substr($tag, 0, 1) ne 2B= defined $occC= $occ ne ""
ABCresult
0XX0
10X0
1100
1111
216 = (substr($tag, 0, 1) ne '2' && defined $occ && $occ ne '')
217 ? sprintf("%02d", $occ)
218 : '';
219  
220 3073 ▶ if ($fields && $occ ne '' && !exists $fields->{"$tag/$occ"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields and $occ ne "" and not exists $$fields{"$tag/$occ"}) 20 3053 3073
Condition100%6/6
$fields and $occ ne "" and not exists $$fields{"$tag/$occ"}
!ll&&!rl&&r
3052120
$fields and $occ ne ""
!ll&&!rl&&r
10304221
MC/DC100%3/3
$fields and $occ ne "" and not exists $$fields{"$tag/$occ"}
$fields $occ ne "" not exists $$fields{"$tag/$occ"}
Truth table100%4/4
A and B and not(C)
A= $fieldsB= $occ ne ""C= not exists $$fields{"$tag/$occ"}
ABCresult
0XX0
10X0
1100
1111
221 20 for my $id (keys %$fields) {
222 2 ▶ return $id
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ m[^$tag/(..)-(..)$]u and $occ >= $1 and $occ <= $2 1 1 2
Condition50%3/6
$id =~ m[^$tag/(..)-(..)$]u and $occ >= $1 and $occ <= $2
!ll&&!rl&&r
011
$id =~ m[^$tag/(..)-(..)$]u and $occ >= $1
!ll&&!rl&&r
002
MC/DC33%1/3
$id =~ m[^$tag/(..)-(..)$]u and $occ >= $1 and $occ <= $2
$id =~ m[^$tag/(..)-(..)$]u $occ >= $1 $occ <= $2
Truth table50%2/4
A and B and C
A= $id =~ m[^$tag/(..)-(..)$]uB= $occ >= $1C= $occ <= $2
ABCresult
0XX0
10X0
1100
1111
223 if $id =~ /^$tag\/(..)-(..)$/ && $occ >= $1 && $occ <= $2;
224 }
225 }
226  
227 3072 ▶ return $occ ne '' ? "$tag/$occ" : $tag;
Branch: 1 branch
BranchTrueFalseTotal
$occ ne '' ? : 22 3050 3072
228 }
229  
230 sub TO_JSON {
231 0 ▶ my ($self) = @_;
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod undocumented
232 0 return {map {$_ => $self->{$_}} keys %$self};
233 }
234  
235 sub abbreviated {
236 0 ▶ my ($self) = @_;
Subroutine abbreviated: not called CC=5 SCAR=34.0
Pod documented
237 0 my $abbr = dclone($self->TO_JSON);
238 0 ▶ for (values %{$abbr->{fields} // {}}) {
Condition0%0/2
$abbr->{"fields"} // {}
l!l
00
MC/DC0%0/1
$abbr->{"fields"} // {}
$abbr->{"fields"}
Truth table0%0/2
A
A= $abbr->{"fields"}
Aresult
11
00
239 0 delete $_->{tag};
240 0 delete $_->{occurrence};
241 0 ▶ for (values %{$_->{subfields} // {}}) {
Condition0%0/2
$_->{"subfields"} // {}
l!l
00
MC/DC0%0/1
$_->{"subfields"} // {}
$_->{"subfields"}
Truth table0%0/2
A
A= $_->{"subfields"}
Aresult
11
00
242 0 delete $_->{code};
243 }
244 }
245 0 return $abbr;
246 }
247  
248 sub clean_pica {
249 50 ▶ my ($record, %options) = @_;
Subroutine clean_pica: called CC=22 SCAR=31.0
Pod documented
250  
251 50 my $ok = 1;
252 50 my $handler = $options{error};
253 my $error = exists $options{error}
254 ? sub {
255 17 ▶ if ($handler) {
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if ($handler) 17 0 17
256 17 ▶ $handler->(PICA::Error->new($_[1] || [], message => $_[0]));
Condition100%2/2
$_[1] || []
l!l
98
MC/DC100%1/1
$_[1] || []
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
257 }
258 17 $ok = 0;
259 }
260 50 ▶ : sub {say STDERR shift; $ok = 0};
Subroutine __ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'error'} ? : 43 7 50
261  
262 50 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 31 19 50
263  
264 50 ▶ if (reftype $record ne 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($record) ne 'ARRAY') { } 5 45 50
elsif (not @$record || $options{'ignore_empty_records'}) { } 1 44 45
265 5 $error->('PICA record must be array reference');
266 }
267 ▶ elsif (!@$record && !$options{ignore_empty_records}) {
Condition100%3/3
@$record || $options{'ignore_empty_records'}
l!l&&r!l&&!r
4311
MC/DC100%2/2
@$record || $options{'ignore_empty_records'}
@$record $options{'ignore_empty_records'}
Truth table100%3/3
A || B
A= @$recordB= $options{'ignore_empty_records'}
ABresult
1X1
011
000
268 1 $error->('PICA record should not be empty');
269 }
270  
271 50 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 6 44 50
272  
273 44 my @filtered;
274  
275 44 for my $field (@$record) {
276 3083 ▶ if (reftype $field ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($field) ne "ARRAY") 2 3081 3083
277 2 $error->('PICA field must be array reference');
278 return
279 2 }
280  
281 3081 my ($tag, $occ, @sf) = @$field;
282  
283 3081 ▶ if ($tag !~ /^[012.][0-9.][0-9.][A-Z@.]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tag =~ /^[012.][0-9.][0-9.][A-Z\@.]$/u) 2 3079 3081
284 2 $error->("Malformed PICA tag: $tag", $field);
285 }
286  
287 3081 ▶ if ($occ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occ) 2874 207 3081
288 2874 ▶ if ($occ !~ /^[0-9]{1,3}$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $occ =~ /^[0-9]{1,3}$/u) { } 1 2873 2874
elsif (substr($tag, 0, 1) ne '2' and length $occ eq 3) { } 1 5725 5726
289 1 $error->("Malformed occurrence: $occ", $field);
290 }
291 ▶ elsif (substr($tag, 0, 1) ne '2' && length $occ eq 3) {
Condition100%3/3
substr($tag, 0, 1) ne 2 and length $occ eq 3
!ll&&!rl&&r
2853191
MC/DC100%2/2
substr($tag, 0, 1) ne 2 and length $occ eq 3
substr($tag, 0, 1) ne 2 length $occ eq 3
Truth table100%3/3
A and B
A= substr($tag, 0, 1) ne 2B= length $occ eq 3
ABresult
0X0
100
111
292 1 $error->(
293 "Three digit occurrences only allowed on PICA level 2",
294 $field
295 );
296 }
297 }
298  
299 3081 my $msg = check_annotation($field, %options);
300 3081 ▶ $error->($msg, $field) if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 3 3078 3081
301  
302 3081 ▶ next if $options{ignore_subfields};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"ignore_subfields"} 0 3081 3081
303  
304 3081 ▶ pop @sf if @sf % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @sf % 2 6 3075 3081
305 3081 ▶ while (@sf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sf) 6764 3081 9845
306 6764 my ($code, $value) = splice @sf, 0, 2;
307  
308 6764 ▶ $error->("Malformed PICA subfield: $code", $field)
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^[_A-Za-z0-9]$/u 1 6763 6764
309 if $code !~ /^[_A-Za-z0-9]$/;
310 $error->("PICA subfield \$$code must be non-empty string", $field)
311 6764 ▶ if $value !~ /^./ && !$options{allow_empty_subfields};
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^./u or $options{"allow_empty_subfields"} 1 6763 6764
Condition66%2/3
$value =~ /^./u or $options{"allow_empty_subfields"}
l!l&&r!l&&!r
676301
MC/DC50%1/2
$value =~ /^./u or $options{"allow_empty_subfields"}
$value =~ /^./u $options{"allow_empty_subfields"}
Truth table66%2/3
A or B
A= $value =~ /^./uB= $options{"allow_empty_subfields"}
ABresult
1X1
011
000
312 }
313 }
314  
315 42 ▶ return $record if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 36 6 42
316 }
317  
318 10 1;
319 __END__
« blib/lib/PICA/Writer/Fields.pm ↑ Summary blib/lib/PICA/Writer/XML.pm »