Field.pm

filter: Statementstmt 98.7% 236 / 239 Branchbran 95.0% 95 / 100 Conditioncond 93.7% 45 / 48 MC/DCmcdc 70.3% 19 / 27 Subroutinesub 97.0% 33 / 34 Podpod 100% 24 / 24 totaltotal 95.7% 452 / 472 CC 72 SCAR 18.7
CC
72
Cov
97%
delete_subfield
18.0
new
9.0
update
7.0
CRAP 6.5
« File.pm ↑ Summary Batch.pm »
1 package MARC::Field;
2  
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use constant SUBFIELD_INDICATOR => "\x1F";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use constant END_OF_FIELD => "\x1E";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use vars qw( $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 MARC::Field - Perl extension for handling MARC fields
16  
17 =head1 SYNOPSIS
18  
19 use MARC::Field;
20  
21 # If your system uses wacky control field tags, add them
22 MARC::Field->allow_controlfield_tags('FMT', 'LLE');
23  
24 my $field = MARC::Field->new( 245, '1', '0',
25 'a' => 'Raccoons and ripe corn / ',
26 'c' => 'Jim Arnosky.'
27 );
28 $field->add_subfields( "a", "1st ed." );
29  
30 =head1 DESCRIPTION
31  
32 Defines MARC fields for use in the MARC::Record module. I suppose
33 you could use them on their own, but that wouldn't be very interesting.
34  
35 =head1 EXPORT
36  
37 None by default. Any errors are stored in C<$MARC::Field::ERROR>, which
38 C<$MARC::Record> usually bubbles up to C<$MARC::Record::ERROR>.
39  
40 =head1 CLASS VARIABLES
41  
42 B<extra_controlfield_tags>: Some systems (notably Ex Libris's Aleph) throw
43 extra control fields in their MARC (e.g., Aleph's MARC-XML tends to have a
44 C<FMT> control field). We keep a class-level hash to track to track them; it can
45 be manipulated with C<allow_controlfield_tags> and c<disallow_controlfield_tags>.
46  
47 =cut
48  
49 40 my %extra_controlfield_tags = ();
50  
51  
52 =head1 METHODS
53  
54 =head2 new()
55  
56 The constructor, which will return a MARC::Field object. Typically you will
57 pass in the tag number, indicator 1, indicator 2, and then a list of any
58 subfield/data pairs. For example:
59  
60 my $field = MARC::Field->new(
61 245, '1', '0',
62 'a' => 'Raccoons and ripe corn / ',
63 'c' => 'Jim Arnosky.'
64 );
65  
66 Or if you want to add a control field (< 010) that does not have indicators.
67  
68 my $field = MARC::Field->new( '001', ' 14919759' );
69  
70 =cut
71  
72 4756 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
73 4756 my $class = shift;
74 4756 $class = $class;
75  
76 ## MARC spec indicates that tags can have alphabetical
77 ## characters in them! If they do appear we assume that
78 ## they have indicators like tags > 010 unless they've
79 ## been previously defined as control tags using
80 ## add_controlfield
81
82 4756 my $tagno = shift;
83 4756 ▶ $class->is_valid_tag($tagno)
Branch: 1 branch
BranchTrueFalseTotal
$class->is_valid_tag($tagno) or croak(qq[Tag "$tagno" is not a valid tag.]) 4754 2 4756
84 or croak( "Tag \"$tagno\" is not a valid tag." );
85 4754 my $is_control = $class->is_controlfield_tag($tagno);
86  
87 4754 my $self = bless {
88 _tag => $tagno,
89 _warnings => [],
90 _is_control_field => $is_control,
91 }, $class;
92  
93 4754 ▶ if ( $is_control ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_control) { } 873 3881 4754
94 873 $self->{_data} = shift;
95 873 ▶ $self->_warn("Too much data for control field '$tagno'") if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 870 873
96 } else {
97 3881 for my $indcode ( qw( _ind1 _ind2 ) ) {
98 7762 my $indicator = shift;
99 7762 ▶ defined($indicator) or croak("Field $tagno must have indicators (use ' ' for empty indicators)");
Branch: 1 branch
BranchTrueFalseTotal
defined $indicator or croak("Field $tagno must have indicators (use ' ' for empty indicators)") 7760 2 7762
100 7760 ▶ unless ($self->is_valid_indicator($indicator)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_valid_indicator($indicator)) 52 7708 7760
101 52 ▶ $self->_warn( "Invalid indicator \"$indicator\" forced to blank" ) unless ($indicator eq "");
Branch: 1 branch
BranchTrueFalseTotal
unless $indicator eq "" 6 46 52
102 52 $indicator = " ";
103 }
104 7760 $self->{$indcode} = $indicator;
105 } # for
106  
107 3879 ▶ (@_ >= 2)
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or croak("Field $tagno must have at least one subfield") 3878 1 3879
108 or croak( "Field $tagno must have at least one subfield" );
109  
110 # Normally, we go thru add_subfields(), but internally we can cheat
111 3878 $self->{_subfields} = [@_];
112 }
113  
114 4751 return $self;
115 } # new()
116  
117  
118 =head2 tag()
119  
120 Returns the three digit tag for the field.
121  
122 =cut
123  
124 1414 ▶sub tag {
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
125 1414 my $self = shift;
126 1414 return $self->{_tag};
127 }
128  
129 =head2 set_tag(tag)
130  
131 Changes the tag number of this field. Updates the control status accordingly.
132 Will C<croak> if an invalid value is passed in.
133  
134 =cut
135  
136 1 ▶sub set_tag {
Subroutine set_tag: called CC=2 SCAR=7.0
Pod documented
137 1 my ( $self, $tagno ) = @_;
138  
139 1 ▶ $self->is_valid_tag($tagno)
Branch: 1 branch
BranchTrueFalseTotal
$self->is_valid_tag($tagno) or croak(qq[Tag "$tagno" is not a valid tag.]) 1 0 1
140 or croak("Tag \"$tagno\" is not a valid tag.");
141 1 $self->{_tag} = $tagno;
142 1 $self->{_is_control_field} = $self->is_controlfield_tag($tagno);
143 }
144  
145 =head2 indicator(indno)
146  
147 Returns the specified indicator. Returns C<undef> and logs
148 a warning if field is a control field and thus doesn't have
149 indicators. If the field is not a control field, croaks
150 if the I<indno> is not 1 or 2.
151  
152 =cut
153  
154 97 ▶sub indicator {
Subroutine indicator: called CC=4 SCAR=13.9
Pod documented
155 97 my $self = shift;
156 97 my $indno = shift;
157  
158 97 ▶ if ($self->is_control_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_control_field) 2 95 97
159 2 $self->_warn( "Control fields (generally, those with tags below 010) do not have indicators" );
160 2 return;
161 }
162  
163 95 ▶ if ( $indno == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($indno == 1) { } 48 47 95
elsif ($indno == 2) { } 46 1 47
164 48 return $self->{_ind1};
165 } elsif ( $indno == 2 ) {
166 46 return $self->{_ind2};
167 } else {
168 1 croak( "Indicator number must be 1 or 2" );
169 }
170 }
171  
172 =head2 set_indicator($indno, $indval)
173  
174 Set the indicator position I<$indno> to the value
175 specified by I<$indval>. Croaks if the indicator position,
176 is invalid, the field is a control field and thus
177 doesn't have indicators, or if the new indicator value
178 is invalid.
179  
180 =cut
181  
182 5 ▶sub set_indicator {
Subroutine set_indicator: called CC=5 SCAR=16.1
Pod documented
183 5 my $self = shift;
184 5 my $indno = shift;
185 5 my $indval = shift;
186  
187 5 ▶ croak('Indicator number must be 1 or 2')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indno and $indno =~ /^[12]$/ 1 4 5
Condition66%2/3
defined $indno and $indno =~ /^[12]$/
!ll&&!rl&&r
014
MC/DC50%1/2
defined $indno and $indno =~ /^[12]$/
defined $indno $indno =~ /^[12]$/
Truth table66%2/3
A and B
A= defined $indnoB= $indno =~ /^[12]$/
ABresult
0X0
100
111
188 unless defined $indno && $indno =~ /^[12]$/;
189 4 ▶ croak('Cannot set indicator for control field')
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_control_field 1 3 4
190 if $self->is_control_field;
191 3 ▶ croak('Indicator value is invalid') unless $self->is_valid_indicator($indval);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_valid_indicator($indval) 1 2 3
192  
193 2 $self->{"_ind$indno"} = $indval;
194 }
195  
196 =head2 allow_controlfield_tags($tag, $tag2, ...)
197  
198 Add $tags to class-level list of strings to consider valid control fields tags (in addition to 001 through 009).
199 Tags must have three characters.
200  
201 =cut
202  
203 2 ▶sub allow_controlfield_tags {
Subroutine allow_controlfield_tags: called CC=2 SCAR=6.9
Pod documented
204 2 my $self = shift;
205 2 foreach my $tag (@_) {
206 2 $extra_controlfield_tags{$tag} = 1;
207 }
208 }
209  
210 =head2 disallow_controlfield_tags($tag, $tag2, ...)
211 =head2 disallow_controlfield_tags('*')
212  
213 Revoke the validity of a control field tag previously added with allow_controlfield_tags. As a special case,
214 if you pass the string '*' it will clear out all previously-added tags.
215  
216 NOTE that this will only deal with stuff added with allow_controlfield_tags; you can't disallow '001'.
217  
218 =cut
219  
220 2 ▶sub disallow_controlfield_tags {
Subroutine disallow_controlfield_tags: called CC=3 SCAR=11.0
Pod documented
221 2 my $self = shift;
222 2 ▶ if ($_[0] eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "*") 1 1 2
223 1 %extra_controlfield_tags = ();
224 1 return;
225 }
226 1 foreach my $tag (@_) {
227 1 delete $extra_controlfield_tags{$tag};
228 }
229 }
230  
231 =head2 is_valid_tag($tag) -- is the given tag valid?
232  
233 Generally called as a class method (e.g., MARC::Field->is_valid_tag('001'))
234  
235 =cut
236  
237 4757 ▶sub is_valid_tag {
Subroutine is_valid_tag: called CC=3 SCAR=11.0
Pod documented
238 4757 my $self = shift;
239 4757 my $tag = shift;
240 4757 ▶ return 1 if defined $tag && $tag =~ /^[0-9A-Za-z]{3}$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag and $tag =~ /^[0-9A-Za-z]{3}$/ 4755 2 4757
Condition66%2/3
defined $tag and $tag =~ /^[0-9A-Za-z]{3}$/
!ll&&!rl&&r
024755
MC/DC50%1/2
defined $tag and $tag =~ /^[0-9A-Za-z]{3}$/
defined $tag $tag =~ /^[0-9A-Za-z]{3}$/
Truth table66%2/3
A and B
A= defined $tagB= $tag =~ /^[0-9A-Za-z]{3}$/
ABresult
0X0
100
111
241 2 return 0;
242 }
243  
244 =head2 is_valid_indicator($indval) -- is the given indicator value valid?
245  
246 Generally called as a class method (e.g., MARC::Field->is_valid_indicator('4'))
247  
248 =cut
249  
250 7763 ▶sub is_valid_indicator {
Subroutine is_valid_indicator: called CC=3 SCAR=11.0
Pod documented
251 7763 my $self = shift;
252 7763 my $indval = shift;
253 7763 ▶ return 1 if defined $indval && $indval =~ /^[0-9A-Za-z ]$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $indval and $indval =~ /^[0-9A-Za-z ]$/ 7710 53 7763
Condition66%2/3
defined $indval and $indval =~ /^[0-9A-Za-z ]$/
!ll&&!rl&&r
0537710
MC/DC50%1/2
defined $indval and $indval =~ /^[0-9A-Za-z ]$/
defined $indval $indval =~ /^[0-9A-Za-z ]$/
Truth table66%2/3
A and B
A= defined $indvalB= $indval =~ /^[0-9A-Za-z ]$/
ABresult
0X0
100
111
254 53 return 0;
255 }
256  
257 =head2 is_controlfield_tag($tag) -- does the given tag denote a control field?
258  
259 Generally called as a class method (e.g., MARC::Field->is_controlfield_tag('001'))
260  
261 =cut
262  
263 sub is_controlfield_tag
264 7342 ▶{
Subroutine is_controlfield_tag: called CC=4 SCAR=13.9
Pod documented
265 7342 my $self = shift;
266 7342 my $tag = shift;
267 7342 ▶ return 1 if ($extra_controlfield_tags{$tag});
Branch: 1 branch
BranchTrueFalseTotal
if $extra_controlfield_tags{$tag} 2 7340 7342
268 7340 ▶ return 1 if (($tag =~ /^\d+$/) && ($tag < 10));
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /^\d+$/ and $tag < 10 1434 5906 7340
Condition100%3/3
$tag =~ /^\d+$/ and $tag < 10
!ll&&!rl&&r
758991434
MC/DC100%2/2
$tag =~ /^\d+$/ and $tag < 10
$tag =~ /^\d+$/ $tag < 10
Truth table100%3/3
A and B
A= $tag =~ /^\d+$/B= $tag < 10
ABresult
0X0
100
111
269 5906 return 0; # otherwise, it's not a control field
270 }
271  
272  
273 =head2 is_control_field()
274  
275 Tells whether this field is one of the control tags from 001-009.
276  
277 =cut
278  
279 484 ▶sub is_control_field {
Subroutine is_control_field: called CC=1 SCAR=0.0
Pod documented
280 484 my $self = shift;
281 484 return $self->{_is_control_field};
282 }
283  
284 =head2 subfield(code)
285  
286 When called in a scalar context returns the text from the first subfield
287 matching the subfield code.
288  
289 my $subfield = $field->subfield( 'a' );
290  
291 Or if you think there might be more than one you can get all of them by
292 calling in a list context:
293  
294 my @subfields = $field->subfield( 'a' );
295  
296 If no matching subfields are found, C<undef> is returned in a scalar context
297 and an empty list in a list context.
298  
299 If the tag is a control field, C<undef> is returned and
300 C<$MARC::Field::ERROR> is set.
301  
302 =cut
303  
304 30 ▶sub subfield {
Subroutine subfield: called CC=5 SCAR=16.1
Pod documented
305 30 my $self = shift;
306 30 my $code_wanted = shift;
307  
308 30 ▶ croak( "Control fields (generally, just tags below 010) do not have subfields, use data()" )
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_control_field 1 29 30
309 if $self->is_control_field;
310  
311 29 my @data = @{$self->{_subfields}};
312 29 my @found;
313 29 ▶ while ( defined( my $code = shift @data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $code = shift @data)) 64 29 93
314 64 ▶ if ( $code eq $code_wanted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code eq $code_wanted) { } 31 33 64
315 31 push( @found, shift @data );
316 } else {
317 33 shift @data;
318 }
319 }
320 29 ▶ if ( wantarray() ) { return @found; }
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 28 29
321 28 return( $found[0] );
322 }
323  
324 =head2 subfields()
325  
326 Returns all the subfields in the field. What's returned is a list of
327 list refs, where the inner list is a subfield code and the subfield data.
328  
329 For example, this might be the subfields from a 245 field:
330  
331 (
332 [ 'a', 'Perl in a nutshell :' ],
333 [ 'b', 'A desktop quick reference.' ],
334 )
335  
336 =cut
337  
338 3 ▶sub subfields {
Subroutine subfields: called CC=3 SCAR=11.0
Pod documented
339 3 my $self = shift;
340  
341 3 ▶ if ($self->is_control_field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_control_field) 1 2 3
342 1 $self->_warn( "Control fields (generally, just tags below 010) do not have subfields" );
343 1 return;
344 }
345  
346 2 my @list;
347 2 my @data = @{$self->{_subfields}};
348 2 ▶ while ( defined( my $code = shift @data ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $code = shift @data)) 4 2 6
349 4 push( @list, [$code, shift @data] );
350 }
351 2 return @list;
352 }
353  
354 =head2 data()
355  
356 Returns the data part of the field, if the tag number is less than 10.
357  
358 =cut
359  
360 12 ▶sub data {
Subroutine data: called CC=3 SCAR=11.4
Pod documented
361 12 my $self = shift;
362  
363 12 ▶ croak( "data() is only for control fields (generally, just tags below 010) , use subfield()" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_control_field 0 12 12
364 unless $self->is_control_field;
365  
366 12 ▶ $self->{_data} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 12 12
367  
368 12 return $self->{_data};
369 }
370  
371 =head2 add_subfields(code,text[,code,text ...])
372  
373 Adds subfields to the end of the subfield list.
374  
375 $field->add_subfields( 'c' => '1985' );
376  
377 Returns the number of subfields added, or C<undef> if there was an error.
378  
379 =cut
380  
381 1 ▶sub add_subfields {
Subroutine add_subfields: called CC=2 SCAR=7.0
Pod documented
382 1 my $self = shift;
383  
384 1 ▶ croak( "Subfields are only for data fields (generally, just tags >= 010)" )
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_control_field 0 1 1
385 if $self->is_control_field;
386  
387 1 push( @{$self->{_subfields}}, @_ );
388 1 return @_/2;
389 }
390  
391 =head2 delete_subfield()
392  
393 delete_subfield() allows you to remove subfields from a field:
394  
395 # delete any subfield a in the field
396 $field->delete_subfield(code => 'a');
397  
398 # delete any subfield a or u in the field
399 $field->delete_subfield(code => ['a', 'u']);
400  
401 # delete any subfield code matching a compiled regular expression
402 $field->delete_subfield(code => qr/[^a-z0-9]/);
403  
404 If you want to only delete subfields at a particular position you can
405 use the pos parameter:
406  
407 # delete subfield u at the first position
408 $field->delete_subfield(code => 'u', pos => 0);
409  
410 # delete subfield u at first or second position
411 $field->delete_subfield(code => 'u', pos => [0,1]);
412  
413 # delete the second subfield, no matter what it is
414 $field->delete_subfield(pos => 1);
415  
416 You can specify a regex to for only deleting subfields that match:
417  
418 # delete any subfield u that matches zombo.com
419 $field->delete_subfield(code => 'u', match => qr/zombo.com/);
420  
421 # delete any subfield that matches quux
422 $field->delete_subfield(match => qr/quux/);
423  
424 You can also pass a single subfield label:
425  
426 # delete all subfield u
427 $field->delete_subfield('u');
428  
429 =cut
430  
431 16 ▶sub delete_subfield {
Subroutine delete_subfield: called CC=18 SCAR=28.9
Pod documented
432 16 my ($self, @options) = @_;
433  
434 16 my %options;
435 16 ▶ if (scalar(@options) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @options == 1) { } 1 15 16
elsif (0 == scalar(@options) % 2) { } 14 1 15
436 1 $options{code} = $options[0];
437 } elsif (0 == scalar(@options) % 2) {
438 14 %options = @options;
439 } else {
440 1 croak 'delete_subfield must be called with single scalar or a hash';
441 }
442  
443 15 my $codes = _normalize_arrayref($options{code});
444 15 my $positions = _normalize_arrayref($options{'pos'});
445 15 my $match = $options{match};
446
447 15 ▶ croak 'match must be a compiled regex'
Branch: 1 branch
BranchTrueFalseTotal
if $match and ref $match ne "Regexp" 1 14 15
Condition100%3/3
$match and ref $match ne "Regexp"
!ll&&!rl&&r
1131
MC/DC100%2/2
$match and ref $match ne "Regexp"
$match ref $match ne "Regexp"
Truth table100%3/3
A and B
A= $matchB= ref $match ne "Regexp"
ABresult
0X0
100
111
448 if $match and ref($match) ne 'Regexp';
449  
450 14 ▶ croak 'must supply subfield code(s) and/or subfield position(s) and/or match patterns to delete_subfield'
Branch: 1 branch
BranchTrueFalseTotal
unless $match or @$codes > 0 or @$positions > 0 2 12 14
Condition100%6/6
$match or @$codes > 0 or @$positions > 0
l!l&&r!l&&!r
1022
$match or @$codes > 0
l!l&&r!l&&!r
374
MC/DC100%3/3
$match or @$codes > 0 or @$positions > 0
$match @$codes > 0 @$positions > 0
Truth table100%4/4
A or B or C
A= $matchB= @$codes > 0C= @$positions > 0
ABCresult
1XX1
01X1
0011
0000
451 unless $match or (@$codes > 0) or (@$positions > 0);
452  
453 12 my @current_subfields = @{$self->{_subfields}};
454 12 my @new_subfields = ();
455 12 my $removed = 0;
456 12 my $subfield_num = $[ - 1; # users $[ preferences control indexing
457  
458 12 ▶ while (@current_subfields > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@current_subfields > 0) 36 12 48
459 36 $subfield_num += 1;
460 36 my $subfield_code = shift @current_subfields;
461 36 my $subfield_value = shift @current_subfields;
462 36 ▶ if ((@$codes==0 or
Branch: 1 branch
BranchTrueFalseTotal
if (@$codes == 0 || grep({ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codes) and !$match || $subfield_value =~ /$match/ and @$positions == 0 || grep({$_ == $subfield_num;} @$positions)) 16 20 36
Condition100%9/9
@$codes == 0 || grep({ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codes) and !$match || $subfield_value =~ /$match/ and @$positions == 0 || grep({$_ == $subfield_num;} @$positions)
!ll&&!rl&&r
16416
@$codes == 0 || grep({ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codes) and !$match || $subfield_value =~ /$match/
!ll&&!rl&&r
11520
@$codes == 0 or grep {ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codes
l!l&&r!l&&!r
101511
MC/DC0%0/4
@$codes == 0 || grep({ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codes) and !$match || $subfield_value =~ /$match/ and @$positions == 0 || grep({$_ == $subfield_num;} @$positions)
@$codes == 0 grep {ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codes !$match || $subfield_value =~ /$match/ @$positions == 0 || grep({$_ == $subfield_num;} @$positions)
Truth table14%1/7
A or B and C and D
A= @$codes == 0B= grep {ref $_ eq "Regexp" and $subfield_code =~ /$_/ or ref $_ ne "Regexp" and $_ eq $subfield_code;} @$codesC= !$match || $subfield_value =~ /$match/D= @$positions == 0 || grep({$_ == $subfield_num;} @$positions)
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
463 grep {
464 29 ▶ (ref($_) eq 'Regexp' && $subfield_code =~ $_) ||
Condition100%15/15
ref $_ eq 'Regexp' && $subfield_code =~ /$_/ || ref $_ ne 'Regexp' && $_ eq $subfield_code
l!l&&r!l&&!r
11414
ref $_ eq "Regexp" and $subfield_code =~ /$_/
!ll&&!rl&&r
2621
ref $_ ne "Regexp" and $_ eq $subfield_code
!ll&&!rl&&r
21214
not $match or $subfield_value =~ /$match/
l!l&&r!l&&!r
1735
@$positions == 0 or grep {$_ == $subfield_num;} @$positions
l!l&&r!l&&!r
1154
MC/DC75%3/4
ref $_ eq 'Regexp' && $subfield_code =~ /$_/ || ref $_ ne 'Regexp' && $_ eq $subfield_code
ref $_ eq "Regexp" $subfield_code =~ /$_/ ref $_ ne "Regexp" $_ eq $subfield_code
MC/DC100%2/2
not $match or $subfield_value =~ /$match/
not $match $subfield_value =~ /$match/
MC/DC100%2/2
@$positions == 0 or grep {$_ == $subfield_num;} @$positions
@$positions == 0 grep {$_ == $subfield_num;} @$positions
Truth table57%4/7
A and B || C and D
A= ref $_ eq "Regexp"B= $subfield_code =~ /$_/C= ref $_ ne "Regexp"D= $_ eq $subfield_code
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
Truth table100%3/3
not(A) or B
A= not $matchB= $subfield_value =~ /$match/
ABresult
1X1
011
000
Truth table100%3/3
A or B
A= @$positions == 0B= grep {$_ == $subfield_num;} @$positions
ABresult
1X1
011
000
465 (ref($_) ne 'Regexp' && $_ eq $subfield_code)
466 } @$codes)
467 and (!$match or $subfield_value =~ $match)
468 15 and (@$positions==0 or grep {$_ == $subfield_num} @$positions)) {
469 16 $removed += 1;
470 16 next;
471 }
472 20 push( @new_subfields, $subfield_code, $subfield_value);
473 }
474 12 $self->{_subfields} = \@new_subfields;
475 12 return $removed;
476 }
477  
478 =head2 delete_subfields()
479  
480 Delete all subfields with a given subfield code. This is here for backwards
481 compatibility, you should use the more flexible delete_subfield().
482  
483 =cut
484  
485 1 ▶sub delete_subfields {
Subroutine delete_subfields: called CC=1 SCAR=0.0
Pod documented
486 1 my ($self, $code) = @_;
487 1 return $self->delete_subfield(code => $code);
488 }
489  
490 =head2 update()
491  
492 Allows you to change the values of the field. You can update indicators
493 and subfields like this:
494  
495 $field->update( ind2 => '4', a => 'The ballad of Abe Lincoln');
496  
497 If you attempt to update a subfield which does not currently exist in the field,
498 then a new subfield will be appended to the field. If you don't like this
499 auto-vivification you must check for the existence of the subfield prior to
500 update.
501  
502 if ( $field->subfield( 'a' ) ) {
503 $field->update( 'a' => 'Cryptonomicon' );
504 }
505  
506 If you want to update a field that has no indicators or subfields (000-009)
507 just call update() with one argument, the string that you would like to
508 set the field to.
509  
510 $field = $record->field( '003' );
511 $field->update('IMchF');
512  
513 Note: when doing subfield updates be aware that C<update()> will only
514 update the first occurrence. If you need to do anything more complicated
515 you will probably need to create a new field and use C<replace_with()>.
516  
517 Returns the number of items modified.
518  
519 =cut
520  
521 6 ▶sub update {
Subroutine update: called CC=7 SCAR=19.5
Pod documented
522 6 my $self = shift;
523  
524 ## tags 000 - 009 don't have indicators or subfields
525 6 ▶ if ( $self->is_control_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_control_field) 1 5 6
526 1 $self->{_data} = shift;
527 1 return(1);
528 }
529  
530 ## otherwise we need to update subfields and indicators
531 5 my @data = @{$self->{_subfields}};
532 5 my $changes = 0;
533  
534 5 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 9 5 14
535  
536 9 my $arg = shift;
537 9 my $val = shift;
538  
539 ## indicator update
540 9 ▶ if ($arg =~ /^ind[12]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^ind[12]$/) { } 1 8 9
541 1 $self->{"_$arg"} = $val;
542 1 $changes++;
543 }
544  
545 ## subfield update
546 else {
547 8 my $found = 0;
548 ## update existing subfield
549 8 ▶ for ( my $i=0; $i<@data; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @data) 17 3 20
550 17 ▶ if ($data[$i] eq $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data[$i] eq $arg) 5 12 17
551 5 $data[$i+1] = $val;
552 5 $found = 1;
553 5 $changes++;
554 5 last;
555 }
556 } # for
557  
558 ## append new subfield
559 8 ▶ if ( !$found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 3 5 8
560 3 push( @data, $arg, $val );
561 3 $changes++;
562 }
563 }
564  
565 } # while
566  
567 ## synchronize our subfields
568 5 $self->{_subfields} = \@data;
569 5 return($changes);
570  
571 }
572  
573 =head2 replace_with()
574  
575 Allows you to replace an existing field with a new one. You need to pass
576 C<replace()> a MARC::Field object to replace the existing field with. For
577 example:
578  
579 $field = $record->field('245');
580 my $new_field = new MARC::Field('245','0','4','The ballad of Abe Lincoln.');
581 $field->replace_with($new_field);
582  
583 Doesn't return a meaningful or reliable value.
584  
585 =cut
586  
587 1 ▶sub replace_with {
Subroutine replace_with: called CC=2 SCAR=7.1
Pod documented
588  
589 1 my ($self,$new) = @_;
590 1 ▶ ref($new) =~ /^MARC::Field$/
Branch: 1 branch
BranchTrueFalseTotal
ref($new) =~ /^MARC::Field$/ or croak("Must pass a MARC::Field object") 1 0 1
591 or croak("Must pass a MARC::Field object");
592  
593 1 %$self = %$new;
594  
595 }
596  
597  
598 =head2 as_string( [$subfields] [, $delimiter] )
599  
600 Returns a string of all subfields run together. A space is added to
601 the result between each subfield, unless the delimiter parameter is
602 passed. The tag number and subfield character are not included.
603  
604 Subfields appear in the output string in the order in which they
605 occur in the field.
606  
607 If C<$subfields> is specified, then only those subfields will be included.
608  
609 my $field = MARC::Field->new(
610 245, '1', '0',
611 'a' => 'Abraham Lincoln',
612 'h' => '[videorecording] :',
613 'b' => 'preserving the union /',
614 'c' => 'A&E Home Video.'
615 );
616 print $field->as_string( 'abh' ); # Only those three subfields
617 # prints 'Abraham Lincoln [videorecording] : preserving the union /'.
618 print $field->as_string( 'ab', '--' ); # Only those two subfields, with a delimiter
619 # prints 'Abraham Lincoln--preserving the union /'.
620  
621 Note that subfield h comes before subfield b in the output.
622  
623 =cut
624  
625 72 ▶sub as_string {
Subroutine as_string: called CC=6 SCAR=17.9
Pod documented
626 72 my $self = shift;
627 72 my $subfields = shift;
628 72 my $delimiter = shift;
629 72 ▶ $delimiter = " " unless defined $delimiter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delimiter 71 1 72
630  
631 72 ▶ if ( $self->is_control_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_control_field) 5 67 72
632 5 return $self->{_data};
633 }
634  
635 67 my @subs;
636  
637 67 my $subs = $self->{_subfields};
638 67 my $nfields = @$subs / 2;
639 67 for my $i ( 1..$nfields ) {
640 115 my $offset = ($i-1)*2;
641 115 my $code = $subs->[$offset];
642 115 my $text = $subs->[$offset+1];
643 115 ▶ push( @subs, $text ) if !defined($subfields) || $code =~ /^[$subfields]$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $subfields or $code =~ /^[$subfields]$/ 105 10 115
Condition100%3/3
not defined $subfields or $code =~ /^[$subfields]$/
l!l&&r!l&&!r
822310
MC/DC100%2/2
not defined $subfields or $code =~ /^[$subfields]$/
not defined $subfields $code =~ /^[$subfields]$/
Truth table100%3/3
not(A) or B
A= not defined $subfieldsB= $code =~ /^[$subfields]$/
ABresult
1X1
011
000
644 } # for
645  
646 67 return join( $delimiter, @subs );
647 }
648  
649  
650 =head2 as_formatted()
651  
652 Returns a pretty string for printing in a MARC dump.
653  
654 =cut
655  
656 194 ▶sub as_formatted {
Subroutine as_formatted: called CC=3 SCAR=11.0
Pod documented
657 194 my $self = shift;
658  
659 194 my @lines;
660  
661 194 ▶ if ( $self->is_control_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_control_field) { } 38 156 194
662 38 push( @lines, sprintf( "%03s %s", $self->{_tag}, $self->{_data} ) );
663 } else {
664 156 my $hanger = sprintf( "%03s %1.1s%1.1s", $self->{_tag}, $self->{_ind1}, $self->{_ind2} );
665  
666 156 my $subs = $self->{_subfields};
667 156 my $nfields = @$subs / 2;
668 156 my $offset = 0;
669 156 for my $i ( 1..$nfields ) {
670 262 push( @lines, sprintf( "%-6.6s _%1.1s%s", $hanger, $subs->[$offset++], $subs->[$offset++] ) );
671 262 $hanger = "";
672 } # for
673 }
674  
675 194 return join( "\n", @lines );
676 }
677  
678  
679 =head2 as_usmarc()
680  
681 Returns a string for putting into a USMARC file. It's really only
682 useful for C<MARC::Record::as_usmarc()>.
683  
684 =cut
685  
686 45 ▶sub as_usmarc {
Subroutine as_usmarc: called CC=3 SCAR=11.0
Pod documented
687 45 my $self = shift;
688  
689 # Control fields are pretty easy
690 45 ▶ if ( $self->is_control_field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_control_field) { } 6 39 45
691 6 return $self->data . END_OF_FIELD;
692 } else {
693 39 my @subs;
694 39 my @subdata = @{$self->{_subfields}};
695 39 ▶ while ( @subdata ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subdata) 52 39 91
696 52 push( @subs, join( "", SUBFIELD_INDICATOR, shift @subdata, shift @subdata ) );
697 } # while
698  
699 return
700 39 join( "",
701 $self->indicator(1),
702 $self->indicator(2),
703 @subs,
704 END_OF_FIELD, );
705 }
706 }
707  
708 =head2 clone()
709  
710 Makes a copy of the field. Note that this is not just the same as saying
711  
712 my $newfield = $field;
713  
714 since that just makes a copy of the reference. To get a new object, you must
715  
716 my $newfield = $field->clone;
717  
718 Returns a MARC::Field record.
719  
720 =cut
721  
722 18 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
723 18 my $self = shift;
724  
725 18 my $tagno = $self->{_tag};
726 18 my $is_control = $self->is_controlfield_tag($tagno);
727  
728 18 my $clone =
729 bless {
730 _tag => $tagno,
731 _warnings => [],
732 _is_control_field => $is_control,
733 }, ref($self);
734  
735 18 ▶ if ( $is_control ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_control) { } 4 14 18
736 4 $clone->{_data} = $self->{_data};
737 } else {
738 14 $clone->{_ind1} = $self->{_ind1};
739 14 $clone->{_ind2} = $self->{_ind2};
740 14 $clone->{_subfields} = [@{$self->{_subfields}}];
741 }
742  
743 18 return $clone;
744 }
745  
746 =head2 warnings()
747  
748 Returns the warnings that were created when the record was read.
749 These are things like "Invalid indicators converted to blanks".
750  
751 The warnings are items that you might be interested in, or might
752 not. It depends on how stringently you're checking data. If
753 you're doing some grunt data analysis, you probably don't care.
754  
755 =cut
756  
757 2018 ▶sub warnings {
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
758 2018 my $self = shift;
759  
760 2018 return @{$self->{_warnings}};
761 }
762  
763 # NOTE: _warn is an object method
764 12 ▶sub _warn {
Subroutine _warn: called CC=1 SCAR=0.0
765 12 my $self = shift;
766  
767 12 push( @{$self->{_warnings}}, join( "", @_ ) );
768 }
769  
770 0 ▶sub _gripe {
Subroutine _gripe: not called CC=1 SCAR=6.9
771 0 $ERROR = join( "", @_ );
772  
773 0 warn $ERROR;
774  
775 0 return;
776 }
777  
778 30 ▶sub _normalize_arrayref {
Subroutine _normalize_arrayref: called CC=3 SCAR=11.0
779 30 my $ref = shift;
780 30 ▶ if (ref($ref) eq 'ARRAY') { return $ref }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref eq 'ARRAY') { } 4 26 30
elsif (defined $ref) { } 9 17 26
781 9 elsif (defined $ref) { return [$ref] }
782 17 return [];
783 }
784  
785  
786 40 1;
787  
788 __END__
« File.pm ↑ Summary Batch.pm »