| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 24 | 10 | ▶ | $occ = shift // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 17 | ▶ | my $ann = pop @_ if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 17 | $field->subfields(@_); | |||||||||||||||||||||||||||
| 37 | 17 | ▶ | $field->annotation($ann) if defined $ann && $ann ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ann and $ann ne ""
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 ""
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 48 | 17 | my $tag = $_[1]; | |||||||||||||||||||||||||||
| 49 | 17 | ▶ | croak "invalid tag: $tag" if $tag !~ qr/^[0-2]\d{2}[A-Z@]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 57 | 19 | my $occ = $_[1]; | |||||||||||||||||||||||||||
| 58 | 19 | ▶ | if ($occ == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 14 | $occ = undef; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 5 | ▶ | croak "invalid occurrence: $occ" if $occ !~ qr/^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 5 | ▶ | if ($occ < 99) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 83 | 23 | my $has_ann = !($#$field % 2); | |||||||||||||||||||||||||||
| 84 | 23 | ▶ | if (($ann // '') eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$ann // ""
MC/DC100%1/1
$ann // ""
$ann
Truth table100%2/2
A
A= $ann
| |||||||||||||||||||||||||||||
| 85 | 9 | ▶ | pop @$field if $has_ann; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 14 | ▶ | croak "invalid annotation: $ann" if $ann !~ /^[^A-Za-z0-9]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 13 | ▶ | if ($has_ann) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 104 | 17 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 23 | ▶ | if (defined $value and $value ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value ne ""
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 ""
| |||||||||||||||||||||||||||||
| 111 | 20 | push @$field, $code, $value; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | my $l = @$field % 2 ? $#$field - 1 : $#$field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | ▶ | return unless defined $value and $value ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value ne ""
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 ""
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | ▶ | for (my $i = 2; $i <= @$field / 2; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 2 | ▶ | if ($field->[$i] eq $code) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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;}))
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;}))
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||