Record.pm

filter: Statementstmt 90.5% 353 / 390 Branchbran 80.3% 143 / 178 Conditioncond 73.6% 42 / 57 MC/DCmcdc 58.3% 21 / 36 Subroutinesub 94.1% 48 / 51 Podpod 100% 31 / 31 totaltotal 85.8% 638 / 743 CC 130 SCAR 21.8
CC
130
Cov
86%
add_headers
20.0
holdings
16.8
subfield
14.0
CRAP 8.9
« PlainParser.pm ↑ Summary Store.pm »
1 package PICA::Record;
2 {
3 13 $PICA::Record::VERSION = '0.585';
4 }
5 #ABSTRACT: Perl module for handling PICA+ records
6 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 our @EXPORT = qw(readpicarecord writepicarecord);
12 13 our @EXPORT_OK = qw(picarecord pgrep pmap);
13 13 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
14  
15 13 our $XMLNAMESPACE = 'info:srw/schema/5/picaXML-v1.0';
16  
17 13 our @CARP_NOT = qw(PICA::Field PICA::Parser);
18  
19 13 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶use PICA::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use PICA::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 13 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 13 ▶use PerlIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 13 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 use overload
30 48 ▶ 'bool' => sub { ! $_[0]->empty },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
31 13 ▶ '""' => sub { $_[0]->string };
Subroutine __ANON__: called CC=1 SCAR=0.0
32  
33 13 ▶use sort 'stable';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35  
36 # private method to append a field
37 11453 ▶my $append_field = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
38 11453 my ($self, $field) = @_;
39 # confess('append_failed') unless ref($field) eq 'PICA::Field';
40 11453 ▶ if ( $field->tag eq '003@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->tag eq "003\@") 71 11382 11453
41 71 $self->{_ppn} = $field->sf('0');
42 71 ▶ if ( $self->field('003@') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->field("003\@")) 2 69 71
43 2 $self->update( '003@', $field );
44 2 return 0;
45 }
46 }
47 # TODO: limit occ and iln, epn
48 11451 ▶ return 0 if $field->empty;
Branch: 1 branch
BranchTrueFalseTotal
if $field->empty 2 11449 11451
49  
50 11449 push(@{ $self->{_fields} }, $field);
51 11449 return 1;
52 13 };
53  
54 # private method to compile and cache a regular expression
55 13 my %field_regex;
56 692 ▶my $get_regex = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
57 692 my $reg = shift;
58  
59 692 ▶ return $reg if ref($reg) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $reg eq "Regexp" 2 690 692
60  
61 690 my $regex = $field_regex{ $reg };
62  
63 690 ▶ if (!defined $regex) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 29 661 690
64 # Compile & stash
65 29 $regex = qr/^$reg$/;
66 29 $field_regex{ $reg } = $regex;
67 }
68  
69 690 return $regex;
70 13 };
71  
72  
73  
74 638 ▶sub new {
Subroutine new: called CC=12 SCAR=24.9
Pod documented
75 638 my $class = shift; # if $_[0] and UNIVERSAL::isa( $_[0], 'PICA::Record'
76 # shift if defined $_[0] and $_[0] eq $class; # called as function
77  
78 638 ▶ $class = $class || ref($class); # Handle cloning
Condition33%1/3
$class || ref $class
l!l&&r!l&&!r
63800
MC/DC0%0/2
$class || ref $class
$class ref $class
Truth table33%1/3
A || B
A= $classB= ref $class
ABresult
1X1
011
000
79 638 my $self = bless {
80 _fields => [],
81 _ppn => undef
82 }, $class;
83  
84 638 ▶ return $self unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 18 620 638
85  
86 620 my $first = $_[0];
87  
88 620 ▶ if (defined $first) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first) { } 619 1 620
89  
90 619 ▶ if ($#_ == 0 and ref(\$first) eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if ($#_ == 0 and ref \$first eq 'SCALAR') { } 11 608 619
elsif (ref $first eq 'GLOB' or eval { do { $first->isa('IO::Handle') } }) { } 8 600 608
Condition100%3/3
$#_ == 0 and ref \$first eq "SCALAR"
!ll&&!rl&&r
5347411
MC/DC100%2/2
$#_ == 0 and ref \$first eq "SCALAR"
$#_ == 0 ref \$first eq "SCALAR"
Truth table100%3/3
A and B
A= $#_ == 0B= ref \$first eq "SCALAR"
ABresult
0X0
100
111
91 11 my @lines = split("\n", $first);
92 11 my @l2 = split("\x1E", $first);
93 11 ▶ if (@l2 > @lines) { # normalized
Branch: 1 branch
BranchTrueFalseTotal
if (@l2 > @lines) 2 9 11
94 2 @lines = @l2;
95 }
96  
97 11 foreach my $line (@lines) {
98 68 $line =~ s/^\x1D//; # start of record
99 68 ▶ next if $line =~ /^\s*$/; # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 6 62 68
100  
101 62 my $field = PICA::Field->parse($line);
102 61 ▶ $append_field->( $self, $field ) if $field;
Branch: 1 branch
BranchTrueFalseTotal
if $field 61 0 61
103 }
104 606 ▶ } elsif (ref($first) eq 'GLOB' or eval { $first->isa('IO::Handle') }) {
Condition100%3/3
ref $first eq "GLOB" or eval { do { $first->isa("IO::Handle") } }
l!l&&r!l&&!r
26600
MC/DC100%2/2
ref $first eq "GLOB" or eval { do { $first->isa("IO::Handle") } }
ref $first eq "GLOB" eval { do { $first->isa("IO::Handle") } }
Truth table100%3/3
A or B
A= ref $first eq "GLOB"B= eval { do { $first->isa("IO::Handle") } }
ABresult
1X1
011
000
105 3110 ▶ PICA::Parser->parsefile( $first, Limit => 1, Field => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 3110 $append_field->( $self, shift );
107 3110 return;
108 8 });
109 } else {
110 600 $self->append( @_ );
111 }
112 } else {
113 1 croak('Undefined parameter in PICA::Record->new');
114 }
115  
116 618 return $self;
117 } # new()
118  
119  
120 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
121 0 my $self = shift;
122 0 return PICA::Record->new( $self );
123 }
124  
125  
126 88 ▶sub field {
Subroutine field: called CC=14 SCAR=26.4
Pod documented
127 88 my $self = shift;
128 88 ▶ my $limit = looks_like_number($_[0]) ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[0]) ? : 4 84 88
129 88 my @specs = @_;
130  
131 88 ▶ my $test = ref($specs[-1]) eq 'CODE' ? pop @specs : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $specs[-1] eq 'CODE' ? : 4 84 88
132 88 ▶ @specs = (".*") if $test and not @specs;
Branch: 1 branch
BranchTrueFalseTotal
if $test and not @specs 1 87 88
Condition100%3/3
$test and not @specs
!ll&&!rl&&r
8431
MC/DC100%2/2
$test and not @specs
$test not @specs
Truth table100%3/3
A and not(B)
A= $testB= not @specs
ABresult
0X0
100
111
133  
134 88 ▶ return unless @specs;
Branch: 1 branch
BranchTrueFalseTotal
unless @specs 0 88 88
135 88 my @list = ();
136  
137 88 for my $tag ( @specs ) {
138 90 my $regex = $get_regex->($tag);
139  
140 90 for my $maybe ( $self->fields ) {
141 431 ▶ if ( $maybe->tag() =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe->tag =~ /$regex/) 44 387 431
142 44 local $_ = $maybe;
143 44 ▶ if ( not $test or $test->($maybe) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $test or &$test($maybe)) 39 5 44
Condition66%2/3
not $test or &$test($maybe)
l!l&&r!l&&!r
28016
MC/DC50%1/2
not $test or &$test($maybe)
not $test &$test($maybe)
Truth table66%2/3
not(A) or B
A= not $testB= &$test($maybe)
ABresult
1X1
011
000
144 39 ▶ return $maybe unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 4 35 39
145 35 push( @list, $maybe );
146 35 ▶ if ($limit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > 0) 5 30 35
147 5 ▶ return @list unless --$limit;
Branch: 1 branch
BranchTrueFalseTotal
unless --$limit 2 3 5
148 }
149 }
150 }
151 }
152 }
153  
154 82 return @list;
155 } # field()
156  
157 # Shortcut
158 13 *f = \&field;
159  
160  
161 581 ▶sub subfield {
Subroutine subfield: called CC=14 SCAR=26.4
Pod documented
162 581 my $self = shift;
163 581 ▶ my $limit = looks_like_number($_[0]) ? shift : 0;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[0]) ? : 8 573 581
164 581 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 581 581
165  
166 581 my @list = ();
167  
168 581 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 588 17 605
169 588 my $tag = shift;
170 588 my $subfield;
171
172 588 ▶ croak "Not a field or full pattern: $tag"
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^([^\$_]{3,})([\$_]([^\$_]+))?/ 0 588 588
173 unless $tag =~ /^([^\$_]{3,})([\$_]([^\$_]+))?/;
174 588 ▶ if (defined $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 574 14 588
175 574 ($tag, $subfield) = ($1, $3);
176 } else {
177 14 $subfield = shift;
178 }
179  
180 588 ▶ croak("Missing subfield for $tag")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subfield 0 588 588
181 unless defined $subfield;
182  
183 588 my $tag_regex = $get_regex->($tag);
184 588 for my $f ( $self->fields ) {
185 824 ▶ if ( $f->tag() =~ $tag_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->tag =~ /$tag_regex/) 596 228 824
186 596 my @s = $f->subfield($subfield);
187 596 ▶ if (@s) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s) 594 2 596
188 594 ▶ return shift @s unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 559 35 594
189 35 ▶ if ($limit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > 0) 10 25 35
190 10 ▶ if (scalar @s >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @s >= $limit) 5 5 10
191 5 push @list, @s[0..($limit-1)];
192 5 return @list;
193 }
194 5 $limit -= scalar @s;
195 }
196 30 push( @list, @s );
197 }
198 }
199 }
200 }
201  
202 17 ▶ return $list[0] unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 15 17
203 15 return @list;
204 } # subfield()
205  
206 # Shortcut
207 13 *sf = \&subfield;
208  
209  
210 8 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
Pod documented
211 8 my $self = shift;
212 8 my @values = $self->subfield( @_ );
213 8 return @values;
214 }
215  
216  
217 741 ▶sub fields() {
Subroutine fields: called CC=2 SCAR=7.0
Pod documented
218 741 my $self = shift;
219 741 ▶ croak("You called all_fields() but you probably want field()") if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 741 741
220 741 return @{$self->{_fields}};
221 }
222  
223  
224 4 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
225 4 my $self = shift;
226 4 return 1 * @{$self->{_fields}};
227 }
228  
229  
230 1 ▶sub occurrence {
Subroutine occurrence: called CC=2 SCAR=7.1
Pod documented
231 1 my $self = shift;
232 1 ▶ return unless $self->{_fields}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_fields"}[0] 0 1 1
233 1 return $self->{_fields}->[0]->occurrence;
234 }
235  
236 1 ▶sub occ {
Subroutine occ: called CC=1 SCAR=0.0
Pod documented
237 1 return shift->occurrence;
238 }
239  
240  
241 2 ▶sub main {
Subroutine main: called CC=1 SCAR=0.0
Pod documented
242 2 my $self = shift;
243 2 my @fields = $self->field("0...(/..)?");
244  
245 2 return PICA::Record->new(@fields);
246 }
247  
248  
249 7 ▶sub holdings {
Subroutine holdings: called CC=15 SCAR=28.2
Pod documented
250 7 my ($self, $iln) = @_;
251  
252 7 my %holdings = ();
253 7 my @fields = ();
254 7 my $prevtag;
255 7 my $curiln = '';
256 7 my @pending;
257
258 7 foreach my $f (@{$self->{_fields}}) {
259 9187 ▶ next if $f->tag =~ /^0/;
Branch: 1 branch
BranchTrueFalseTotal
if $f->tag =~ /^0/ 152 9035 9187
260  
261 9035 ▶ if ($f->tag eq '101@' and defined $f->sf('a')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f->tag eq '101@' and defined $f->sf('a')) { } 175 8860 9035
elsif ($curiln eq '') { } 0 8860 8860
Condition66%2/3
$f->tag eq "101\@" and defined $f->sf("a")
!ll&&!rl&&r
88600175
MC/DC50%1/2
$f->tag eq "101\@" and defined $f->sf("a")
$f->tag eq "101\@" defined $f->sf("a")
Truth table66%2/3
A and B
A= $f->tag eq "101\@"B= defined $f->sf("a")
ABresult
0X0
100
111
262 175 $curiln = $f->sf('a');
263 175 ▶ $holdings{$curiln} //= [ ];
Condition50%1/2
$holdings{$curiln} //= []
l!l
0175
MC/DC0%0/1
$holdings{$curiln} //= []
$holdings{$curiln}
Truth table50%1/2
A
A= $holdings{$curiln}
Aresult
11
00
264 175 ▶ if (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 0 175 175
265 0 push @{ $holdings{$curiln} }, @pending;
266 0 @pending = ();
267 }
268 175 push @{ $holdings{$curiln} }, $f;
269 } elsif( $curiln eq '' ) {
270 0 push @pending, $f;
271 } else {
272 8860 push @{ $holdings{$curiln} }, $f;
273 }
274  
275 9035 push @fields, $f;
276 9035 $prevtag = $f->tag;
277 }
278  
279 0 ▶ push @{ $holdings{$curiln} }, @pending if @pending;
Branch: 1 branch
BranchTrueFalseTotal
if @pending 0 7 7
280  
281 7 ▶ if ($iln) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iln) 2 5 7
282 2 %holdings = ($iln => $holdings{$iln});
283 2 ▶ %holdings = () unless $holdings{$iln};
Branch: 1 branch
BranchTrueFalseTotal
unless $holdings{$iln} 0 2 2
284 }
285  
286 7 foreach my $iln (keys %holdings) {
287 65 @{$holdings{$iln}} = sort {
288 3578 my ($ta,$tb) = ($a->tag, $b->tag);
289 3578 $ta =~ s{^(2...)/(..)$}{2$2$1};
290 3578 $tb =~ s{^(2...)/(..)$}{2$2$1};
291 3578 $ta cmp $tb;
292 65 } @{$holdings{$iln}};
293 }
294  
295 272 ▶ my @h = sort { ($a->iln // '0') <=> ($b->iln // '0') }
Condition50%2/4
$a->iln // 0
l!l
2720
$b->iln // 0
l!l
2720
MC/DC0%0/1
$a->iln // 0
$a->iln
MC/DC0%0/1
$b->iln // 0
$b->iln
Truth table50%1/2
A
A= $a->iln
Aresult
11
00
Truth table50%1/2
A
A= $b->iln
Aresult
11
00
296 7 map { PICA::Record->new( @$_ ) } CORE::values(%holdings);
297  
298 7 ▶ return $iln ? $h[0] : @h;
Branch: 1 branch
BranchTrueFalseTotal
$iln ? : 2 5 7
299 }
300  
301  
302 5 ▶sub items {
Subroutine items: called CC=10 SCAR=23.0
Pod documented
303 5 my $self = shift;
304  
305 5 my @copies = ();
306 5 my @fields = ();
307 5 my $prevocc;
308  
309 5 foreach my $f (@{$self->{_fields}}) {
310 3332 ▶ next unless $f->tag =~ /^[^0]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->tag =~ /^[^0]/ 63 3269 3332
311  
312 3269 ▶ if ($f->tag =~ /^1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->tag =~ /^1/) { } 181 3088 3269
313 181 $prevocc = undef;
314 181 ▶ push @copies, PICA::Record->new(@fields) if (@fields);
Branch: 1 branch
BranchTrueFalseTotal
if @fields 57 124 181
315 181 @fields = ();
316 } else {
317 3088 ▶ next unless $f->tag =~ /^2...\/(..)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->tag =~ m[^2.../(..)] 0 3088 3088
318  
319 3088 ▶ if (!($prevocc && $prevocc eq $1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prevocc and $prevocc eq $1) 385 2703 3088
Condition100%3/3
$prevocc and $prevocc eq $1
!ll&&!rl&&r
623232703
MC/DC100%2/2
$prevocc and $prevocc eq $1
$prevocc $prevocc eq $1
Truth table100%3/3
A and B
A= $prevoccB= $prevocc eq $1
ABresult
0X0
100
111
320 385 $prevocc = $1;
321 385 ▶ push @copies, PICA::Record->new(@fields) if (@fields);
Branch: 1 branch
BranchTrueFalseTotal
if @fields 323 62 385
322 385 @fields = ();
323 }
324  
325 3088 push @fields, $f;
326 }
327 }
328 5 ▶ push @copies, PICA::Record->new(@fields) if (@fields);
Branch: 1 branch
BranchTrueFalseTotal
if @fields 5 0 5
329 5 return @copies;
330 }
331  
332  
333 149 ▶sub empty {
Subroutine empty: called CC=3 SCAR=11.1
Pod documented
334 149 my $self = shift;
335 149 foreach my $field (@{$self->{_fields}}) {
336 128 ▶ return 0 if !$field->empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->empty 128 0 128
337 }
338 21 return 1;
339 }
340  
341  
342 5 ▶sub ppn {
Subroutine ppn: called CC=3 SCAR=11.2
Pod documented
343 5 my $self = shift;
344 5 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 4 5
345 1 my $ppn = shift;
346 1 ▶ if (defined $ppn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ppn) { } 1 0 1
347 1 $append_field->( $self, PICA::Field->new('003@', '0' => $ppn) )
348 } else {
349 0 $self->remove('003@');
350 }
351 }
352 5 return $self->{_ppn};
353 }
354  
355  
356 4 ▶sub epn {
Subroutine epn: called CC=1 SCAR=0.0
Pod documented
357 4 my $self = shift;
358 #for(my $i=0; $i<@_; $i++) {
359 # # TODO: add EPNs
360 #}
361 4 return $self->subfield('203@/..$0');
362 }
363  
364  
365 545 ▶sub iln {
Subroutine iln: called CC=1 SCAR=0.0
Pod documented
366 # TODO: set ILN with this method and check uniqueness
367 545 my $self = shift;
368 545 return $self->subfield('101@$a');
369 }
370  
371  
372 608 ▶sub append {
Subroutine append: called CC=12 SCAR=24.9
Pod documented
373 608 my $self = shift;
374 # TODO: this method can be simplified by use of ->new (see appendif)
375  
376 608 my $c = 0;
377  
378 608 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 622 608 1230
379 # Append a field (whithout creating a copy)
380 622 ▶ while (@_ and UNIVERSAL::isa($_[0],'PICA::Field') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and UNIVERSAL::isa($_[0], "PICA::Field")) 8221 622 8843
Condition100%3/3
@_ and UNIVERSAL::isa($_[0], "PICA::Field")
!ll&&!rl&&r
581418221
MC/DC100%2/2
@_ and UNIVERSAL::isa($_[0], "PICA::Field")
@_ UNIVERSAL::isa($_[0], "PICA::Field")
Truth table100%3/3
A and B
A= @_B= UNIVERSAL::isa($_[0], "PICA::Field")
ABresult
0X0
100
111
381 8221 $c += $append_field->( $self, shift );
382 }
383 # Append a whole record (copy all its fields)
384 622 ▶ while (@_ and UNIVERSAL::isa($_[0],'PICA::Record')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and UNIVERSAL::isa($_[0], "PICA::Record")) 5 622 627
Condition100%3/3
@_ and UNIVERSAL::isa($_[0], "PICA::Record")
!ll&&!rl&&r
586365
MC/DC100%2/2
@_ and UNIVERSAL::isa($_[0], "PICA::Record")
@_ UNIVERSAL::isa($_[0], "PICA::Record")
Truth table100%3/3
A and B
A= @_B= UNIVERSAL::isa($_[0], "PICA::Record")
ABresult
0X0
100
111
385 5 my $record = shift;
386 5 for my $field ( $record->fields ) {
387 22 $c += $append_field->( $self, $field->copy );
388 }
389 }
390 622 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 36 586 622
391 36 my @params = (shift); # tag
392 36 ▶ while (@_ and defined $_[0] and length($_[0]) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and defined $_[0] and length $_[0] == 1) 41 36 77
Condition83%5/6
@_ and defined $_[0] and length $_[0] == 1
!ll&&!rl&&r
221441
@_ and defined $_[0]
!ll&&!rl&&r
22055
MC/DC66%2/3
@_ and defined $_[0] and length $_[0] == 1
@_ defined $_[0] length $_[0] == 1
Truth table75%3/4
A and B and C
A= @_B= defined $_[0]C= length $_[0] == 1
ABCresult
0XX0
10X0
1100
1111
393 41 push @params, shift; # subfield
394 41 push @params, shift; # value
395 }
396 36 ▶ $c += $append_field->( $self, PICA::Field->new( @params ) ) if @params > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @params > 1 36 0 36
397 }
398 }
399  
400 #use Data::Dumper;
401 #print Dumper(\@_)."\n";
402 # local $Carp::CarpLevel = 1;
403  
404  
405 608 return $c;
406 }
407  
408  
409 3 ▶sub appendif {
Subroutine appendif: called CC=3 SCAR=11.0
Pod documented
410 3 my $self = shift;
411 3 my $append = PICA::Record->new( @_ );
412 3 for my $field ( $append->fields ) {
413 2 $field = $field->purged();
414 2 ▶ $append_field->( $self, $field ) if $field;
Branch: 1 branch
BranchTrueFalseTotal
if $field 2 0 2
415 }
416 3 $self;
417 }
418  
419  
420 9 ▶sub update {
Subroutine update: called CC=13 SCAR=25.7
Pod documented
421 9 my $self = shift;
422 9 my $tag = shift;
423  
424 9 ▶ croak("Not a valid tag: $tag")
Branch: 1 branch
BranchTrueFalseTotal
unless PICA::Field::parse_pp_tag($tag) 0 9 9
425 unless PICA::Field::parse_pp_tag( $tag );
426  
427 9 my $replace;
428  
429 9 ▶ return unless @_; # ignore
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 8 9
430  
431 8 ▶ if ( not defined $_[0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0]) { } 1 7 8
elsif (UNIVERSAL::isa($_[0], 'PICA::Field') or ref $_[0] eq 'CODE') { } 4 3 7
432 1 $replace = shift;
433 ▶ } elsif ( UNIVERSAL::isa( $_[0], 'PICA::Field' ) or ref($_[0]) eq 'CODE' ) {
Condition100%3/3
UNIVERSAL::isa($_[0], "PICA::Field") or ref $_[0] eq "CODE"
l!l&&r!l&&!r
313
MC/DC100%2/2
UNIVERSAL::isa($_[0], "PICA::Field") or ref $_[0] eq "CODE"
UNIVERSAL::isa($_[0], "PICA::Field") ref $_[0] eq "CODE"
Truth table100%3/3
A or B
A= UNIVERSAL::isa($_[0], "PICA::Field")B= ref $_[0] eq "CODE"
ABresult
1X1
011
000
434 4 $replace = shift;
435 } else {
436 3 $replace = PICA::Field->new($tag, @_);
437 }
438  
439 8 my $regex = $get_regex->($tag);
440  
441 8 for my $field ( $self->fields ) {
442 36 ▶ if ( $field->tag() =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->tag =~ /$regex/) 8 28 36
443 8 my $rep = $replace;
444 8 ▶ if ( UNIVERSAL::isa( $replace, 'CODE' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($replace, "CODE")) 2 6 8
445 2 $rep = $rep->( $field );
446 2 ▶ $rep = undef unless UNIVERSAL::isa( $rep, 'PICA::Field' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($rep, "PICA::Field") 1 1 2
447 }
448 8 ▶ if (defined $rep) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rep) 7 1 8
449 7 ▶ $self->{_ppn} = $rep->sf('0') if $rep->tag eq '003@';
Branch: 1 branch
BranchTrueFalseTotal
if $rep->tag eq "003\@" 3 4 7
450 7 $field->replace( $rep );
451 }
452 8 ▶ return unless ref($replace) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $replace eq "CODE" 6 2 8
453 }
454 }
455 }
456  
457  
458 5 ▶sub remove {
Subroutine remove: called CC=6 SCAR=17.9
Pod documented
459 5 my $self = shift;
460 5 my @specs = @_;
461  
462 5 ▶ return 0 if !@specs;
Branch: 1 branch
BranchTrueFalseTotal
unless @specs 0 5 5
463 5 my $c = 0;
464  
465 5 for my $tag ( @specs ) {
466 6 my $regex = $get_regex->($tag);
467  
468 6 my $i=0;
469 6 for my $maybe ( $self->fields ) {
470 47 ▶ if ( $maybe->tag() =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe->tag =~ /$regex/) { } 32 15 47
471 32 ▶ $self->{_ppn} = undef if $maybe->tag() eq '003@';
Branch: 1 branch
BranchTrueFalseTotal
if $maybe->tag eq "003\@" 1 31 32
472 32 splice( @{$self->{_fields}}, $i, 1);
473 32 $c++;
474 } else {
475 15 $i++;
476 }
477 }
478 } # for $tag
479  
480 5 return $c;
481 }
482  
483  
484 2 ▶sub sort {
Subroutine sort: called CC=2 SCAR=6.9
Pod documented
485 2 my $self = shift;
486  
487 2 my $main = $self->main;
488 2 my @holdings = $self->holdings;
489  
490 2 @{$self->{_fields}} = sort { $a->tag cmp $b->tag } @{$main->{_fields}};
491  
492 2 foreach my $h ( @holdings ) {
493 4 push @{$self->{_fields}}, @{$h->{_fields}};
494 }
495  
496 2 $self;
497 }
498  
499  
500 0 ▶sub add_headers {
Subroutine add_headers: not called CC=4 SCAR=30.0
Pod documented
501 0 my ($self, %params) = @_;
502  
503 0 my $eln = $params{eln};
504 0 ▶ croak("add_headers needs an ELN") unless defined $eln;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eln 0 0 0
505  
506 0 my $status = $params{status};
507 0 ▶ croak("add_headers needs status") unless defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 0 0
508  
509 0 ▶ my @timestamp = defined $params{timestamp} ? @{$params{timestamp}} : localtime;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'timestamp'} ? : 0 0 0
510 # TODO: Test timestamp
511  
512 0 my $hdate = strftime ("$eln:%d-%m-%g", @timestamp);
513 0 my $htime = strftime ("%H:%M:%S", @timestamp);
514  
515 # Pica3: 000K - Unicode-Kennzeichen
516 0 $self->append( "001U", '0' => 'utf8' );
517  
518 # PICA3: 0200 - Kennung und Datum der Ersterfassung
519 # http://www.gbv.de/vgm/info/mitglieder/02Verbund/01Erschliessung/02Richtlinien/01KatRicht/0200.pdf
520 0 $self->append( "001A", '0' => $hdate );
521  
522 # PICA3: 0200 - Kennung und Datum der letzten Aenderung
523 # http://www.gbv.de/vgm/info/mitglieder/02Verbund/01Erschliessung/02Richtlinien/01KatRicht/0210.pdf
524 0 $self->append( "001B", '0' => $hdate, 't' => $htime );
525  
526 # PICA3: 0230 - Kennung und Datum der Statusaenderung
527 # http://www.gbv.de/vgm/info/mitglieder/02Verbund/01Erschliessung/02Richtlinien/01KatRicht/0230.pdf
528 0 $self->append( "001D", '0' => $hdate );
529  
530 # PCIA3: 0500 - Bibliographische Gattung und Status
531 # http://www.gbv.de/vgm/info/mitglieder/02Verbund/01Erschliessung/02Richtlinien/01KatRicht/0500.pdf
532 0 $self->append( "002@", '0' => $status );
533 }
534  
535  
536 59 ▶sub string {
Subroutine string: called CC=3 SCAR=11.0
Pod documented
537 59 my ($self, %args) = @_;
538  
539 59 ▶ $args{endfield} = "\n" unless defined($args{endfield});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"endfield"} 59 0 59
540  
541 59 my @lines = ();
542 59 for my $field ( @{$self->{_fields}} ) {
543 796 push( @lines, $field->string(%args) );
544 }
545 59 return join('', @lines);
546 }
547  
548  
549 6 ▶sub normalized() {
Subroutine normalized: called CC=3 SCAR=11.0
Pod documented
550 6 my $self = shift;
551 6 my $prefix = shift;
552 6 ▶ $prefix = "" if (!$prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless $prefix 6 0 6
553  
554 6 my @lines = ();
555 6 for my $field ( @{$self->{_fields}} ) {
556 6 push( @lines, $field->normalized() );
557 }
558  
559 6 return "\x1D\x0A" . $prefix . join( "", @lines );
560 }
561  
562  
563 3 ▶sub xml {
Subroutine xml: called CC=6 SCAR=22.0
Pod documented
564 3 my $self = shift;
565 3 my $writer = $_[0];
566 3 my ($string, $sref);
567  
568 # write to a string
569 3 ▶ if (not UNIVERSAL::isa( $writer, 'XML::Writer' )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($writer, "XML::Writer")) 0 3 3
570 0 my %params = @_;
571 0 ▶ if (not defined $params{OUTPUT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"OUTPUT"}) 0 0 0
572 0 $sref = \$string;
573 0 $params{OUTPUT} = $sref;
574 }
575 0 $writer = PICA::Writer::xmlwriter( %params );
576 }
577  
578 3 ▶ if ( UNIVERSAL::isa( $writer, 'XML::Writer::Namespaces' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($writer, 'XML::Writer::Namespaces')) { } 3 0 3
579 3 $writer->startTag( [$PICA::Record::XMLNAMESPACE, 'record'] );
580 } else {
581 0 $writer->startTag( 'record' );
582 }
583 3 for my $field ( @{$self->{_fields}} ) {
584 12 $field->xml( $writer );
585 }
586 3 $writer->endTag();
587  
588 3 ▶ return defined $sref ? $$sref : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $sref ? : 0 3 3
589 }
590  
591  
592 0 ▶sub html {
Subroutine html: not called CC=2 SCAR=17.9
Pod documented
593 0 my $self = shift;
594 0 my %options = @_;
595  
596 0 my @html = ("<div class='record'>\n");
597 0 for my $field ( @{$self->{_fields}} ) {
598 0 push @html, $field->html( %options );
599 }
600 0 push @html, "</div>";
601  
602 0 return join("", @html) . "\n";
603 }
604  
605  
606 5 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
Pod documented
607 5 my $record = shift;
608 5 my $writer = PICA::Writer->new( @_ );
609 5 ▶ return $writer unless $writer;
Branch: 1 branch
BranchTrueFalseTotal
unless $writer 1 4 5
610 4 $writer->write( $record )->end;
611 }
612  
613  
614  
615 2 ▶sub pgrep (&@) {
Subroutine pgrep: called CC=5 SCAR=16.1
Pod documented
616 2 my $block = shift;
617 2 ▶ my $record = (@_ == 1 and UNIVERSAL::isa( $_[0],'PICA::Record' ))
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && UNIVERSAL::isa($_[0], 'PICA::Record') ? : 1 1 2
Condition66%2/3
@_ == 1 and UNIVERSAL::isa($_[0], "PICA::Record")
!ll&&!rl&&r
101
MC/DC50%1/2
@_ == 1 and UNIVERSAL::isa($_[0], "PICA::Record")
@_ == 1 UNIVERSAL::isa($_[0], "PICA::Record")
Truth table66%2/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "PICA::Record")
ABresult
0X0
100
111
618 ? $_[0] : PICA::Record->new( @_ );
619 2 my @fields;
620  
621 2 for my $f ( $record->fields ) {
622 6 local $_ = $f;
623 6 ▶ push @fields, $f if $block->();
Branch: 1 branch
BranchTrueFalseTotal
if &$block() 2 4 6
624 }
625  
626 2 return PICA::Record->new( @fields );
627 }
628  
629  
630 1 ▶sub pmap (&@) {
Subroutine pmap: called CC=6 SCAR=20.2
Pod documented
631 1 my $block = shift;
632 1 ▶ my $record = (@_ == 1 and UNIVERSAL::isa( $_[0],'PICA::Record' ))
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && UNIVERSAL::isa($_[0], 'PICA::Record') ? : 1 0 1
Condition33%1/3
@_ == 1 and UNIVERSAL::isa($_[0], "PICA::Record")
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 1 and UNIVERSAL::isa($_[0], "PICA::Record")
@_ == 1 UNIVERSAL::isa($_[0], "PICA::Record")
Truth table33%1/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "PICA::Record")
ABresult
0X0
100
111
633 ? $_[0] : PICA::Record->new( @_ );
634 1 my @fields;
635  
636 1 for my $f ( $record->fields ) {
637 3 local $_ = $f;
638 3 my @r = $block->();
639 3 ▶ if (@r == 1 and UNIVERSAL::isa( $_[0],'PICA::Field' )) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r == 1 and UNIVERSAL::isa($_[0], 'PICA::Field')) { } 0 3 3
Condition33%1/3
@r == 1 and UNIVERSAL::isa($_[0], "PICA::Field")
!ll&&!rl&&r
030
MC/DC0%0/2
@r == 1 and UNIVERSAL::isa($_[0], "PICA::Field")
@r == 1 UNIVERSAL::isa($_[0], "PICA::Field")
Truth table33%1/3
A and B
A= @r == 1B= UNIVERSAL::isa($_[0], "PICA::Field")
ABresult
0X0
100
111
640 0 push @fields, $r[0];
641 } else {
642 3 push @fields, PICA::Field->new( @r );
643 }
644 }
645  
646 1 return PICA::Record->new( @fields );
647 }
648  
649  
650 10 ▶sub readpicarecord {
Subroutine readpicarecord: called CC=5 SCAR=16.4
Pod documented
651 10 my ($file, %options) = @_;
652 10 ▶ if ( wantarray and defined $options{Limit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and defined $options{"Limit"}) 1 9 10
Condition100%3/3
wantarray and defined $options{"Limit"}
!ll&&!rl&&r
631
MC/DC100%2/2
wantarray and defined $options{"Limit"}
wantarray defined $options{"Limit"}
Truth table100%3/3
A and B
A= wantarrayB= defined $options{"Limit"}
ABresult
0X0
100
111
653 1 return PICA::Parser->parsefile( $file, %options )->records();
654 }
655 9 $options{Limit} = 1;
656 9 my ($record) = PICA::Parser->parsefile( $file, %options )->records();
657 9 ▶ return undef unless $record and not $record->empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $record and not $record->empty 0 9 9
Condition33%1/3
$record and not $record->empty
!ll&&!rl&&r
009
MC/DC0%0/2
$record and not $record->empty
$record not $record->empty
Truth table33%1/3
A and not(B)
A= $recordB= not $record->empty
ABresult
0X0
100
111
658 9 return $record;
659 }
660  
661  
662 13 *writepicarecord = *write;
663  
664  
665 1 ▶sub picarecord {
Subroutine picarecord: called CC=1 SCAR=0.0
Pod documented
666 1 return PICA::Record->new( @_ );
667 }
668  
669 13 1;
670  
671 __END__
« PlainParser.pm ↑ Summary Store.pm »