PICA/Data/Field.pm

filter: Statementstmt 95.2% 80 / 84 Branchbran 68.9% 40 / 58 Conditioncond 68.7% 11 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 100% 16 / 16 Podpod 81.8% 9 / 11 totaltotal 82.5% 161 / 195 CC 36 SCAR 18.2
CC
36
Cov
83%
new
10.5
annotation
8.0
subfields
7.9
CRAP 6.2
« PICA/Error.pm ↑ Summary PICA/Parser/Plus.pm »
1 package PICA::Data::Field;
2 17 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 17 our $VERSION = '2.12';
5  
6 17 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶sub new {
Subroutine new: called CC=9 SCAR=23.5
Pod documented
11 17 my $class = shift;
12 17 my $tag = shift;
13  
14 # simplify migration from PICA::Record
15 return pica_field($tag->{_tag}, $tag->{_occurrence},
16 0 ▶ @{$tag->{_subfields}})
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag eq "PICA::Field" 0 17 17
17 if ref $tag eq 'PICA::Field';
18  
19 17 my $field = bless [], $class;
20  
21 17 my $occ = '';
22  
23 17 ▶ if (@_ % 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2) { } 10 7 17
elsif ($tag =~ m[^([0-2]\d{2}[A-Z\@])(/(\d+))?$]u) { } 7 0 7
24 10 ▶ $occ = shift // '';
Condition50%1/2
shift() // ""
l!l
100
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
25 }
26 elsif ($tag =~ m/^([0-2]\d{2}[A-Z@])(\/(\d+))?$/) {
27 7 $tag = $1;
28 7 $occ = $3;
29 }
30  
31 17 $field->tag($tag);
32 17 $field->occurrence($occ);
33  
34 17 ▶ croak "missing subfields" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 17 17
35 17 ▶ my $ann = pop @_ if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 17 17
36 17 $field->subfields(@_);
37 17 ▶ $field->annotation($ann) if defined $ann && $ann ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ann and $ann ne "" 0 17 17
Condition33%1/3
defined $ann and $ann ne ""
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $ann and $ann ne ""
defined $ann $ann ne ""
Truth table33%1/3
A and B
A= defined $annB= $ann ne ""
ABresult
0X0
100
111
38  
39 17 return $field;
40 }
41  
42 1 ▶sub level {
Subroutine level: called CC=1 SCAR=0.0
Pod documented
43 1 substr $_[0]->[0], 0, 1;
44 }
45  
46 17 ▶sub tag {
Subroutine tag: called CC=3 SCAR=11.3
Pod documented
47 17 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 17 0 17
48 17 my $tag = $_[1];
49 17 ▶ croak "invalid tag: $tag" if $tag !~ qr/^[0-2]\d{2}[A-Z@]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ qr/^[0-2]\d{2}[A-Z\@]$/u 0 17 17
50 17 $_[0]->[0] = $tag;
51 }
52 17 $_[0]->[0];
53 }
54  
55 20 ▶sub occurrence {
Subroutine occurrence: called CC=6 SCAR=18.8
Pod documented
56 20 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 19 1 20
57 19 my $occ = $_[1];
58 19 ▶ if ($occ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occ == 0) { } 14 5 19
59 14 $occ = undef;
60 }
61 else {
62 5 ▶ croak "invalid occurrence: $occ" if $occ !~ qr/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $occ =~ qr/^\d+$/u 0 5 5
63 5 ▶ if ($occ < 99) {
Branch: 2 branches
BranchTrueFalseTotal
if ($occ < 99) { } 5 0 5
elsif ($_[0]->level eq '2') { } 0 0 0
64 5 $occ = sprintf('%02d', $occ);
65 }
66 elsif ($_[0]->level eq '2') {
67 0 $occ = sprintf('%03d', $occ);
68 }
69 }
70 19 $_[0]->[1] = $occ;
71 }
72 20 $_[0]->[1];
73 }
74  
75 2 ▶sub id {
Subroutine id: called CC=2 SCAR=6.9
Pod documented
76 2 my ($tag, $occ) = @{$_[0]};
77 2 ▶ $occ > 0 ? "$tag/$occ" : $tag;
Branch: 1 branch
BranchTrueFalseTotal
$occ > 0 ? : 1 1 2
78 }
79  
80 61 ▶sub annotation {
Subroutine annotation: called CC=8 SCAR=20.8
Pod undocumented
81 61 my ($field, $ann) = @_;
82 61 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 23 38 61
83 23 my $has_ann = !($#$field % 2);
84 23 ▶ if (($ann // '') eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (($ann // '') eq '') { } 9 14 23
Condition100%2/2
$ann // ""
l!l
158
MC/DC100%1/1
$ann // ""
$ann
Truth table100%2/2
A
A= $ann
Aresult
11
00
85 9 ▶ pop @$field if $has_ann;
Branch: 1 branch
BranchTrueFalseTotal
if $has_ann 9 0 9
86 }
87 else {
88 14 ▶ croak "invalid annotation: $ann" if $ann !~ /^[^A-Za-z0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ann =~ /^[^A-Za-z0-9]$/u 1 13 14
89 13 ▶ if ($has_ann) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ann) { } 2 11 13
90 2 $field->[-1] = $ann;
91 }
92 else {
93 11 push @$field, $ann;
94 }
95 }
96 }
97 60 ▶ return $#$field % 2 ? undef : $field->[-1];
Branch: 1 branch
BranchTrueFalseTotal
$field->$#* % 2 ? : 13 47 60
98 }
99  
100 17 ▶sub subfields {
Subroutine subfields: called CC=7 SCAR=20.6
Pod documented
101 17 my $field = shift;
102  
103 17 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 17 0 17
104 17 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 23 17 40
105 23 my $code = shift;
106 23 my $value = shift;
107  
108 23 ▶ croak "invalid subfield code: $code" if $code !~ /^[A-Za-z0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^[A-Za-z0-9]$/u 0 23 23
109  
110 23 ▶ if (defined $value and $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "") 20 3 23
Condition100%3/3
defined $value and $value ne ""
!ll&&!rl&&r
1220
MC/DC100%2/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table100%3/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
111 20 push @$field, $code, $value;
112 }
113 }
114 }
115 else {
116 0 ▶ my $l = @$field % 2 ? $#$field - 1 : $#$field;
Branch: 1 branch
BranchTrueFalseTotal
@$field % 2 ? : 0 0 0
117 0 return Hash::MultiValue->new(@$field[2 .. $l]);
118 }
119 }
120  
121 2 ▶sub set {
Subroutine set: called CC=6 SCAR=18.5
Pod documented
122 2 my ($field, $code, $value) = @_;
123 2 ▶ croak "invalid subfield code: $code" if $code !~ /^[A-Za-z0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^[A-Za-z0-9]$/u 0 2 2
124  
125 2 ▶ return unless defined $value and $value ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value ne "" 0 2 2
Condition33%1/3
defined $value and $value ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table33%1/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
126  
127 2 ▶ for (my $i = 2; $i <= @$field / 2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= @$field / 2) 2 1 3
128 2 ▶ if ($field->[$i] eq $code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] eq $code) 1 1 2
129 1 $field->[$i + 1] = $value;
130 1 return;
131 }
132 }
133  
134 1 push @$field, $code, $value;
135 }
136  
137 4 ▶sub equal {
Subroutine equal: called CC=2 SCAR=6.9
Pod documented
138 4 my ($a, $b) = @_;
139 4 ▶ return (@$a == @$b && !defined first {$a->[$_] ne $b->[$_]} 0 .. $#{$b});
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
@$a == @$b && !defined(&first(sub { $a->[$_] ne $b->[$_]; } , 0 .. $#{$b;}))
!ll&&!rl&&r
112
MC/DC100%2/2
@$a == @$b && !defined(&first(sub { $a->[$_] ne $b->[$_]; } , 0 .. $#{$b;}))
@$a == @$b !defined(&first(sub { $a->[$_] ne $b->[$_]; } , 0 .. $#{$b;}))
Truth table100%3/3
A && not(B)
A= @$a == @$bB= !defined(&first(sub { $a->[$_] ne $b->[$_]; } , 0 .. $#{$b;}))
ABresult
0X0
100
111
140 }
141  
142 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
143 1 bless TO_JSON($_[0]), 'PICA::Data::Field';
144 }
145  
146 3 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
147 3 [@{$_[0]}];
148 }
149  
150 17 1;
151  
152 =head1 NAME
153  
154 PICA::Data::Field - PICA+ Field
155  
156 =head1 DESCRIPTION
157  
158 A PICA::Data::Field is a blessed array reference with tag, occurrence,
159 subfields, and optional annotation.
160  
161 =head1 METHODS
162  
163 =head2 new( $tag, [$occ,] @subfields [,$annotation] )
164  
165 Create a new PICA+ field. Will die on invalid tag, occurrence, subfield
166 code or annotation.
167  
168 =head2 level
169  
170 Get the record level (0, 1, or 2).
171  
172 =head2 tag( [$value] )
173  
174 Get or set the tag.
175  
176 =head2 occurrence( [$value] )
177  
178 Get or set the occurrence.
179  
180 =head2 id
181  
182 Get the field identifier (tag and optional occurrence).
183  
184 =head2 subfields( [ $code => $value, ...] )
185  
186 Set all subfields if arguments are given. Otherwise return a
187 L<Hash::MultiValue> of all subfields. Use it's getter methods to access
188 subfield values. Changing subfields this way won't work!
189  
190 =head2 set( $code => $value )
191  
192 Set or append a subfield.
193  
194 =head2 equal( $field )
195  
196 Check whether the field is equal to another field.
197  
198 =head2 clone
199  
200 Return a copy of this field.
201  
202 =head2 SEE ALSO
203  
204 Full records are processed as L<PICA::Data> objects.
205  
206 =cut
« PICA/Error.pm ↑ Summary PICA/Parser/Plus.pm »