blib/lib/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
« blib/lib/PICA/Error.pm ↑ Summary blib/lib/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.09';
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 sub new {
11 16 ▶ my $class = shift;
Subroutine new: called CC=9 SCAR=23.5
Pod documented
12 16 my $tag = shift;
13  
14 # simplify migration from PICA::Record
15 return pica_field($tag->{_tag}, $tag->{_occurrence},
16 16 ▶ @{$tag->{_subfields}})
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag eq "PICA::Field" 0 16 16
17 if ref $tag eq 'PICA::Field';
18  
19 16 my $field = bless [], $class;
20  
21 16 my $occ = '';
22  
23 16 ▶ if (@_ % 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ % 2) { } 9 7 16
elsif ($tag =~ m[^([0-2]\d{2}[A-Z\@])(/(\d+))?$]u) { } 7 0 7
24 9 ▶ $occ = shift // '';
Condition50%1/2
shift() // ""
l!l
90
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 16 $field->tag($tag);
32 16 $field->occurrence($occ);
33  
34 16 ▶ croak "missing subfields" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 16 16
35 16 ▶ my $ann = pop @_ if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 16 16
36 16 $field->subfields(@_);
37 16 ▶ $field->annotation($ann) if defined $ann && $ann ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $ann and $ann ne "" 0 16 16
Condition33%1/3
defined $ann and $ann ne ""
!ll&&!rl&&r
1600
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 16 return $field;
40 }
41  
42 sub level {
43 1 ▶ substr $_[0]->[0], 0, 1;
Subroutine level: called CC=1 SCAR=0.0
Pod documented
44 }
45  
46 sub tag {
47 16 ▶ if (@_ > 1) {
Subroutine tag: called CC=3 SCAR=11.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 16 0 16
48 16 my $tag = $_[1];
49 16 ▶ 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 16 16
50 16 $_[0]->[0] = $tag;
51 }
52 16 $_[0]->[0];
53 }
54  
55 sub occurrence {
56 19 ▶ if (@_ > 1) {
Subroutine occurrence: called CC=6 SCAR=18.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 18 1 19
57 18 my $occ = $_[1];
58 18 ▶ if ($occ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($occ == 0) { } 14 4 18
59 14 $occ = undef;
60 }
61 else {
62 4 ▶ croak "invalid occurrence: $occ" if $occ !~ qr/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $occ =~ qr/^\d+$/u 0 4 4
63 4 ▶ if ($occ < 99) {
Branch: 2 branches
BranchTrueFalseTotal
if ($occ < 99) { } 4 0 4
elsif ($_[0]->level eq '2') { } 0 0 0
64 4 $occ = sprintf('%02d', $occ);
65 }
66 elsif ($_[0]->level eq '2') {
67 0 $occ = sprintf('%03d', $occ);
68 }
69 }
70 18 $_[0]->[1] = $occ;
71 }
72 19 $_[0]->[1];
73 }
74  
75 sub id {
76 2 ▶ my ($tag, $occ) = @{$_[0]};
Subroutine id: called CC=2 SCAR=6.9
Pod documented
77 2 ▶ $occ > 0 ? "$tag/$occ" : $tag;
Branch: 1 branch
BranchTrueFalseTotal
$occ > 0 ? : 1 1 2
78 }
79  
80 sub annotation {
81 60 ▶ my ($field, $ann) = @_;
Subroutine annotation: called CC=8 SCAR=20.8
Pod undocumented
82 60 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 22 38 60
83 22 my $has_ann = !($#$field % 2);
84 22 ▶ if (($ann // '') eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (($ann // '') eq '') { } 9 13 22
Condition100%2/2
$ann // ""
l!l
148
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 13 ▶ croak "invalid annotation: $ann" if $ann !~ /^[^A-Za-z0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ann =~ /^[^A-Za-z0-9]$/u 1 12 13
89 12 ▶ if ($has_ann) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ann) { } 2 10 12
90 2 $field->[-1] = $ann;
91 }
92 else {
93 10 push @$field, $ann;
94 }
95 }
96 }
97 59 ▶ return $#$field % 2 ? undef : $field->[-1];
Branch: 1 branch
BranchTrueFalseTotal
$field->$#* % 2 ? : 13 46 59
98 }
99  
100 sub subfields {
101 16 ▶ my $field = shift;
Subroutine subfields: called CC=7 SCAR=20.6
Pod documented
102  
103 16 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 16 0 16
104 16 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 22 16 38
105 22 my $code = shift;
106 22 my $value = shift;
107  
108 22 ▶ croak "invalid subfield code: $code" if $code !~ /^[A-Za-z0-9]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /^[A-Za-z0-9]$/u 0 22 22
109  
110 22 ▶ if (defined $value and $value ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value ne "") 19 3 22
Condition100%3/3
defined $value and $value ne ""
!ll&&!rl&&r
1219
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 19 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 sub set {
122 2 ▶ my ($field, $code, $value) = @_;
Subroutine set: called CC=6 SCAR=18.5
Pod documented
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 sub equal {
138 4 ▶ my ($a, $b) = @_;
Subroutine equal: called CC=2 SCAR=6.9
Pod documented
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 sub clone {
143 1 ▶ bless $_[0]->TO_JSON, 'PICA::Data::Field';
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
144 }
145  
146 sub TO_JSON {
147 3 ▶ [@{$_[0]}];
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
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 =cut
« blib/lib/PICA/Error.pm ↑ Summary blib/lib/PICA/Parser/Plus.pm »