MARC.pm

filter: Statementstmt 89.2% 648 / 726 Branchbran 74.2% 383 / 516 Conditioncond 50.4% 105 / 208 MC/DCmcdc 29.8% 34 / 114 Subroutinesub 100% 35 / 35 Podpod 85.7% 12 / 14 totaltotal 75.4% 1217 / 1613 CC 364 SCAR 40.1
CC
364
Cov
78%
marc_paste
122.6
marc_append
105.1
marc_copy
100.4
CRAP 55.2
↑ Summary Exporter/MARC/ALEPHSEQ.pm »
1 package Catmandu::MARC;
2  
3 33 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use Catmandu::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Catmandu::Exporter::MARC::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use MARC::Spec::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 33 with 'MooX::Singleton';
13  
14 33 memoize('compile_marc_path');
15 33 memoize('parse_marc_spec');
16 33 memoize('_get_index_range');
17  
18 33 our $VERSION = '1.33';
19  
20 675 ▶sub marc_map {
Subroutine marc_map: called CC=51 SCAR=40.9
Pod documented
21 675 my $self = $_[0];
22  
23 # $_[2] : marc_path
24 675 ▶ my $context
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] ? : 645 30 675
25 = ref( $_[2] )
26 ? $_[2]
27 : $self->compile_marc_path( $_[2], subfield_wildcard => 1 );
28  
29 675 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 675 675
30  
31 # $_[1] : data record
32 675 my $record = $_[1]->{'record'};
33  
34 675 ▶ return wantarray ? () : undef
Branch: 2 branches
BranchTrueFalseTotal
unless defined $record and ref $record eq "ARRAY" 0 675 675
wantarray ? : 0 0 0
Condition33%1/3
defined $record and ref $record eq "ARRAY"
!ll&&!rl&&r
00675
MC/DC0%0/2
defined $record and ref $record eq "ARRAY"
defined $record ref $record eq "ARRAY"
Truth table33%1/3
A and B
A= defined $recordB= ref $record eq "ARRAY"
ABresult
0X0
100
111
35 unless ( defined $record && ref($record) eq 'ARRAY' );
36  
37 # $_[3] : opts
38 675 ▶ my $split = $_[3]->{'-split'} // 0;
Condition50%1/2
$_[3]{"-split"} // 0
l!l
6750
MC/DC0%0/1
$_[3]{"-split"} // 0
$_[3]{"-split"}
Truth table50%1/2
A
A= $_[3]{"-split"}
Aresult
11
00
39 675 ▶ my $join_char = $_[3]->{'-join'} // '';
Condition50%1/2
$_[3]{"-join"} // ""
l!l
6750
MC/DC0%0/1
$_[3]{"-join"} // ""
$_[3]{"-join"}
Truth table50%1/2
A
A= $_[3]{"-join"}
Aresult
11
00
40 675 ▶ my $pluck = $_[3]->{'-pluck'} // 0;
Condition50%1/2
$_[3]{"-pluck"} // 0
l!l
6750
MC/DC0%0/1
$_[3]{"-pluck"} // 0
$_[3]{"-pluck"}
Truth table50%1/2
A
A= $_[3]{"-pluck"}
Aresult
11
00
41 675 ▶ my $value_set = $_[3]->{'-value'} // undef;
Condition100%2/2
$_[3]{"-value"} // undef
l!l
64611
MC/DC100%1/1
$_[3]{"-value"} // undef
$_[3]{"-value"}
Truth table100%2/2
A
A= $_[3]{"-value"}
Aresult
11
00
42 675 ▶ my $nested_arrays = $_[3]->{'-nested_arrays'} // 0;
Condition50%1/2
$_[3]{"-nested_arrays"} // 0
l!l
6750
MC/DC0%0/1
$_[3]{"-nested_arrays"} // 0
$_[3]{"-nested_arrays"}
Truth table50%1/2
A
A= $_[3]{"-nested_arrays"}
Aresult
11
00
43 675 ▶ my $append = $_[3]->{'-force_array'} // undef;
Condition100%2/2
$_[3]{"-force_array"} // undef
l!l
65223
MC/DC100%1/1
$_[3]{"-force_array"} // undef
$_[3]{"-force_array"}
Truth table100%2/2
A
A= $_[3]{"-force_array"}
Aresult
11
00
44  
45 # Do an implicit split for nested_arrays , except when no-implicit-split is set
46 675 ▶ if ( $nested_arrays == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested_arrays == 1) 45 630 675
47 45 ▶ $split = 1 unless $_[3]->{'-no-implicit-split'};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[3]{"-no-implicit-split"} 16 29 45
48 }
49  
50 675 my $vals;
51  
52 675 for my $field (@$record) {
53 next
54 if (
55 ( $context->{is_regex_field} == 0
56 && $field->[0] ne $context->{field}
57 )
58 || ( defined $context->{ind1}
59 && ( !defined $field->[1] || $field->[1] ne $context->{ind1} )
60 )
61 || ( defined $context->{ind2}
62 && ( !defined $field->[2] || $field->[2] ne $context->{ind2} )
63 )
64 || ( $context->{is_regex_field} == 1
65 && $field->[0] !~ $context->{field_regex} )
66 5497 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $context->{"is_regex_field"} == 0 and $field->[0] ne $context->{"field"} or defined $context->{"ind1"} and !defined($field->[1]) || $field->[1] ne $context->{"ind1"} or defined $context->{"ind2"} and !defined($field->[2]) || $field->[2] ne $context->{"ind2"} or $context->{"is_regex_field"} == 1 and not $field->[0] =~ /$context->{"field_regex"}/ 5023 474 5497
Condition48%13/27
$context->{"is_regex_field"} == 0 and $field->[0] ne $context->{"field"} or defined $context->{"ind1"} and !defined($field->[1]) || $field->[1] ne $context->{"ind1"} or defined $context->{"ind2"} and !defined($field->[2]) || $field->[2] ne $context->{"ind2"} or $context->{"is_regex_field"} == 1 and not $field->[0] =~ /$context->{"field_regex"}/
l!l&&r!l&&!r
50230474
$context->{"is_regex_field"} == 0 and $field->[0] ne $context->{"field"} or defined $context->{"ind1"} and !defined($field->[1]) || $field->[1] ne $context->{"ind1"} or defined $context->{"ind2"} and !defined($field->[2]) || $field->[2] ne $context->{"ind2"}
l!l&&r!l&&!r
50230474
$context->{"is_regex_field"} == 0 and $field->[0] ne $context->{"field"} or defined $context->{"ind1"} and !defined($field->[1]) || $field->[1] ne $context->{"ind1"}
l!l&&r!l&&!r
50230474
$context->{"is_regex_field"} == 0 and $field->[0] ne $context->{"field"}
!ll&&!rl&&r
1603145023
defined $context->{"ind1"} and !defined($field->[1]) || $field->[1] ne $context->{"ind1"}
!ll&&!rl&&r
47400
not defined $field->[1] or $field->[1] ne $context->{"ind1"}
l!l&&r!l&&!r
000
defined $context->{"ind2"} and !defined($field->[2]) || $field->[2] ne $context->{"ind2"}
!ll&&!rl&&r
47400
not defined $field->[2] or $field->[2] ne $context->{"ind2"}
l!l&&r!l&&!r
000
$context->{"is_regex_field"} == 1 and not $field->[0] =~ /$context->{"field_regex"}/
!ll&&!rl&&r
3141600
MC/DC20%2/10
$context->{"is_regex_field"} == 0 and $field->[0] ne $context->{"field"} or defined $context->{"ind1"} and !defined($field->[1]) || $field->[1] ne $context->{"ind1"} or defined $context->{"ind2"} and !defined($field->[2]) || $field->[2] ne $context->{"ind2"} or $context->{"is_regex_field"} == 1 and not $field->[0] =~ /$context->{"field_regex"}/
$context->{"is_regex_field"} == 0 $field->[0] ne $context->{"field"} defined $context->{"ind1"} not defined $field->[1] $field->[1] ne $context->{"ind1"} defined $context->{"ind2"} not defined $field->[2] $field->[2] ne $context->{"ind2"} $context->{"is_regex_field"} == 1 not $field->[0] =~ /$context->{"field_regex"}/
Truth table8%3/37
A and B or C and not(D) or E or F and not(G) or H or I and not(J)
A= $context->{"is_regex_field"} == 0B= $field->[0] ne $context->{"field"}C= defined $context->{"ind1"}D= not defined $field->[1]E= $field->[1] ne $context->{"ind1"}F= defined $context->{"ind2"}G= not defined $field->[2]H= $field->[2] ne $context->{"ind2"}I= $context->{"is_regex_field"} == 1J= not $field->[0] =~ /$context->{"field_regex"}/
ABCDEFGHIJresult
11XXXXXXXX1
0X11XXXXXX1
0X101XXXXX1
1011XXXXXX1
10101XXXXX1
0X0XX11XXX1
0X0XX101XX1
0X10011XXX1
0X100101XX1
100XX11XXX1
100XX101XX1
1010011XXX1
10100101XX1
0X0XX0XX111
0X0XX100111
0X1000XX111
0X100100111
100XX0XX111
100XX100111
101000XX111
10100100111
0X0XX0XX0X0
0X0XX0XX100
0X0XX1000X0
0X0XX100100
0X1000XX0X0
0X1000XX100
0X1001000X0
0X100100100
100XX0XX0X0
100XX0XX100
100XX1000X0
100XX100100
101000XX0X0
101000XX100
101001000X0
10100100100
67  
68 474 my $v;
69  
70 474 ▶ if ($value_set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_set) { } 48 426 474
71 48 ▶ for ( my $i = 3; $i < @{$field}; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 96 14 110
72 96 my $subfield_regex = $context->{subfield_regex};
73 96 ▶ if ( $field->[$i] =~ $subfield_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] =~ /$subfield_regex/) 34 62 96
74 34 $v = $value_set;
75 34 last;
76 }
77 }
78 }
79 else {
80 426 $v = [];
81  
82 426 ▶ if ($pluck) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pluck) { } 18 408 426
83  
84 # Treat the subfield as a hash index
85 18 my $_h = {};
86 18 ▶ for ( my $i = $context->{start}; $i < @{$field}; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 37 18 55
87 37 push @{ $_h->{ $field->[$i] } }, $field->[ $i + 1 ];
88 }
89 18 my $subfield = $context->{subfield};
90 18 $subfield =~ s{[^a-zA-Z0-9]}{}g;
91 18 for my $c ( split( '', $subfield ) ) {
92 37 ▶ my $val = $_h->{$c} // [undef];
Condition100%2/2
$_h->{$c} // [undef]
l!l
343
MC/DC100%1/1
$_h->{$c} // [undef]
$_h->{$c}
Truth table100%2/2
A
A= $_h->{$c}
Aresult
11
00
93 37 push @$v, @{$val};
94 }
95 }
96 else {
97 408 ▶ for ( my $i = $context->{start}; $i < @{$field}; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 604 408 1012
98 604 my $subfield_regex = $context->{subfield_regex};
99 604 ▶ if ( $field->[$i] =~ $subfield_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] =~ /$subfield_regex/) 444 160 604
100 444 push( @$v, $field->[ $i + 1 ] );
101 }
102 }
103 }
104  
105 426 ▶ if (@$v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$v) { } 372 54 426
106 372 ▶ if ( !$split ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($split) 290 82 372
107 290 my @defined_values = grep { defined($_) } @$v;
108 290 $v = join $join_char, @defined_values;
109 }
110  
111 372 ▶ if ( defined( my $off = $context->{from} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $off = $context->{"from"})) 51 321 372
112 51 ▶ if ( ref $v eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 11 40 51
113 11 my @defined_values = grep { defined($_) } @$v;
114 11 $v = join $join_char, @defined_values;
115 }
116 51 my $len = $context->{len};
117 51 ▶ if ( length( ${v} ) > $off ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $v > $off) { } 41 10 51
118 41 $v = substr( $v, $off, $len );
119 }
120 else {
121 10 $v = undef;
122 }
123 }
124 }
125 else {
126 54 $v = undef;
127 }
128 }
129  
130 474 ▶ if ( defined $v ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 396 78 474
131 396 ▶ if ($split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($split) { } 82 314 396
132 82 ▶ $v = [$v] unless ( defined($v) && ref($v) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and ref $v eq "ARRAY" 11 71 82
Condition66%2/3
defined $v and ref $v eq "ARRAY"
!ll&&!rl&&r
01171
MC/DC50%1/2
defined $v and ref $v eq "ARRAY"
defined $v ref $v eq "ARRAY"
Truth table66%2/3
A and B
A= defined $vB= ref $v eq "ARRAY"
ABresult
0X0
100
111
133 82 ▶ if ( defined($vals) && ref($vals) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vals and ref $vals eq 'ARRAY') { } 31 51 82
Condition66%2/3
defined $vals and ref $vals eq "ARRAY"
!ll&&!rl&&r
51031
MC/DC50%1/2
defined $vals and ref $vals eq "ARRAY"
defined $vals ref $vals eq "ARRAY"
Truth table66%2/3
A and B
A= defined $valsB= ref $vals eq "ARRAY"
ABresult
0X0
100
111
134  
135 # With the nested arrays option a split will
136 # always return an array of array of values.
137 # This was the old behavior of Inline marc_map functions
138 31 ▶ if ( $nested_arrays == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested_arrays == 1) { } 18 13 31
139 18 push @$vals, $v;
140 }
141 else {
142 13 push @$vals, @$v;
143 }
144 }
145 else {
146 51 ▶ if ( $nested_arrays == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested_arrays == 1) { } 21 30 51
147 21 $vals = [$v];
148 }
149 else {
150 30 $vals = [@$v];
151 }
152 }
153 }
154 else {
155 314 push @$vals, $v;
156 }
157 }
158 }
159  
160 675 ▶ if ( $split && defined $vals ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($split and defined $vals) { } 51 624 675
elsif ($append) { } 416 208 624
elsif (defined $vals) { } 149 59 208
Condition66%2/3
$split and defined $vals
!ll&&!rl&&r
624051
MC/DC50%1/2
$split and defined $vals
$split defined $vals
Truth table66%2/3
A and B
A= $splitB= defined $vals
ABresult
0X0
100
111
161 51 $vals = [$vals];
162 }
163 elsif ($append) {
164  
165 # we got a $append
166 }
167 elsif ( defined $vals ) {
168 149 $vals = join $join_char, @$vals;
169 }
170 else {
171 # no result
172 }
173  
174 675 $vals;
175 }
176  
177 10 ▶sub marc_add {
Subroutine marc_add: called CC=17 SCAR=30.2
Pod documented
178 10 my ( $self, $data, $marc_path, @subfields ) = @_;
179  
180 10 my %subfields = @subfields;
181 10 ▶ my $marc = $data->{'record'} // [];
Condition100%2/2
$data->{"record"} // []
l!l
91
MC/DC100%1/1
$data->{"record"} // []
$data->{"record"}
Truth table100%2/2
A
A= $data->{"record"}
Aresult
11
00
182  
183 10 ▶ if ( $marc_path =~ /^\w{3}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marc_path =~ /^\w{3}$/) 10 0 10
184 10 my @field = ();
185  
186 10 push @field, $marc_path;
187  
188 10 ▶ my $ind1 = $subfields{ind1} // ' ';
Condition100%2/2
$subfields{"ind1"} // " "
l!l
37
MC/DC100%1/1
$subfields{"ind1"} // " "
$subfields{"ind1"}
Truth table100%2/2
A
A= $subfields{"ind1"}
Aresult
11
00
189 10 ▶ my $ind2 = $subfields{ind2} // ' ';
Condition100%2/2
$subfields{"ind2"} // " "
l!l
37
MC/DC100%1/1
$subfields{"ind2"} // " "
$subfields{"ind2"}
Truth table100%2/2
A
A= $subfields{"ind2"}
Aresult
11
00
190  
191 10 ▶ if ($ind1 =~ /^\$\.(\S+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind1 =~ /^\$\.(\S+)$/) 0 10 10
192 0 my $path = $1;
193 0 $ind1 = Catmandu::Util::data_at( $path, $data );
194 }
195  
196 10 ▶ if ($ind2 =~ /^\$\.(\S+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind2 =~ /^\$\.(\S+)$/) 0 10 10
197 0 my $path = $1;
198 0 $ind2 = Catmandu::Util::data_at( $path, $data );
199 }
200
201 10 push @field, $ind1;
202 10 push @field, $ind2;
203  
204 10 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 20 10 30
205 20 my $code = $subfields[$i];
206 20 ▶ next unless length $code == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $code == 1 6 14 20
207 14 my $value = $subfields[ $i + 1 ];
208  
209 14 ▶ if ( $value =~ /^\$\.(\S+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\$\.(\S+)$/) 6 8 14
210 6 my $path = $1;
211 6 $value = Catmandu::Util::data_at( $path, $data );
212 }
213  
214 14 ▶ if ( Catmandu::Util::is_array_ref $value) {
Branch: 3 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($value)) { } 3 11 14
elsif (Catmandu::Util::is_hash_ref($value)) { } 0 11 11
elsif (Catmandu::Util::is_value($value) and length $value > 0) { } 11 0 11
215 3 for (@$value) {
216 9 push @field, $code;
217 9 push @field, $_;
218 }
219 }
220 elsif ( Catmandu::Util::is_hash_ref $value) {
221 0 for ( keys %$value ) {
222 0 push @field, $code;
223 0 push @field, $value->{$_};
224 }
225 }
226 ▶ elsif ( Catmandu::Util::is_value($value) && length($value) > 0 ) {
Condition33%1/3
Catmandu::Util::is_value($value) and length $value > 0
!ll&&!rl&&r
0011
MC/DC0%0/2
Catmandu::Util::is_value($value) and length $value > 0
Catmandu::Util::is_value($value) length $value > 0
Truth table33%1/3
A and B
A= Catmandu::Util::is_value($value)B= length $value > 0
ABresult
0X0
100
111
227 11 push @field, $code;
228 11 push @field, $value;
229 }
230 }
231  
232 10 ▶ push @{$marc}, \@field if @field > 3;
Branch: 1 branch
BranchTrueFalseTotal
if @field > 3 10 0 10
233 }
234  
235 10 $data->{'record'} = $marc;
236  
237 10 $data;
238 }
239  
240 1 ▶sub marc_append {
Subroutine marc_append: called CC=20 SCAR=46.6
Pod documented
241 1 my ( $self, $data, $marc_path, $value ) = @_;
242 1 my $record = $data->{'record'};
243  
244 1 ▶ return $data unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 0 1 1
245  
246 1 ▶ if ( $value =~ /^\$\.(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\$\.(\S+)/) 0 1 1
247 0 my $path = $1;
248 0 $value = Catmandu::Util::data_at( $path, $data );
249 }
250  
251 1 ▶ if ( Catmandu::Util::is_array_ref $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($value)) { } 0 1 1
elsif (Catmandu::Util::is_hash_ref($value)) { } 0 1 1
252 0 $value = $value->[-1];
253 }
254 elsif ( Catmandu::Util::is_hash_ref $value) {
255 0 my $last;
256 0 for ( keys %$value ) {
257 0 $last = $value->{$_};
258 }
259 0 $value = $last;
260 }
261  
262 1 my $context = $self->compile_marc_path($marc_path);
263  
264 1 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 1 1
265  
266 1 for my $field (@$record) {
267 19 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
268  
269 19 ▶ if ( $context->{is_regex_field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'is_regex_field'}) { } 0 19 19
270 0 ▶ next unless $tag =~ $context->{field_regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /$context->{"field_regex"}/ 0 0 0
271 }
272 else {
273 19 ▶ next unless $tag eq $context->{field};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq $context->{"field"} 18 1 19
274 }
275  
276 1 ▶ if ( defined $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind1"}) 0 1 1
277 0 ▶ if ( !defined $ind1 || $ind1 ne $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind1 or $ind1 ne $context->{"ind1"}) 0 0 0
Condition0%0/3
not defined $ind1 or $ind1 ne $context->{"ind1"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind1 or $ind1 ne $context->{"ind1"}
not defined $ind1 $ind1 ne $context->{"ind1"}
Truth table0%0/3
not(A) or B
A= not defined $ind1B= $ind1 ne $context->{"ind1"}
ABresult
1X1
011
000
278 0 next;
279 }
280 }
281 1 ▶ if ( defined $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind2"}) 0 1 1
282 0 ▶ if ( !defined $ind2 || $ind2 ne $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind2 or $ind2 ne $context->{"ind2"}) 0 0 0
Condition0%0/3
not defined $ind2 or $ind2 ne $context->{"ind2"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind2 or $ind2 ne $context->{"ind2"}
not defined $ind2 $ind2 ne $context->{"ind2"}
Truth table0%0/3
not(A) or B
A= not defined $ind2B= $ind2 ne $context->{"ind2"}
ABresult
1X1
011
000
283 0 next;
284 }
285 }
286  
287 1 ▶ if ( $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'subfield'}) { } 0 1 1
288 0 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 0 0 0
289 0 ▶ if ( $subfields[$i] =~ $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfields[$i] =~ /$context->{"subfield"}/) 0 0 0
290 0 $field->[ $i + 4 ] .= $value;
291 }
292 }
293 }
294 else {
295 1 $field->[-1] .= $value;
296 }
297 }
298  
299 1 $data;
300 }
301  
302 3 ▶sub marc_replace_all {
Subroutine marc_replace_all: called CC=19 SCAR=41.6
Pod documented
303 3 my ( $self, $data, $marc_path, $regex, $value ) = @_;
304 3 my $record = $data->{'record'};
305  
306 3 ▶ return $data unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 0 3 3
307  
308 3 ▶ if ( $value =~ /^\$\.(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\$\.(\S+)/) 0 3 3
309 0 my $path = $1;
310 0 $value = Catmandu::Util::data_at( $path, $data );
311 }
312  
313 3 ▶ if ( Catmandu::Util::is_array_ref $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($value)) { } 0 3 3
elsif (Catmandu::Util::is_hash_ref($value)) { } 0 3 3
314 0 $value = $value->[-1];
315 }
316 elsif ( Catmandu::Util::is_hash_ref $value) {
317 0 my $last;
318 0 for ( keys %$value ) {
319 0 $last = $value->{$_};
320 }
321 0 $value = $last;
322 }
323  
324 3 my $context
325 = $self->compile_marc_path( $marc_path, subfield_wildcard => 1 );
326  
327 3 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 3 3
328  
329 3 for my $field (@$record) {
330 57 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
331  
332 57 ▶ if ( $context->{is_regex_field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'is_regex_field'}) { } 0 57 57
333 0 ▶ next unless $tag =~ $context->{field_regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /$context->{"field_regex"}/ 0 0 0
334 }
335 else {
336 57 ▶ next unless $tag eq $context->{field};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq $context->{"field"} 52 5 57
337 }
338  
339 5 ▶ if ( defined $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind1"}) 0 5 5
340 0 ▶ if ( !defined $ind1 || $ind1 ne $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind1 or $ind1 ne $context->{"ind1"}) 0 0 0
Condition0%0/3
not defined $ind1 or $ind1 ne $context->{"ind1"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind1 or $ind1 ne $context->{"ind1"}
not defined $ind1 $ind1 ne $context->{"ind1"}
Truth table0%0/3
not(A) or B
A= not defined $ind1B= $ind1 ne $context->{"ind1"}
ABresult
1X1
011
000
341 0 next;
342 }
343 }
344 5 ▶ if ( defined $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind2"}) 0 5 5
345 0 ▶ if ( !defined $ind2 || $ind2 ne $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind2 or $ind2 ne $context->{"ind2"}) 0 0 0
Condition0%0/3
not defined $ind2 or $ind2 ne $context->{"ind2"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind2 or $ind2 ne $context->{"ind2"}
not defined $ind2 $ind2 ne $context->{"ind2"}
Truth table0%0/3
not(A) or B
A= not defined $ind2B= $ind2 ne $context->{"ind2"}
ABresult
1X1
011
000
346 0 next;
347 }
348 }
349  
350 5 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 6 5 11
351 6 ▶ if ( $subfields[$i] =~ $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfields[$i] =~ /$context->{"subfield"}/) 5 1 6
352  
353 # Trick to double eval the right hand side
354 5 $field->[ $i + 4 ] =~ s{$regex}{"\"$value\""}eeg;
355 }
356 }
357 }
358  
359 3 $data;
360 }
361  
362 8 ▶sub marc_set {
Subroutine marc_set: called CC=21 SCAR=36.6
Pod documented
363 8 my ( $self, $data, $marc_path, $value, %opts ) = @_;
364 8 my $record = $data->{'record'};
365  
366 8 ▶ return $data unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 0 8 8
367  
368 8 ▶ if ( $value =~ /^\$\.(\S+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\$\.(\S+)/) 3 5 8
369 3 my $path = $1;
370 3 $value = Catmandu::Util::data_at( $path, $data );
371 }
372  
373 8 ▶ if ( Catmandu::Util::is_array_ref $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($value)) { } 0 8 8
elsif (Catmandu::Util::is_hash_ref($value)) { } 0 8 8
374 0 $value = $value->[-1];
375 }
376 elsif ( Catmandu::Util::is_hash_ref $value) {
377 0 my $last;
378 0 for ( keys %$value ) {
379 0 $last = $value->{$_};
380 }
381 0 $value = $last;
382 }
383  
384 8 my $context
385 = $self->compile_marc_path( $marc_path, subfield_default => 1 );
386  
387 8 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 8 8
388  
389 8 for my $field (@$record) {
390 168 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
391  
392 168 ▶ if ( $context->{is_regex_field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'is_regex_field'}) { } 0 168 168
393 0 ▶ next unless $tag =~ $context->{field_regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /$context->{"field_regex"}/ 0 0 0
394 }
395 else {
396 168 ▶ next unless $tag eq $context->{field};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq $context->{"field"} 160 8 168
397 }
398  
399 8 ▶ if ( defined $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind1"}) 2 6 8
400 2 ▶ if ( !defined $ind1 || $ind1 ne $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind1 or $ind1 ne $context->{"ind1"}) 0 2 2
Condition33%1/3
not defined $ind1 or $ind1 ne $context->{"ind1"}
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $ind1 or $ind1 ne $context->{"ind1"}
not defined $ind1 $ind1 ne $context->{"ind1"}
Truth table33%1/3
not(A) or B
A= not defined $ind1B= $ind1 ne $context->{"ind1"}
ABresult
1X1
011
000
401 0 next;
402 }
403 }
404 8 ▶ if ( defined $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind2"}) 0 8 8
405 0 ▶ if ( !defined $ind2 || $ind2 ne $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind2 or $ind2 ne $context->{"ind2"}) 0 0 0
Condition0%0/3
not defined $ind2 or $ind2 ne $context->{"ind2"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind2 or $ind2 ne $context->{"ind2"}
not defined $ind2 $ind2 ne $context->{"ind2"}
Truth table0%0/3
not(A) or B
A= not defined $ind2B= $ind2 ne $context->{"ind2"}
ABresult
1X1
011
000
406 0 next;
407 }
408 }
409  
410 8 my $found = 0;
411 8 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 13 8 21
412 13 ▶ if ( $subfields[$i] =~ $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfields[$i] =~ /$context->{"subfield"}/) 5 8 13
413 5 ▶ if ( defined $context->{from} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{'from'}) { } 1 4 5
414 substr( $field->[ $i + 4 ],
415 $context->{from}, $context->{len} )
416 1 = $value;
417 }
418 else {
419 4 $field->[ $i + 4 ] = $value;
420 }
421 5 $found = 1;
422 }
423 }
424  
425 8 ▶ if ( $found == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found == 0) 3 5 8
426 3 push( @$field, $context->{subfield}, $value );
427 }
428 }
429  
430 8 $data;
431 }
432  
433 12 ▶sub marc_remove {
Subroutine marc_remove: called CC=25 SCAR=36.9
Pod documented
434 12 my ( $self, $data, $marc_path, %opts ) = @_;
435 12 my $record = $data->{'record'};
436  
437 12 my $new_record;
438  
439 12 my $context = $self->compile_marc_path($marc_path);
440  
441 12 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 12 12
442  
443 12 for my $field (@$record) {
444 160 my $field_size = int(@$field);
445  
446 160 ▶ if (( $context->{is_regex_field} == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"is_regex_field"} == 0 and $field->[0] eq $context->{"field"} or $context->{"is_regex_field"} == 1 and $field->[0] =~ /$context->{"field_regex"}/) 12 148 160
Condition55%5/9
$context->{"is_regex_field"} == 0 and $field->[0] eq $context->{"field"} or $context->{"is_regex_field"} == 1 and $field->[0] =~ /$context->{"field_regex"}/
l!l&&r!l&&!r
120148
$context->{"is_regex_field"} == 0 and $field->[0] eq $context->{"field"}
!ll&&!rl&&r
014812
$context->{"is_regex_field"} == 1 and $field->[0] =~ /$context->{"field_regex"}/
!ll&&!rl&&r
14800
MC/DC25%1/4
$context->{"is_regex_field"} == 0 and $field->[0] eq $context->{"field"} or $context->{"is_regex_field"} == 1 and $field->[0] =~ /$context->{"field_regex"}/
$context->{"is_regex_field"} == 0 $field->[0] eq $context->{"field"} $context->{"is_regex_field"} == 1 $field->[0] =~ /$context->{"field_regex"}/
Truth table28%2/7
A and B or C and D
A= $context->{"is_regex_field"} == 0B= $field->[0] eq $context->{"field"}C= $context->{"is_regex_field"} == 1D= $field->[0] =~ /$context->{"field_regex"}/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
447 && $field->[0] eq $context->{field}
448 )
449 || ( $context->{is_regex_field} == 1
450 && $field->[0] =~ $context->{field_regex} )
451 )
452 {
453  
454 12 my $ind_match = undef;
455  
456 12 ▶ if ( defined $context->{ind1} && defined $context->{ind2} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $context->{'ind1'} and defined $context->{'ind2'}) { } 1 11 12
elsif (defined $context->{'ind1'}) { } 0 11 11
elsif (defined $context->{'ind2'}) { } 1 10 11
Condition66%2/3
defined $context->{"ind1"} and defined $context->{"ind2"}
!ll&&!rl&&r
1101
MC/DC50%1/2
defined $context->{"ind1"} and defined $context->{"ind2"}
defined $context->{"ind1"} defined $context->{"ind2"}
Truth table66%2/3
A and B
A= defined $context->{"ind1"}B= defined $context->{"ind2"}
ABresult
0X0
100
111
457 $ind_match = 1
458 if ( defined $field->[1]
459 && $field->[1] eq $context->{ind1}
460 && defined $field->[2]
461 1 ▶ && $field->[2] eq $context->{ind2} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->[1] and $field->[1] eq $context->{"ind1"} and defined $field->[2] and $field->[2] eq $context->{"ind2"} 0 1 1
Condition33%3/9
defined $field->[1] and $field->[1] eq $context->{"ind1"} and defined $field->[2] and $field->[2] eq $context->{"ind2"}
!ll&&!rl&&r
100
defined $field->[1] and $field->[1] eq $context->{"ind1"} and defined $field->[2]
!ll&&!rl&&r
100
defined $field->[1] and $field->[1] eq $context->{"ind1"}
!ll&&!rl&&r
010
MC/DC0%0/4
defined $field->[1] and $field->[1] eq $context->{"ind1"} and defined $field->[2] and $field->[2] eq $context->{"ind2"}
defined $field->[1] $field->[1] eq $context->{"ind1"} defined $field->[2] $field->[2] eq $context->{"ind2"}
Truth table20%1/5
A and B and C and D
A= defined $field->[1]B= $field->[1] eq $context->{"ind1"}C= defined $field->[2]D= $field->[2] eq $context->{"ind2"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
462 }
463 elsif ( defined $context->{ind1} ) {
464 $ind_match = 1
465 if ( defined $field->[1]
466 0 ▶ && $field->[1] eq $context->{ind1} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->[1] and $field->[1] eq $context->{"ind1"} 0 0 0
Condition0%0/3
defined $field->[1] and $field->[1] eq $context->{"ind1"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $field->[1] and $field->[1] eq $context->{"ind1"}
defined $field->[1] $field->[1] eq $context->{"ind1"}
Truth table0%0/3
A and B
A= defined $field->[1]B= $field->[1] eq $context->{"ind1"}
ABresult
0X0
100
111
467 }
468 elsif ( defined $context->{ind2} ) {
469 $ind_match = 1
470 if ( defined $field->[2]
471 1 ▶ && $field->[2] eq $context->{ind2} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->[2] and $field->[2] eq $context->{"ind2"} 1 0 1
Condition33%1/3
defined $field->[2] and $field->[2] eq $context->{"ind2"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $field->[2] and $field->[2] eq $context->{"ind2"}
defined $field->[2] $field->[2] eq $context->{"ind2"}
Truth table33%1/3
A and B
A= defined $field->[2]B= $field->[2] eq $context->{"ind2"}
ABresult
0X0
100
111
472 }
473 else {
474 10 $ind_match = 1;
475 }
476  
477 12 ▶ if ( $ind_match && !defined $context->{subfield_regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ind_match and not defined $context->{"subfield_regex"}) 9 3 12
Condition100%3/3
$ind_match and not defined $context->{"subfield_regex"}
!ll&&!rl&&r
129
MC/DC100%2/2
$ind_match and not defined $context->{"subfield_regex"}
$ind_match not defined $context->{"subfield_regex"}
Truth table100%3/3
A and not(B)
A= $ind_matchB= not defined $context->{"subfield_regex"}
ABresult
0X0
100
111
478 9 next;
479 }
480  
481 3 ▶ if ( defined $context->{subfield_regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"subfield_regex"}) 3 0 3
482 3 my $subfield_regex = $context->{subfield_regex};
483 3 my $new_subf = [];
484 3 ▶ for ( my $i = $context->{start}; $i < $field_size; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $field_size) 6 3 9
485 6 ▶ unless ( $field->[$i] =~ $subfield_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->[$i] =~ /$subfield_regex/) 2 4 6
486 2 push @$new_subf, $field->[$i];
487 2 push @$new_subf, $field->[ $i + 1 ];
488 }
489 }
490  
491 3 ▶ splice @$field, $context->{start}, int(@$field), @$new_subf
Branch: 1 branch
BranchTrueFalseTotal
if $ind_match 2 1 3
492 if $ind_match;
493 }
494 }
495  
496 151 push @$new_record, $field;
497 }
498  
499 12 $data->{'record'} = $new_record;
500  
501 12 return $data;
502 }
503  
504 266 ▶sub marc_spec {
Subroutine marc_spec: called CC=62 SCAR=41.5
Pod documented
505 266 my $self = $_[0];
506  
507 # $_[1] : data record
508 266 my $data = $_[1];
509 266 my $record = $data->{'record'};
510  
511 # $_[2] : spec
512 266 my ( $ms, $spec );
513 266 ▶ if ( ref $_[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[2]) { } 128 138 266
514 128 $ms = $_[2];
515 128 $spec = $ms->to_string();
516 }
517 else {
518 138 $ms = $self->parse_marc_spec( $_[2] ); # memoized
519 138 $spec = $_[2];
520 }
521  
522 266 my $EMPTY = q{};
523  
524 # $_[3] : opts
525 266 ▶ my $split = $_[3]->{'-split'} // 0;
Condition50%1/2
$_[3]{"-split"} // 0
l!l
2660
MC/DC0%0/1
$_[3]{"-split"} // 0
$_[3]{"-split"}
Truth table50%1/2
A
A= $_[3]{"-split"}
Aresult
11
00
526 266 ▶ my $join_char = $_[3]->{'-join'} // $EMPTY;
Condition66%2/3
$_[3]{"-join"} // $EMPTY
l!l&&r!l&&!r
1281380
MC/DC0%0/2
$_[3]{"-join"} // $EMPTY
$_[3]{"-join"} $EMPTY
Truth table66%2/3
A // B
A= $_[3]{"-join"}B= $EMPTY
ABresult
1X1
011
000
527 266 ▶ my $pluck = $_[3]->{'-pluck'} // 0;
Condition100%2/2
$_[3]{"-pluck"} // 0
l!l
128138
MC/DC100%1/1
$_[3]{"-pluck"} // 0
$_[3]{"-pluck"}
Truth table100%2/2
A
A= $_[3]{"-pluck"}
Aresult
11
00
528 266 ▶ my $value_set = $_[3]->{'-value'} // undef;
Condition100%2/2
$_[3]{"-value"} // undef
l!l
2264
MC/DC100%1/1
$_[3]{"-value"} // undef
$_[3]{"-value"}
Truth table100%2/2
A
A= $_[3]{"-value"}
Aresult
11
00
529 266 ▶ my $invert = $_[3]->{'-invert'} // 0;
Condition100%2/2
$_[3]{"-invert"} // 0
l!l
128138
MC/DC100%1/1
$_[3]{"-invert"} // 0
$_[3]{"-invert"}
Truth table100%2/2
A
A= $_[3]{"-invert"}
Aresult
11
00
530 266 ▶ my $nested_arrays = $_[3]->{'-nested_arrays'} // 0;
Condition100%2/2
$_[3]{"-nested_arrays"} // 0
l!l
128138
MC/DC100%1/1
$_[3]{"-nested_arrays"} // 0
$_[3]{"-nested_arrays"}
Truth table100%2/2
A
A= $_[3]{"-nested_arrays"}
Aresult
11
00
531 266 ▶ my $append = $_[3]->{'-force_array'} // 0;
Condition100%2/2
$_[3]{"-force_array"} // 0
l!l
128138
MC/DC100%1/1
$_[3]{"-force_array"} // 0
$_[3]{"-force_array"}
Truth table100%2/2
A
A= $_[3]{"-force_array"}
Aresult
11
00
532  
533 266 ▶ if ($nested_arrays) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested_arrays) 12 254 266
534 12 $split = 1;
535 }
536  
537 # filter by tag
538 266 my @fields = ();
539 266 my $field_spec = $ms->field;
540 266 my $tag_spec = $field_spec->tag;
541  
542 266 @fields = grep { $_->[0] =~ /$tag_spec/ } @{$record};
543 266 ▶ return unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 6 260 266
544  
545 # calculate char start
546 313 ▶ my $chst = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
547 313 my ($sp) = @_;
548 313 my $char_start;
549 313 ▶ if ( $sp->has_char_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sp->has_char_start) 29 284 313
550 29 ▶ $char_start
Branch: 1 branch
BranchTrueFalseTotal
'#' eq $sp->char_start ? : 5 24 29
551 = ( '#' eq $sp->char_start )
552 ? $sp->char_length * -1
553 : $sp->char_start;
554 }
555 313 return $char_start;
556 260 };
557  
558 # vars we need only for subfields
559 260 my ( @sf_spec, $invert_level, $codes, $invert_chars );
560 260 ▶ if ( $ms->has_subfields ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ms->has_subfields) { } 107 153 260
561  
562 # set the order of subfields
563 107 @sf_spec = map {$_} @{ $ms->subfields };
564 107 ▶ unless ($pluck) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pluck) 106 1 107
565 106 @sf_spec = sort { $a->code cmp $b->code } @sf_spec;
566 }
567  
568 # set invert level default
569 107 $invert_level = 4;
570 107 ▶ if ($invert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invert) 11 96 107
571 11 $codes = '[^';
572 11 $codes .= join $EMPTY, map { $_->code } @sf_spec;
573 11 $codes .= ']';
574 }
575  
576 7 ▶ $invert_chars = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
577 7 my ( $str, $start, $length ) = @_;
578 7 for ( substr $str, $start, $length ) {
579 7 $_ = $EMPTY;
580 }
581 7 return $str;
582 107 };
583 }
584 else {
585 # return $value_set ASAP
586 153 ▶ return $value_set if defined $value_set;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value_set 1 152 153
587 }
588  
589 # vars we need for fields and subfields
590 259 my ( $referred, $char_start, $prev_tag, $index_range );
591 259 my $current_tag = $EMPTY;
592 259 my $tag_index = 0;
593 259 my $index_start = $field_spec->index_start;
594 259 my $index_end = $field_spec->index_end;
595  
596 351 ▶ my $to_referred = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
597 351 my (@values) = @_;
598 351 ▶ if ($nested_arrays) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nested_arrays) { } 39 312 351
elsif ($split) { } 191 121 312
599 39 push @{$referred}, \@values;
600 }
601 elsif ($split) {
602 191 push @{$referred}, @values;
603 }
604 else {
605 121 push @{$referred}, join $join_char, @values;
606 }
607 259 };
608  
609 259 ▶ if ( defined $field_spec->index_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field_spec->index_start) 259 0 259
610 259 $index_range = _get_index_range( $field_spec->index_start,
611 $field_spec->index_end, $#fields );
612 }
613  
614 # iterate over fields
615 259 for my $field (@fields) {
616 541 $prev_tag = $current_tag;
617 541 $current_tag = $field->[0];
618  
619 541 ▶ $tag_index
Branch: 1 branch
BranchTrueFalseTotal
$prev_tag eq $current_tag && defined $tag_index ? : 203 338 541
Condition66%2/3
$prev_tag eq $current_tag and defined $tag_index
!ll&&!rl&&r
3380203
MC/DC50%1/2
$prev_tag eq $current_tag and defined $tag_index
$prev_tag eq $current_tag defined $tag_index
Truth table66%2/3
A and B
A= $prev_tag eq $current_tagB= defined $tag_index
ABresult
0X0
100
111
620 = ( $prev_tag eq $current_tag and defined $tag_index )
621 ? ++$tag_index
622 : 0; #: $field_spec->index_start;
623  
624 # filter by index
625 541 ▶ if ( defined $index_range ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index_range) 541 0 541
626 next
627 unless (
628 541 ▶ Catmandu::Util::array_includes( $index_range, $tag_index ) );
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::array_includes($index_range, $tag_index) 98 443 541
629 }
630  
631 # filter field by subspec
632 443 ▶ if ( $field_spec->has_subspecs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_spec->has_subspecs) 96 347 443
633 96 my $valid = $self->_it_subspecs( $data, $current_tag,
634 $field_spec->subspecs, $tag_index );
635 96 ▶ next unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 68 28 96
636 }
637  
638 375 my @subfields = ();
639  
640 375 ▶ if ( $ms->has_subfields ) { # now we dealing with subfields
Branch: 2 branches
BranchTrueFalseTotal
if ($ms->has_subfields) { } 196 179 375
elsif ($ms->has_indicator) { } 90 89 179
641 196 for my $sf (@sf_spec) {
642  
643 # set invert level
644 220 ▶ if ( $invert && !$sf->has_subspecs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invert and not $sf->has_subspecs) 23 197 220
Condition66%2/3
$invert and not $sf->has_subspecs
!ll&&!rl&&r
197023
MC/DC50%1/2
$invert and not $sf->has_subspecs
$invert not $sf->has_subspecs
Truth table66%2/3
A and not(B)
A= $invertB= not $sf->has_subspecs
ABresult
0X0
100
111
645 23 ▶ if ( -1 == $sf->index_length && !$sf->has_char_start ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-1 == $sf->index_length and not $sf->has_char_start) { } 10 13 23
elsif ($sf->has_char_start) { } 7 6 13
Condition100%3/3
-1 == $sf->index_length and not $sf->has_char_start
!ll&&!rl&&r
10310
MC/DC100%2/2
-1 == $sf->index_length and not $sf->has_char_start
-1 == $sf->index_length not $sf->has_char_start
Truth table100%3/3
A and not(B)
A= -1 == $sf->index_lengthB= not $sf->has_char_start
ABresult
0X0
100
111
646 next
647 10 ▶ if ( $invert_level == 3 )
Branch: 1 branch
BranchTrueFalseTotal
if $invert_level == 3 1 9 10
648 ; # skip subfield spec it's already covered
649 9 $invert_level = 3;
650 }
651 elsif ( $sf->has_char_start ) {
652 7 $invert_level = 1;
653 }
654 else {
655 6 $invert_level = 2;
656 }
657 }
658  
659 219 my @subfield = ();
660 219 ▶ my $code = ( $invert_level == 3 ) ? $codes : $sf->code;
Branch: 1 branch
BranchTrueFalseTotal
$invert_level == 3 ? : 9 210 219
661 219 $code = qr/$code/;
662 219 ▶ for ( my $i = 3; $i < @{$field}; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 490 219 709
663 490 ▶ if ( $field->[$i] =~ /$code/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[$i] =~ /$code/) 304 186 490
664 304 push @subfield, $field->[ $i + 1 ];
665 }
666 }
667  
668 219 ▶ if ( $invert_level == 3 ) { # no index or charpos
Branch: 1 branch
BranchTrueFalseTotal
if ($invert_level == 3) 9 210 219
669 9 ▶ if (@subfield) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subfield) 5 4 9
670 5 push @subfields, @subfield;
671 }
672  
673 9 ▶ if ( $referred && $value_set ) { # return $value_set ASAP
Branch: 1 branch
BranchTrueFalseTotal
if ($referred and $value_set) 0 9 9
Condition33%1/3
$referred and $value_set
!ll&&!rl&&r
900
MC/DC0%0/2
$referred and $value_set
$referred $value_set
Truth table33%1/3
A and B
A= $referredB= $value_set
ABresult
0X0
100
111
674 0 return $value_set;
675 }
676  
677 9 next;
678 }
679  
680 210 ▶ next unless (@subfield);
Branch: 1 branch
BranchTrueFalseTotal
unless @subfield 17 193 210
681  
682 # filter by index
683 193 ▶ if ( defined $sf->index_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sf->index_start) 193 0 193
684 193 my $sf_range
685 = _get_index_range( $sf->index_start, $sf->index_end,
686 $#subfield );
687  
688 193 ▶ if ( $invert_level == 2 ) { # inverted
Branch: 1 branch
BranchTrueFalseTotal
if ($invert_level == 2) { } 5 188 193
689 @subfield = map {
690 5 ▶ Catmandu::Util::array_includes( $sf_range, $_ )
Branch: 1 branch
BranchTrueFalseTotal
Catmandu::Util::array_includes($sf_range, $_) ? : 5 4 9
691 ? ()
692 : $subfield[$_];
693 } 0 .. $#subfield;
694 }
695 else { # without invert
696 @subfield = map {
697 249 ▶ defined $subfield[$_] ? $subfield[$_] : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $subfield[$_] ? : 247 2 249
698 188 } @{$sf_range};
699 }
700 193 ▶ next unless (@subfield);
Branch: 1 branch
BranchTrueFalseTotal
unless @subfield 4 189 193
701 }
702  
703 # return $value_set ASAP
704 189 ▶ return $value_set if $value_set;
Branch: 1 branch
BranchTrueFalseTotal
if $value_set 0 189 189
705  
706 # filter subfield by subspec
707 189 ▶ if ( $sf->has_subspecs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sf->has_subspecs) 16 173 189
708 16 my $valid = $self->_it_subspecs( $data, $field_spec->tag,
709 $sf->subspecs, $tag_index );
710 16 ▶ next unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 8 8 16
711 }
712  
713 # get substring
714 181 $char_start = $chst->($sf);
715 181 ▶ if ( defined $char_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char_start) 19 162 181
716 19 ▶ if ( $invert_level == 1 ) { # inverted
Branch: 1 branch
BranchTrueFalseTotal
if ($invert_level == 1) { } 7 12 19
717 @subfield = map {
718 7 $invert_chars->(
719 $_, $char_start, $sf->char_length
720 );
721 } @subfield;
722 }
723 else {
724 @subfield
725 12 = map { substr $_, $char_start, $sf->char_length; }
726 @subfield;
727 }
728 }
729 181 ▶ next unless @subfield;
Branch: 1 branch
BranchTrueFalseTotal
unless @subfield 0 181 181
730 181 push @subfields, @subfield;
731 } # end of subfield iteration
732 196 ▶ $to_referred->(@subfields) if @subfields;
Branch: 1 branch
BranchTrueFalseTotal
if @subfields 172 24 196
733 } # end of subfield handling
734 elsif ( $ms->has_indicator ) {
735  
736 # filter field by subspec
737 90 ▶ if ( $ms->indicator->has_subspecs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ms->indicator->has_subspecs) 0 90 90
738 0 my $valid = $self->_it_subspecs( $data, $current_tag,
739 $ms->indicator->subspecs, $tag_index );
740 0 ▶ next unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 0 0 0
741 }
742 90 my @indicators = ();
743 90 ▶ push @indicators, $field->[ $ms->indicator->position ]
Branch: 1 branch
BranchTrueFalseTotal
if defined $$field[$ms->indicator->position] 90 0 90
744 if defined $field->[ $ms->indicator->position ];
745 90 $to_referred->(@indicators);
746 }
747 else { # no particular subfields requested
748 89 my @contents = ();
749 89 ▶ for ( my $i = 4; $i < @{$field}; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 132 89 221
750  
751 # get substring
752 132 $char_start = $chst->($field_spec);
753 132 ▶ my $content = ( defined $char_start )
Branch: 1 branch
BranchTrueFalseTotal
defined $char_start ? : 10 122 132
754 ? substr $field->[$i], $char_start,
755 $field_spec->char_length
756 : $field->[$i];
757 132 push @contents, $content;
758 }
759 89 ▶ next unless (@contents);
Branch: 1 branch
BranchTrueFalseTotal
unless @contents 0 89 89
760 89 $to_referred->(@contents);
761 } # end of field handling
762 } # end of field iteration
763 259 ▶ return unless ($referred);
Branch: 1 branch
BranchTrueFalseTotal
unless $referred 11 248 259
764  
765 248 ▶ if ($append) {
Branch: 2 branches
BranchTrueFalseTotal
if ($append) { } 31 217 248
elsif ($split) { } 157 60 217
766 31 ▶ return [$referred] if $split;
Branch: 1 branch
BranchTrueFalseTotal
if $split 13 18 31
767 18 return $referred;
768 }
769 elsif ($split) {
770 157 return [$referred];
771 }
772  
773 60 return join $join_char, @{$referred};
774 }
775  
776 112 ▶sub _it_subspecs {
Subroutine _it_subspecs: called CC=6 SCAR=17.9
777 112 my ( $self, $data, $tag, $subspecs, $tag_index ) = @_;
778  
779 138 ▶ my $set_index = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
780 138 my ($subspec) = @_;
781 138 foreach my $side ( ( 'left', 'right' ) ) {
782 276 ▶ next if ( ref $subspec->$side eq 'MARC::Spec::Comparisonstring' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $subspec->$side eq "MARC::Spec::Comparisonstring" 129 147 276
783  
784 # only set new index if subspec field tag equals spec field tag!!
785 147 my $spec_tag = $subspec->$side->field->tag;
786 147 ▶ next unless ( $tag =~ /$spec_tag/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /$spec_tag/ 33 114 147
787 114 $subspec->$side->field->set_index_start_end($tag_index);
788 }
789 112 };
790  
791 112 my $valid = 1;
792 112 foreach my $subspec ( @{$subspecs} ) {
793 129 ▶ if ( ref $subspec eq 'ARRAY' ) { # chained subSpecs (OR)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subspec eq 'ARRAY') { } 7 122 129
794 7 foreach my $or_subspec ( @{$subspec} ) {
795 16 $set_index->($or_subspec);
796 16 $valid = $self->_validate_subspec( $or_subspec, $data, $tag );
797  
798 # at least one of them is true (OR)
799 16 ▶ last if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 4 12 16
800 }
801 }
802 else { # repeated SubSpecs (AND)
803 122 $set_index->($subspec);
804 122 $valid = $self->_validate_subspec( $subspec, $data, $tag );
805  
806 # all of them have to be true (AND)
807 122 ▶ last unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 73 49 122
808 }
809 }
810 112 return $valid;
811 }
812  
813 138 ▶sub _validate_subspec {
Subroutine _validate_subspec: called CC=23 SCAR=31.4
814 138 my ( $self, $subspec, $data, $tag ) = @_;
815 138 my ( $left_subterm, $right_subterm );
816  
817 138 ▶ if ( '!' ne $subspec->operator && '?' ne $subspec->operator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("!" ne $subspec->operator and "?" ne $subspec->operator) 129 9 138
Condition100%3/3
"!" ne $subspec->operator and "?" ne $subspec->operator
!ll&&!rl&&r
63129
MC/DC100%2/2
"!" ne $subspec->operator and "?" ne $subspec->operator
"!" ne $subspec->operator "?" ne $subspec->operator
Truth table100%3/3
A and B
A= "!" ne $subspec->operatorB= "?" ne $subspec->operator
ABresult
0X0
100
111
818 129 ▶ if ( ref $subspec->left ne 'MARC::Spec::Comparisonstring' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subspec->left ne 'MARC::Spec::Comparisonstring') { } 129 0 129
819 129 my $new_spec = $subspec->left->to_string();
820 129 $new_spec =~ s/^\.\.\./$tag/;
821 129 $left_subterm
822 = $self->marc_spec( $data, $new_spec, { '-split' => 1 } )
823 ; # split should result in an array ref
824 129 ▶ return 0 unless defined $left_subterm;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left_subterm 0 129 129
825 }
826 else {
827 0 push @{$left_subterm}, $subspec->left->comparable;
828 }
829 }
830  
831 138 ▶ if ( ref $subspec->right ne 'MARC::Spec::Comparisonstring' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $subspec->right ne 'MARC::Spec::Comparisonstring') { } 9 129 138
832 9 my $new_spec = $subspec->right->to_string();
833 9 $new_spec =~ s/^\.\.\./$tag/;
834 9 $right_subterm
835 = $self->marc_spec( $data, $new_spec, { '-split' => 1 } )
836 ; # split should result in an array ref
837 9 ▶ unless ( defined $right_subterm ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $right_subterm) 5 4 9
838 5 $right_subterm = [];
839 }
840 }
841 else {
842 129 push @{$right_subterm}, $subspec->right->comparable;
843 }
844  
845 138 ▶ if ( $subspec->operator eq '?' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subspec->operator eq "?") 3 135 138
846 3 ▶ return ( @{$right_subterm} ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$right_subterm;} ? : 2 1 3
847 }
848  
849 135 ▶ if ( $subspec->operator eq '!' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subspec->operator eq "!") 6 129 135
850 6 ▶ return ( @{$right_subterm} ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@{$right_subterm;} ? : 2 4 6
851 }
852  
853 129 ▶ if ( $subspec->operator eq '=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subspec->operator eq "=") 114 15 129
854 114 foreach my $v ( @{ $left_subterm->[0] } ) {
855 121 ▶ return 1 if List::Util::any { $v eq $_ } @{$right_subterm};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $v eq $_; } , @{$right_subterm;}) 38 83 121
856 }
857 }
858  
859 91 ▶ if ( $subspec->operator eq '!=' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subspec->operator eq "!=") 8 83 91
860 8 foreach my $v ( @{ $left_subterm->[0] } ) {
861 12 ▶ return 0 if List::Util::any { $v eq $_ } @{$right_subterm};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $v eq $_; } , @{$right_subterm;}) 3 9 12
862 }
863 5 return 1;
864 }
865  
866 83 ▶ if ( $subspec->operator eq '~' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subspec->operator eq "~") 3 80 83
867 3 foreach my $v ( @{ $left_subterm->[0] } ) {
868 3 ▶ return 1 if List::Util::any { $v =~ /$_/ } @{$right_subterm};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $v =~ /$_/; } , @{$right_subterm;}) 1 2 3
869 }
870 }
871  
872 82 ▶ if ( $subspec->operator eq '!~' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subspec->operator eq "!~") 4 78 82
873 4 foreach my $v ( @{ $left_subterm->[0] } ) {
874 4 ▶ return 0 if List::Util::any { $v =~ /$_/ } @{$right_subterm};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &List::Util::any(sub { $v =~ /$_/; } , @{$right_subterm;}) 1 3 4
875 }
876 3 return 1;
877 }
878  
879 78 return 0;
880 }
881  
882 162 ▶sub parse_marc_spec {
Subroutine parse_marc_spec: called CC=1 SCAR=0.0
883 162 my ( $self, $marc_spec ) = @_;
884 162 return MARC::Spec::Parser->new($marc_spec)->marcspec;
885 }
886  
887 47 ▶sub _get_index_range {
Subroutine _get_index_range: called CC=9 SCAR=22.1
888 47 my ( $index_start, $index_end, $last_index ) = @_;
889  
890 47 ▶ if ( '#' eq $index_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('#' eq $index_start) { } 9 38 47
891 9 ▶ if ( '#' eq $index_end or 0 == $index_end ) { return [$last_index]; }
Branch: 1 branch
BranchTrueFalseTotal
if ("#" eq $index_end or 0 == $index_end) 8 1 9
Condition66%2/3
"#" eq $index_end or 0 == $index_end
l!l&&r!l&&!r
801
MC/DC50%1/2
"#" eq $index_end or 0 == $index_end
"#" eq $index_end 0 == $index_end
Truth table66%2/3
A or B
A= "#" eq $index_endB= 0 == $index_end
ABresult
1X1
011
000
892 1 $index_start = $last_index;
893 1 $index_end = $last_index - $index_end;
894 0 ▶ if ( 0 > $index_end ) { $index_end = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (0 > $index_end) 0 1 1
895 }
896 else {
897 38 ▶ if ( $last_index < $index_start ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_index < $index_start) 1 37 38
898 1 return [$index_start];
899 } # this will result to no hits
900 }
901  
902 38 ▶ if ( '#' eq $index_end or $index_end > $last_index ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("#" eq $index_end or $index_end > $last_index) 15 23 38
Condition100%3/3
"#" eq $index_end or $index_end > $last_index
l!l&&r!l&&!r
14123
MC/DC100%2/2
"#" eq $index_end or $index_end > $last_index
"#" eq $index_end $index_end > $last_index
Truth table100%3/3
A or B
A= "#" eq $index_endB= $index_end > $last_index
ABresult
1X1
011
000
903 15 $index_end = $last_index;
904 }
905  
906 38 ▶ return ( $index_start <= $index_end )
Branch: 1 branch
BranchTrueFalseTotal
$index_start <= $index_end ? : 37 1 38
907 ? [ $index_start .. $index_end ]
908 : [ $index_end .. $index_start ];
909 }
910  
911 15 ▶sub marc_xml {
Subroutine marc_xml: called CC=3 SCAR=11.1
Pod documented
912 15 my ( $self, $data, %opts ) = @_;
913  
914 15 ▶ if ( $opts{reverse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'reverse'}) { } 1 14 15
915 1 my $record
916 = Catmandu->import_from_string( $data, 'MARC', type => 'XML' );
917 1 ▶ return $record->[0]->{record} if $record;
Branch: 1 branch
BranchTrueFalseTotal
if $record 1 0 1
918 0 return undef;
919 }
920 else {
921 14 my $xml;
922 14 my $exporter = Catmandu::Exporter::MARC::XML->new(
923 file => \$xml,
924 xml_declaration => 0,
925 collection => 0
926 );
927 14 $exporter->add( { record => $data } );
928 14 $exporter->commit;
929  
930 14 return $xml;
931 }
932 }
933  
934 1 ▶sub marc_record_to_json {
Subroutine marc_record_to_json: called CC=11 SCAR=24.4
Pod undocumented
935 1 my ( $self, $data, %opts ) = @_;
936  
937 1 ▶ if ( my $marc = delete $data->{'record'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $marc = delete $data->{"record"}) 1 0 1
938 1 for my $field (@$marc) {
939 6 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
940  
941 6 ▶ if ( $tag eq 'LDR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'LDR') { } 1 5 6
elsif ($tag eq 'FMT' or substr($tag, 0, 2) eq '00') { } 2 3 5
942 1 shift @subfields;
943 1 $data->{leader} = join "", @subfields;
944 }
945 ▶ elsif ( $tag eq 'FMT' || substr( $tag, 0, 2 ) eq '00' ) {
Condition66%2/3
$tag eq "FMT" or substr($tag, 0, 2) eq "00"
l!l&&r!l&&!r
023
MC/DC50%1/2
$tag eq "FMT" or substr($tag, 0, 2) eq "00"
$tag eq "FMT" substr($tag, 0, 2) eq "00"
Truth table66%2/3
A or B
A= $tag eq "FMT"B= substr($tag, 0, 2) eq "00"
ABresult
1X1
011
000
946 2 shift @subfields;
947 2 ▶ push @{ $data->{fields} ||= [] },
Condition100%2/2
$data->{"fields"} ||= []
l!l
11
MC/DC100%1/1
$data->{"fields"} ||= []
$data->{"fields"}
Truth table100%2/2
A
A= $data->{"fields"}
Aresult
11
00
948 { $tag => join "", @subfields };
949 }
950 else {
951 3 my @sf;
952 3 ▶ my $start = !defined( $subfields[0] )
Branch: 1 branch
BranchTrueFalseTotal
!defined($subfields[0]) || $subfields[0] eq '_' ? : 3 0 3
Condition33%1/3
not defined $subfields[0] or $subfields[0] eq "_"
l!l&&r!l&&!r
030
MC/DC0%0/2
not defined $subfields[0] or $subfields[0] eq "_"
not defined $subfields[0] $subfields[0] eq "_"
Truth table33%1/3
not(A) or B
A= not defined $subfields[0]B= $subfields[0] eq "_"
ABresult
1X1
011
000
953 || $subfields[0] eq '_' ? 2 : 0;
954 3 ▶ for ( my $i = $start; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 5 3 8
955 5 push @sf, { $subfields[$i] => $subfields[ $i + 1 ] };
956 }
957 3 ▶ push @{ $data->{fields} ||= [] },
Condition50%1/2
$data->{"fields"} ||= []
l!l
30
MC/DC0%0/1
$data->{"fields"} ||= []
$data->{"fields"}
Truth table50%1/2
A
A= $data->{"fields"}
Aresult
11
00
958 { $tag =>
959 { subfields => \@sf, ind1 => $ind1, ind2 => $ind2 } };
960 }
961 }
962 }
963  
964 1 $data;
965 }
966  
967 1 ▶sub marc_json_to_record {
Subroutine marc_json_to_record: called CC=12 SCAR=25.4
Pod undocumented
968 1 my ( $self, $data, %opts ) = @_;
969  
970 1 my $record = [];
971  
972 1 ▶ if ( Catmandu::Util::is_string( $data->{leader} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Catmandu::Util::is_string($data->{"leader"})) 1 0 1
973 1 push @$record, [ 'LDR', undef, undef, '_', $data->{leader} ],;
974 }
975  
976 1 ▶ if ( Catmandu::Util::is_array_ref( $data->{fields} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Catmandu::Util::is_array_ref($data->{"fields"})) 1 0 1
977 1 for my $field ( @{ $data->{fields} } ) {
978 5 ▶ next unless Catmandu::Util::is_hash_ref($field);
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::is_hash_ref($field) 0 5 5
979  
980 5 my ($tag) = keys %$field;
981 5 my $val = $field->{$tag};
982  
983 5 ▶ if ( $tag eq 'FMT' || substr( $tag, 0, 2 ) eq '00' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'FMT' or substr($tag, 0, 2) eq '00') { } 2 3 5
elsif (Catmandu::Util::is_hash_ref($val)) { } 3 0 3
Condition66%2/3
$tag eq "FMT" or substr($tag, 0, 2) eq "00"
l!l&&r!l&&!r
023
MC/DC50%1/2
$tag eq "FMT" or substr($tag, 0, 2) eq "00"
$tag eq "FMT" substr($tag, 0, 2) eq "00"
Truth table66%2/3
A or B
A= $tag eq "FMT"B= substr($tag, 0, 2) eq "00"
ABresult
1X1
011
000
984 2 push @$record, [ $tag, undef, undef, '_', $val ],;
985 }
986 elsif ( Catmandu::Util::is_hash_ref($val) ) {
987 3 my $ind1 = $val->{ind1};
988 3 my $ind2 = $val->{ind2};
989 3 ▶ next unless Catmandu::Util::is_array_ref( $val->{subfields} );
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::is_array_ref($val->{"subfields"}) 0 3 3
990  
991 3 my $sfs = [ '_', '' ];
992 3 for my $sf ( @{ $val->{subfields} } ) {
993 5 ▶ next unless Catmandu::Util::is_hash_ref($sf);
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::is_hash_ref($sf) 0 5 5
994  
995 5 my ($code) = keys %$sf;
996 5 my $sval = $sf->{$code};
997  
998 5 push @$sfs, [ $code, $sval ];
999 }
1000  
1001 3 push @$record, [ $tag, $ind1, $ind2, @$sfs ];
1002 }
1003 }
1004 }
1005  
1006 1 ▶ if ( @$record > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$record > 0) 1 0 1
1007 1 delete $data->{fields};
1008 1 delete $data->{leader};
1009 1 $data->{'record'} = $record;
1010 }
1011  
1012 1 $data;
1013 }
1014  
1015 1 ▶sub marc_decode_dollar_subfields {
Subroutine marc_decode_dollar_subfields: called CC=4 SCAR=13.9
Pod documented
1016 1 my ( $self, $data, %opts ) = @_;
1017 1 my $old_record = $data->{'record'};
1018 1 my $new_record = [];
1019  
1020 1 for my $field (@$old_record) {
1021 14 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
1022  
1023 14 my $fixed_field = [ $tag, $ind1, $ind2 ];
1024  
1025 14 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 30 14 44
1026 30 my $code = $subfields[$i];
1027 30 my $value = $subfields[ $i + 1 ];
1028  
1029 # If a subfield contains fields coded like: data$xmore$yevenmore
1030 # chunks = (data,x,y,evenmore)
1031 30 my @chunks = split( /\$([a-z])/, $value );
1032  
1033 30 my $real_value = shift @chunks;
1034  
1035 30 push @$fixed_field, ( $code, $real_value );
1036  
1037 30 ▶ while (@chunks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chunks) 2 30 32
1038 2 push @$fixed_field, ( splice @chunks, 0, 2 );
1039 }
1040 }
1041  
1042 14 push @$new_record, $fixed_field;
1043 }
1044  
1045 1 $data->{'record'} = $new_record;
1046  
1047 1 $data;
1048 }
1049  
1050 119 ▶sub compile_marc_path {
Subroutine compile_marc_path: called CC=10 SCAR=23.1
1051 119 my ( $self, $marc_path, %opts ) = @_;
1052  
1053 119 my ($field, $field_regex, $ind1, $ind2,
1054 $subfield, $subfield_regex, $from, $to,
1055 $len, $is_regex_field
1056 );
1057  
1058 119 my $MARC_PATH_REGEX
1059 = qr/(\S{1,3})(\[([^,])?,?([^,])?\])?([\$_a-zA-Z0-9^-]+)?(\/([0-9]+)(-([0-9]+))?)?/;
1060 119 ▶ if ( $marc_path =~ $MARC_PATH_REGEX ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($marc_path =~ /$MARC_PATH_REGEX/) { } 119 0 119
1061 119 $field = $1;
1062 119 $ind1 = $3;
1063 119 $ind2 = $4;
1064 119 $subfield = $5;
1065 119 $field = "0" x ( 3 - length($field) )
1066 . $field; # fixing 020 treated as 20 bug
1067 119 ▶ if ( defined($subfield) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $subfield) { } 66 53 119
elsif ($opts{'subfield_default'}) { } 1 52 53
elsif ($opts{'subfield_wildcard'}) { } 36 16 52
1068 66 $subfield =~ s{\$}{}g;
1069 66 ▶ unless ( $subfield =~ /^[a-zA-Z0-9]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subfield =~ /^[a-zA-Z0-9]$/) 13 53 66
1070 13 $subfield = "[$subfield]";
1071 }
1072 }
1073 elsif ( $opts{subfield_default} ) {
1074 1 ▶ $subfield = $field =~ /^0|LDR|FMT/ ? '_' : 'a';
Branch: 1 branch
BranchTrueFalseTotal
$field =~ /^0|LDR|FMT/ ? : 1 0 1
1075 }
1076 elsif ( $opts{subfield_wildcard} ) {
1077 36 $subfield = '[a-z0-9_]';
1078 }
1079 119 ▶ if ( defined($subfield) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subfield) 103 16 119
1080 103 $subfield_regex = qr/^(?:${subfield})$/;
1081 }
1082 119 $from = $7;
1083 119 $to = $9;
1084 119 ▶ $len = defined $to ? $to - $from + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $to ? : 5 114 119
1085 }
1086 else {
1087 0 return undef;
1088 }
1089  
1090 119 ▶ if ( $field =~ /[\*\.]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ /[\*\.]/) { } 5 114 119
1091 5 $field_regex = $field;
1092 5 $field_regex =~ s/[\*\.]/(?:[A-Z0-9])/g;
1093 5 $is_regex_field = 1;
1094 5 $field_regex = qr/^$field_regex$/;
1095 }
1096 else {
1097 114 $is_regex_field = 0;
1098 }
1099  
1100 return {
1101 119 field => $field,
1102 field_regex => $field_regex,
1103 is_regex_field => $is_regex_field,
1104 subfield => $subfield,
1105 subfield_regex => $subfield_regex,
1106 ind1 => $ind1,
1107 ind2 => $ind2,
1108 start => 3,
1109 from => $from,
1110 to => $to,
1111 len => $len
1112 };
1113 }
1114  
1115 20 ▶sub marc_copy {
Subroutine marc_copy: called CC=36 SCAR=46.1
Pod documented
1116 20 my $self = $_[0];
1117 20 my $data = $_[1];
1118 20 my $marc_path = $_[2];
1119 20 my $marc_value = $_[3];
1120 20 my $is_cut = $_[4];
1121  
1122 # $_[2] : marc_path
1123 20 ▶ my $context
Branch: 1 branch
BranchTrueFalseTotal
ref $marc_path ? : 20 0 20
1124 = ref($marc_path)
1125 ? $marc_path
1126 : $self->compile_marc_path( $_[2], subfield_wildcard => 0 );
1127  
1128 20 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 20 20
1129  
1130 # $_[1] : data record
1131 20 my $record = $data->{'record'};
1132  
1133 20 ▶ return wantarray ? () : undef
Branch: 2 branches
BranchTrueFalseTotal
unless defined $record and ref $record eq "ARRAY" 0 20 20
wantarray ? : 0 0 0
Condition33%1/3
defined $record and ref $record eq "ARRAY"
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $record and ref $record eq "ARRAY"
defined $record ref $record eq "ARRAY"
Truth table33%1/3
A and B
A= defined $recordB= ref $record eq "ARRAY"
ABresult
0X0
100
111
1134 unless ( defined $record && ref($record) eq 'ARRAY' );
1135  
1136 # When is_cut is on, we need to create a new record containing the remaining fields
1137 20 my @new_record = ();
1138  
1139 20 my $fields = [];
1140  
1141 20 for my $field (@$record) {
1142 100 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
1143  
1144 100 ▶ if (( $context->{is_regex_field} == 0 && $tag ne $context->{field} )
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"is_regex_field"} == 0 and $tag ne $context->{"field"} or $context->{"is_regex_field"} == 1 and not $tag =~ /$context->{"field_regex"}/) 70 30 100
Condition77%7/9
$context->{"is_regex_field"} == 0 and $tag ne $context->{"field"} or $context->{"is_regex_field"} == 1 and not $tag =~ /$context->{"field_regex"}/
l!l&&r!l&&!r
70030
$context->{"is_regex_field"} == 0 and $tag ne $context->{"field"}
!ll&&!rl&&r
102070
$context->{"is_regex_field"} == 1 and not $tag =~ /$context->{"field_regex"}/
!ll&&!rl&&r
20100
MC/DC50%2/4
$context->{"is_regex_field"} == 0 and $tag ne $context->{"field"} or $context->{"is_regex_field"} == 1 and not $tag =~ /$context->{"field_regex"}/
$context->{"is_regex_field"} == 0 $tag ne $context->{"field"} $context->{"is_regex_field"} == 1 not $tag =~ /$context->{"field_regex"}/
Truth table42%3/7
A and B or C and not(D)
A= $context->{"is_regex_field"} == 0B= $tag ne $context->{"field"}C= $context->{"is_regex_field"} == 1D= not $tag =~ /$context->{"field_regex"}/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1145 || ( $context->{is_regex_field} == 1
1146 && $tag !~ $context->{field_regex} )
1147 )
1148 {
1149 70 ▶ push @new_record, $field if $is_cut;
Branch: 1 branch
BranchTrueFalseTotal
if $is_cut 23 47 70
1150 70 next;
1151 }
1152  
1153 30 ▶ if ( defined $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind1"}) 0 30 30
1154 0 ▶ if ( !defined $ind1 || $ind1 ne $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind1 or $ind1 ne $context->{"ind1"}) 0 0 0
Condition0%0/3
not defined $ind1 or $ind1 ne $context->{"ind1"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind1 or $ind1 ne $context->{"ind1"}
not defined $ind1 $ind1 ne $context->{"ind1"}
Truth table0%0/3
not(A) or B
A= not defined $ind1B= $ind1 ne $context->{"ind1"}
ABresult
1X1
011
000
1155 0 ▶ push @new_record, $field if $is_cut;
Branch: 1 branch
BranchTrueFalseTotal
if $is_cut 0 0 0
1156 0 next;
1157 }
1158 }
1159 30 ▶ if ( defined $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind2"}) 0 30 30
1160 0 ▶ if ( !defined $ind2 || $ind2 ne $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind2 or $ind2 ne $context->{"ind2"}) 0 0 0
Condition0%0/3
not defined $ind2 or $ind2 ne $context->{"ind2"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind2 or $ind2 ne $context->{"ind2"}
not defined $ind2 $ind2 ne $context->{"ind2"}
Truth table0%0/3
not(A) or B
A= not defined $ind2B= $ind2 ne $context->{"ind2"}
ABresult
1X1
011
000
1161 0 ▶ push @new_record, $field if $is_cut;
Branch: 1 branch
BranchTrueFalseTotal
if $is_cut 0 0 0
1162 0 next;
1163 }
1164 }
1165  
1166 30 ▶ if ( $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'subfield'}) { } 6 24 30
1167 6 my $found = 0;
1168 6 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 12 6 18
1169 12 ▶ if ( $subfields[$i] =~ $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfields[$i] =~ /$context->{"subfield"}/) 4 8 12
1170 4 ▶ if ( defined($marc_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $marc_value) { } 2 2 4
1171 2 ▶ $found = 1 if $subfields[ $i + 1 ] =~ /$marc_value/;
Branch: 1 branch
BranchTrueFalseTotal
if $subfields[$i + 1] =~ /$marc_value/ 2 0 2
1172 }
1173 else {
1174 2 $found = 1;
1175 }
1176 }
1177 }
1178  
1179 6 ▶ unless ($found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 2 4 6
1180 2 ▶ push @new_record, $field if $is_cut;
Branch: 1 branch
BranchTrueFalseTotal
if $is_cut 1 1 2
1181 2 next;
1182 }
1183 }
1184 else {
1185 24 ▶ if ( defined($marc_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $marc_value) 0 24 24
1186 0 my @sf = ();
1187 0 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 0 0 0
1188 0 push @sf, $subfields[ $i + 1 ];
1189 }
1190  
1191 0 my $string = join "", @sf;
1192  
1193 0 ▶ unless ( $string =~ /$marc_value/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /$marc_value/) 0 0 0
1194 0 ▶ push @new_record, $field if $is_cut;
Branch: 1 branch
BranchTrueFalseTotal
if $is_cut 0 0 0
1195 0 next;
1196 }
1197 }
1198 }
1199  
1200 28 my $f = {};
1201 28 $f->{tag} = $field->[0];
1202  
1203 # indicator 1
1204 28 ▶ if ( defined $field->[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->[1]) { } 28 0 28
1205 28 $f->{ind1} = $field->[1];
1206 }
1207 else {
1208 0 $f->{ind1} = undef;
1209 }
1210  
1211 # indicator 2
1212 28 ▶ if ( defined $field->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->[2]) { } 28 0 28
1213 28 $f->{ind2} = $field->[2];
1214 }
1215 else {
1216 0 $f->{ind2} = undef;
1217 }
1218  
1219 # fixed fields
1220 28 ▶ if ( $field->[3] eq '_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->[3] eq "_") 12 16 28
1221 12 $f->{content} = $field->[4];
1222 12 push( @$fields, $f );
1223 12 next;
1224 }
1225  
1226 # subfields
1227 16 ▶ for ( my $i = $context->{start}; $i < @{$field}; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$field;}) 28 16 44
1228 push(
1229 28 @{ $f->{subfields} },
1230 { $field->[$i] => $field->[ $i + 1 ] }
1231 );
1232 }
1233  
1234 16 push( @$fields, $f );
1235 }
1236  
1237 20 ▶ if ($is_cut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_cut) 7 13 20
1238 7 $data->{record} = \@new_record;
1239 }
1240  
1241 20 [$fields];
1242 }
1243  
1244 6 ▶sub marc_paste {
Subroutine marc_paste: called CC=39 SCAR=48.1
Pod documented
1245 6 my $self = $_[0];
1246 6 my $data = $_[1];
1247 6 my $json_path = $_[2];
1248 6 my $marc_path = $_[3];
1249 6 my $marc_value = $_[4];
1250  
1251 6 my $value = Catmandu::Util::data_at( $json_path, $data );
1252  
1253 6 ▶ return $data
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::is_array_ref($value) or Catmandu::Util::is_hash_ref($value) 0 6 6
Condition33%1/3
Catmandu::Util::is_array_ref($value) or Catmandu::Util::is_hash_ref($value)
l!l&&r!l&&!r
600
MC/DC0%0/2
Catmandu::Util::is_array_ref($value) or Catmandu::Util::is_hash_ref($value)
Catmandu::Util::is_array_ref($value) Catmandu::Util::is_hash_ref($value)
Truth table33%1/3
A or B
A= Catmandu::Util::is_array_ref($value)B= Catmandu::Util::is_hash_ref($value)
ABresult
1X1
011
000
1254 unless Catmandu::Util::is_array_ref($value)
1255 || Catmandu::Util::is_hash_ref($value);
1256  
1257 6 ▶ $value = [$value] unless Catmandu::Util::is_array_ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::is_array_ref($value) 0 6 6
1258  
1259 6 my @new_parts;
1260  
1261 6 for my $part (@$value) {
1262 return $data
1263 unless Catmandu::Util::is_hash_ref($part)
1264 && exists $part->{tag}
1265 && exists $part->{ind1}
1266 && exists $part->{ind2}
1267 6 ▶ && ( exists $part->{content} || exists $part->{subfields} );
Branch: 1 branch
BranchTrueFalseTotal
unless Catmandu::Util::is_hash_ref($part) and exists $part->{"tag"} and exists $part->{"ind1"} and exists $part->{"ind2"} and exists $part->{"content"} || exists $part->{"subfields"} 0 6 6
Condition33%5/15
Catmandu::Util::is_hash_ref($part) and exists $part->{"tag"} and exists $part->{"ind1"} and exists $part->{"ind2"} and exists $part->{"content"} || exists $part->{"subfields"}
!ll&&!rl&&r
006
Catmandu::Util::is_hash_ref($part) and exists $part->{"tag"} and exists $part->{"ind1"} and exists $part->{"ind2"}
!ll&&!rl&&r
006
Catmandu::Util::is_hash_ref($part) and exists $part->{"tag"} and exists $part->{"ind1"}
!ll&&!rl&&r
006
Catmandu::Util::is_hash_ref($part) and exists $part->{"tag"}
!ll&&!rl&&r
006
exists $part->{"content"} or exists $part->{"subfields"}
l!l&&r!l&&!r
600
MC/DC0%0/6
Catmandu::Util::is_hash_ref($part) and exists $part->{"tag"} and exists $part->{"ind1"} and exists $part->{"ind2"} and exists $part->{"content"} || exists $part->{"subfields"}
Catmandu::Util::is_hash_ref($part) exists $part->{"tag"} exists $part->{"ind1"} exists $part->{"ind2"} exists $part->{"content"} exists $part->{"subfields"}
Truth table14%1/7
A and B and C and D and E or F
A= Catmandu::Util::is_hash_ref($part)B= exists $part->{"tag"}C= exists $part->{"ind1"}D= exists $part->{"ind2"}E= exists $part->{"content"}F= exists $part->{"subfields"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
1111000
11111X1
1111011
1268  
1269 6 my $tag = $part->{tag};
1270 6 ▶ my $ind1 = $part->{ind1} // ' ';
Condition50%1/2
$part->{"ind1"} // " "
l!l
60
MC/DC0%0/1
$part->{"ind1"} // " "
$part->{"ind1"}
Truth table50%1/2
A
A= $part->{"ind1"}
Aresult
11
00
1271 6 ▶ my $ind2 = $part->{ind2} // ' ';
Condition50%1/2
$part->{"ind2"} // " "
l!l
60
MC/DC0%0/1
$part->{"ind2"} // " "
$part->{"ind2"}
Truth table50%1/2
A
A= $part->{"ind2"}
Aresult
11
00
1272 6 my $content = $part->{content};
1273 6 my $subfields = $part->{subfields};
1274  
1275 6 ▶ if ( defined($content) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $content) { } 6 0 6
elsif (defined $subfields and Catmandu::Util::is_array_ref($subfields)) { } 0 0 0
1276 6 push @new_parts, [ $tag, $ind1, $ind2, '_', $content ];
1277 }
1278 ▶ elsif ( defined($subfields)
Condition0%0/3
defined $subfields and Catmandu::Util::is_array_ref($subfields)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subfields and Catmandu::Util::is_array_ref($subfields)
defined $subfields Catmandu::Util::is_array_ref($subfields)
Truth table0%0/3
A and B
A= defined $subfieldsB= Catmandu::Util::is_array_ref($subfields)
ABresult
0X0
100
111
1279 && Catmandu::Util::is_array_ref($subfields) )
1280 {
1281 0 my @tmp = ( $tag, $ind1, $ind2 );
1282  
1283 0 for my $sf (@$subfields) {
1284 0 ▶ while ( my ( $key, $value ) = each %$sf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sf) 0 0 0
1285 0 push @tmp, $key, $value;
1286 }
1287 }
1288  
1289 0 push @new_parts, [@tmp];
1290 }
1291 else {
1292 # Illegal input
1293 0 return $data;
1294 }
1295 }
1296  
1297 6 ▶ if ( defined($marc_path) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $marc_path) { } 5 1 6
1298 5 my $context
1299 = $self->compile_marc_path( $marc_path, subfield_wildcard => 0 );
1300  
1301 5 ▶ confess "invalid marc path" unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 5 5
1302  
1303 5 my @record = @{ $data->{record} };
1304 5 my $found_match = undef;
1305  
1306 5 my $field_position = -1;
1307  
1308 5 for my $field (@record) {
1309 25 $field_position++;
1310 25 my ( $tag, $ind1, $ind2, @subfields ) = @$field;
1311  
1312 25 ▶ if ( $context->{is_regex_field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'is_regex_field'}) { } 0 25 25
1313 0 ▶ next unless $tag =~ $context->{field_regex};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /$context->{"field_regex"}/ 0 0 0
1314 }
1315 else {
1316 25 ▶ next unless $tag eq $context->{field};
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq $context->{"field"} 20 5 25
1317 }
1318  
1319 5 ▶ if ( defined $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind1"}) 0 5 5
1320 0 ▶ if ( !defined $ind1 || $ind1 ne $context->{ind1} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind1 or $ind1 ne $context->{"ind1"}) 0 0 0
Condition0%0/3
not defined $ind1 or $ind1 ne $context->{"ind1"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind1 or $ind1 ne $context->{"ind1"}
not defined $ind1 $ind1 ne $context->{"ind1"}
Truth table0%0/3
not(A) or B
A= not defined $ind1B= $ind1 ne $context->{"ind1"}
ABresult
1X1
011
000
1321 0 next;
1322 }
1323 }
1324 5 ▶ if ( defined $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->{"ind2"}) 0 5 5
1325 0 ▶ if ( !defined $ind2 || $ind2 ne $context->{ind2} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ind2 or $ind2 ne $context->{"ind2"}) 0 0 0
Condition0%0/3
not defined $ind2 or $ind2 ne $context->{"ind2"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $ind2 or $ind2 ne $context->{"ind2"}
not defined $ind2 $ind2 ne $context->{"ind2"}
Truth table0%0/3
not(A) or B
A= not defined $ind2B= $ind2 ne $context->{"ind2"}
ABresult
1X1
011
000
1326 0 next;
1327 }
1328 }
1329  
1330 5 ▶ if ( $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'subfield'}) { } 3 2 5
1331 3 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 6 3 9
1332 6 ▶ if ( $subfields[$i] =~ $context->{subfield} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfields[$i] =~ /$context->{"subfield"}/) 2 4 6
1333 2 ▶ if ( defined($marc_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $marc_value) { } 1 1 2
1334 1 ▶ $found_match = $field_position
Branch: 1 branch
BranchTrueFalseTotal
if $subfields[$i + 1] =~ /$marc_value/ 1 0 1
1335 if $subfields[ $i + 1 ] =~ /$marc_value/;
1336 }
1337 else {
1338 1 $found_match = $field_position;
1339 }
1340 }
1341 }
1342 }
1343 else {
1344 2 ▶ if ( defined($marc_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $marc_value) { } 1 1 2
1345 1 my @sf = ();
1346 1 ▶ for ( my $i = 0; $i < @subfields; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @subfields) 2 1 3
1347 2 push @sf, $subfields[ $i + 1 ];
1348 }
1349  
1350 1 my $string = join "", @sf;
1351  
1352 1 ▶ if ( $string =~ /$marc_value/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$marc_value/) { } 1 0 1
1353 1 $found_match = $field_position;
1354 }
1355 else {
1356 # don't match anything
1357 }
1358 }
1359 else {
1360 1 $found_match = $field_position;
1361 }
1362 }
1363 }
1364  
1365 5 ▶ if ( defined $found_match ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found_match) 4 1 5
1366 4 my @new_record = (
1367 @record[ 0 .. $found_match ],
1368 @new_parts, @record[ $found_match + 1 .. $#record ]
1369 );
1370 4 $data->{record} = \@new_record;
1371 }
1372 }
1373 else {
1374 1 push @{ $data->{record} }, @new_parts;
1375 }
1376  
1377 6 $data;
1378 }
1379  
1380 2 ▶sub marc_sort {
Subroutine marc_sort: called CC=5 SCAR=16.1
Pod documented
1381 2 my ( $self, $data ) = @_;
1382 2 ▶ if ( defined $data->{record} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"record"}) 2 0 2
1383 18 my @record = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map {
1384 18 ▶ [ $_,
Branch: 3 branches
BranchTrueFalseTotal
$_->[0] =~ /^FMT$/ ? : 1 17 18
$_->[0] =~ /^LDR$/ ? : 2 15 17
$_->[0] =~ /^[A-Z]/ ? : 3 12 15
1385 $_->[0] =~ m/^FMT$/ ? 0.1
1386 : $_->[0] =~ m/^LDR$/ ? 0.2
1387 : $_->[0] =~ m/^[A-Z]/ ? 99 . ord( substr( $_->[0], 0 ) )
1388 : $_->[0]
1389 ]
1390 2 } @{ $data->{record} };
1391 2 $data->{record} = \@record;
1392 }
1393 2 return $data;
1394 }
1395  
1396 33 1;
1397  
1398 __END__
↑ Summary Exporter/MARC/ALEPHSEQ.pm »