Record.pm

filter: Statementstmt 95.1% 197 / 207 Branchbran 85.2% 58 / 68 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 97.2% 35 / 36 Podpod 100% 23 / 23 totaltotal 91.0% 316 / 347 CC 48 SCAR 13.4
CC
48
Cov
91%
add_fields
7.8
field
7.0
clone
5.5
CRAP 3.8
« Field.pm ↑ Summary File/Utils.pm »
1 package MARC::Record;
2  
3 =head1 NAME
4  
5 MARC::Record - Perl extension for handling MARC records
6  
7 =cut
8  
9 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 31 ▶use vars qw( $ERROR );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 31 ▶use MARC::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 VERSION
18  
19 Version 1.39_02
20  
21 =cut
22  
23 31 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 31 $VERSION = '1.39_02';
25  
26 31 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 31 ▶use vars qw( @ISA @EXPORTS @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 31 @ISA = qw( Exporter );
29 31 @EXPORTS = qw();
30 31 @EXPORT_OK = qw( LEADER_LEN );
31  
32 31 ▶use vars qw( $DEBUG ); $DEBUG = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 31 ▶use constant LEADER_LEN => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 =head1 DESCRIPTION
37  
38 Module for handling MARC records as objects. The file-handling stuff is
39 in MARC::File::*.
40  
41 =head1 ERROR HANDLING
42  
43 Any errors generated are stored in C<$MARC::Record::ERROR>.
44 Warnings are kept with the record and accessible in the C<warnings()> method.
45  
46 =head1 CONSTRUCTORS
47  
48 =head2 new()
49  
50 Base constructor for the class. It just returns a completely empty record.
51 To get real data, you'll need to populate it with fields, or use one of
52 the MARC::File::* modules to read from a file.
53  
54 =cut
55  
56 sub new {
57 390 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
58 390 my $self = {
59 _leader => ' ' x 24,
60 _fields => [],
61 _warnings => [],
62 };
63 390 return bless $self, $class;
64 } # new()
65  
66 =head2 new_from_usmarc( $marcblob [, \&filter_func($tagno,$tagdata)] )
67  
68 This is a wrapper around C<MARC::File::USMARC::decode()> for compatibility with
69 older versions of MARC::Record.
70  
71 The C<wanted_func()> is optional. See L<MARC::File::USMARC>::decode for details.
72  
73 =cut
74  
75 sub new_from_usmarc {
76 2 ▶ my $blob = shift;
Subroutine new_from_usmarc: called CC=3 SCAR=12.0
Pod documented
77 2 ▶ $blob = shift if (ref($blob) || ($blob eq "MARC::Record"));
Branch: 1 branch
BranchTrueFalseTotal
if ref $blob or $blob eq "MARC::Record" 2 0 2
Condition33%1/3
ref $blob or $blob eq "MARC::Record"
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $blob or $blob eq "MARC::Record"
ref $blob $blob eq "MARC::Record"
Truth table33%1/3
A or B
A= ref $blobB= $blob eq "MARC::Record"
ABresult
1X1
011
000
78  
79 2 require MARC::File::USMARC;
80  
81 2 return MARC::File::USMARC::decode( $blob, @_ );
82 }
83  
84 =head1 COMMON FIELD RETRIEVAL METHODS
85  
86 Following are a number of convenience methods for commonly-retrieved
87 data fields. Please note that they each return strings, not MARC::Field
88 objects. They return empty strings if the appropriate field or subfield
89 is not found. This is as opposed to the C<field()>/C<subfield()> methods
90 which return C<undef> if something's not found. My assumption is that
91 these methods are used for quick & dirty reports and you don't want to
92 mess around with noting if something is undef.
93  
94 Also note that no punctuation cleanup is done. If the 245a is
95 "Programming Perl / ", then that's what you'll get back, rather than
96 "Programming Perl".
97  
98 =head2 title()
99  
100 Returns the title from the 245 tag.
101  
102 =cut
103  
104 sub title() {
105 11 ▶ my $self = shift;
Subroutine title: called CC=2 SCAR=6.9
Pod documented
106  
107 11 my $field = $self->field(245);
108 11 ▶ return $field ? $field->as_string : "";
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 9 2 11
109 }
110  
111 =head2 title_proper()
112  
113 Returns the title proper from the 245 tag, subfields a, n and p.
114  
115 =cut
116  
117 sub title_proper() {
118 11 ▶ my $self = shift;
Subroutine title_proper: called CC=2 SCAR=6.9
Pod documented
119  
120 11 my $field = $self->field(245);
121  
122 11 ▶ if ( $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 9 2 11
123 9 return $field->as_string('anp');
124 } else {
125 2 return "";
126 }
127 }
128  
129 =head2 author()
130  
131 Returns the author from the 100, 110 or 111 tag.
132  
133 =cut
134  
135 sub author() {
136 6 ▶ my $self = shift;
Subroutine author: called CC=2 SCAR=6.9
Pod documented
137  
138 6 my $field = $self->field('100|110|111');
139 6 ▶ return $field ? $field->as_string : "";
Branch: 1 branch
BranchTrueFalseTotal
$field ? : 3 3 6
140 }
141  
142 =head2 edition()
143  
144 Returns the edition from the 250 tag, subfield a.
145  
146 =cut
147  
148 sub edition() {
149 6 ▶ my $self = shift;
Subroutine edition: called CC=2 SCAR=6.9
Pod documented
150  
151 6 my $str = $self->subfield(250,'a');
152 6 ▶ return defined $str ? $str : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 2 4 6
153 }
154  
155 =head2 publication_date()
156  
157 Returns the publication date from the 260 tag, subfield c.
158  
159 =cut
160  
161 sub publication_date() {
162 6 ▶ my $self = shift;
Subroutine publication_date: called CC=2 SCAR=6.9
Pod documented
163  
164 6 my $str = $self->subfield(260,'c');
165 6 ▶ return defined $str ? $str : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 3 3 6
166 }
167  
168 =head1 FIELD & SUBFIELD ACCESS METHODS
169  
170 =head2 fields()
171  
172 Returns a list of all the fields in the record. The list contains
173 a MARC::Field object for each field in the record.
174  
175 =cut
176  
177 sub fields() {
178 256 ▶ my $self = shift;
Subroutine fields: called CC=1 SCAR=0.0
Pod documented
179 256 return @{$self->{_fields}};
180 }
181  
182 =head2 field( I<tagspec(s)> )
183  
184 Returns a list of tags that match the field specifier, or an empty
185 list if nothing matched. In scalar context, returns the first
186 matching tag, or undef if nothing matched.
187  
188 The field specifier can be a simple number (i.e. "245"), or use the "."
189 notation of wildcarding (i.e. subject tags are "6..").
190  
191 =cut
192  
193 31 my %field_regex;
194  
195 sub field {
196 232 ▶ my $self = shift;
Subroutine field: called CC=7 SCAR=19.5
Pod documented
197 232 my @specs = @_;
198  
199 232 my @list = ();
200 232 for my $tag ( @specs ) {
201 232 my $regex = $field_regex{ $tag };
202  
203 # Compile & stash it if necessary
204 232 ▶ if ( not defined $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 44 188 232
205 44 $regex = qr/^$tag$/;
206 44 $field_regex{ $tag } = $regex;
207 } # not defined
208  
209 232 for my $maybe ( $self->fields ) {
210 1312 ▶ if ( $maybe->tag =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe->tag =~ /$regex/) 217 1095 1312
211 217 ▶ return $maybe unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 211 6 217
212  
213 6 push( @list, $maybe );
214 } # if
215 } # for $maybe
216 } # for $tag
217  
218 21 ▶ return unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 17 4 21
219 4 return @list;
220 }
221  
222 =head2 subfield( $tag, $subfield )
223  
224 Shortcut method for getting just a subfield for a tag. These are equivalent:
225  
226 my $title = $marc->field('245')->subfield("a");
227 my $title = $marc->subfield('245',"a");
228  
229 If either the field or subfield can't be found, C<undef> is returned.
230  
231 =cut
232  
233 sub subfield {
234 16 ▶ my $self = shift;
Subroutine subfield: called CC=2 SCAR=6.9
Pod documented
235 16 my $tag = shift;
236 16 my $subfield = shift;
237  
238 16 ▶ my $field = $self->field($tag) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $field = $self->field($tag) or return 7 9 16
239 9 return $field->subfield($subfield);
240 } # subfield()
241  
242 =for internal
243  
244 =cut
245  
246 sub _all_parms_are_fields {
247 2382 ▶ for ( @_ ) {
Subroutine _all_parms_are_fields: called CC=3 SCAR=11.0
248 2397 ▶ return 0 unless ref($_) eq 'MARC::Field';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "MARC::Field" 4 2393 2397
249 }
250 2378 return 1;
251 }
252  
253 =head2 append_fields( @fields )
254  
255 Appends the field specified by C<$field> to the end of the record.
256 C<@fields> need to be MARC::Field objects.
257  
258 my $field = MARC::Field->new('590','','','a' => 'My local note.');
259 $record->append_fields($field);
260  
261 Returns the number of fields appended.
262  
263 =cut
264  
265 sub append_fields {
266 2368 ▶ my $self = shift;
Subroutine append_fields: called CC=2 SCAR=6.9
Pod documented
267  
268 2368 ▶ _all_parms_are_fields(@_) or croak('Arguments must be MARC::Field objects');
Branch: 1 branch
BranchTrueFalseTotal
_all_parms_are_fields(@_) or croak("Arguments must be MARC::Field objects") 1 2367 2368
269  
270 2367 push(@{ $self->{_fields} }, @_);
271 2367 return scalar @_;
272 }
273  
274 =head2 insert_fields_before( $before_field, @new_fields )
275  
276 Inserts the field specified by C<$new_field> before the field C<$before_field>.
277 Returns the number of fields inserted, or undef on failures.
278 Both C<$before_field> and all C<@new_fields> need to be MARC::Field objects.
279 If they are not an exception will be thrown.
280  
281 my $before_field = $record->field('260');
282 my $new_field = MARC::Field->new('250','','','a' => '2nd ed.');
283 $record->insert_fields_before($before_field,$new_field);
284  
285 =cut
286  
287 sub insert_fields_before {
288 7 ▶ my $self = shift;
Subroutine insert_fields_before: called CC=5 SCAR=16.1
Pod documented
289  
290 7 ▶ _all_parms_are_fields(@_)
Branch: 1 branch
BranchTrueFalseTotal
_all_parms_are_fields(@_) or croak("All arguments must be MARC::Field objects") 1 6 7
291 or croak('All arguments must be MARC::Field objects');
292  
293 6 my ($before,@new) = @_;
294  
295 ## find position of $before
296 6 my $fields = $self->{_fields};
297 6 my $pos = 0;
298 6 foreach my $f (@$fields) {
299 68 ▶ last if ($f == $before);
Branch: 1 branch
BranchTrueFalseTotal
if $f == $before 5 63 68
300 63 $pos++;
301 }
302  
303 ## insert before $before
304 6 ▶ if ($pos >= @$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= @$fields) 1 5 6
305 1 $self->_warn("Couldn't find field to insert before");
306 1 return;
307 }
308 5 splice(@$fields,$pos,0,@new);
309 5 return scalar @new;
310  
311 }
312  
313 =head2 insert_fields_after( $after_field, @new_fields )
314  
315 Identical to C<insert_fields_before()>, but fields are added after
316 C<$after_field>. Remember, C<$after_field> and any new fields must be
317 valid MARC::Field objects or else an exception will be thrown.
318  
319 =cut
320  
321 sub insert_fields_after {
322 3 ▶ my $self = shift;
Subroutine insert_fields_after: called CC=5 SCAR=16.3
Pod documented
323  
324 3 ▶ _all_parms_are_fields(@_) or croak('All arguments must be MARC::Field objects');
Branch: 1 branch
BranchTrueFalseTotal
_all_parms_are_fields(@_) or croak("All arguments must be MARC::Field objects") 1 2 3
325 2 my ($after,@new) = @_;
326  
327 ## find position of $after
328 2 my $fields = $self->{_fields};
329 2 my $pos = 0;
330 2 foreach my $f (@$fields) {
331 32 ▶ last if ($f == $after);
Branch: 1 branch
BranchTrueFalseTotal
if $f == $after 2 30 32
332 30 $pos++;
333 }
334  
335 ## insert after $after
336 2 ▶ if ($pos+1 >= @$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 1 >= @$fields) 0 2 2
337 0 $self->_warn("Couldn't find field to insert after");
338 0 return;
339 }
340 2 splice(@$fields,$pos+1,0,@new);
341 2 return scalar @new;
342 }
343  
344 =head2 insert_fields_ordered( @new_fields )
345  
346 Will insert fields in strictly numerical order. So a 008 will be filed
347 after a 001 field. See C<insert_grouped_field()> for an additional ordering.
348  
349 =cut
350  
351 sub insert_fields_ordered {
352 2 ▶ my ( $self, @new ) = @_;
Subroutine insert_fields_ordered: called CC=5 SCAR=16.1
Pod documented
353  
354 2 ▶ _all_parms_are_fields(@new)
Branch: 1 branch
BranchTrueFalseTotal
_all_parms_are_fields(@new) or croak("All arguments must be MARC::Field objects") 0 2 2
355 or croak('All arguments must be MARC::Field objects');
356  
357 ## go through each new field
358 2 NEW_FIELD: foreach my $newField ( @new ) {
359  
360 ## find location before which it should be inserted
361 4 EXISTING_FIELD: foreach my $field ( @{ $self->{_fields} } ) {
362 4 ▶ if ( $field->tag() >= $newField->tag() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->tag >= $newField->tag) 2 2 4
363 2 $self->insert_fields_before( $field, $newField );
364 2 next NEW_FIELD;
365 }
366 }
367  
368 ## if we fell through then this new field is higher than
369 ## all the existing fields, so we append.
370 2 $self->append_fields( $newField );
371  
372 }
373 2 return( scalar( @new ) );
374 }
375  
376 =head2 insert_grouped_field( $field )
377  
378 Will insert the specified MARC::Field object into the record in grouped
379 order and return true (1) on success, and false (undef) on failure.
380  
381 my $field = MARC::Field->new( '510', 'Indexed by Google.' );
382 $record->insert_grouped_field( $field );
383  
384 For example, if a '650' field is inserted with C<insert_grouped_field()>
385 it will be inserted at the end of the 6XX group of tags. After discussion
386 most people wanted the ability to add a new field to the end of the
387 hundred group where it belonged. The reason is that according to the MARC
388 format, fields within a record are supposed to be grouped by block
389 (hundred groups). This means that fields may not necessarily be in tag
390 order.
391  
392 =cut
393  
394 sub insert_grouped_field {
395 2 ▶ my ($self,$new) = @_;
Subroutine insert_grouped_field: called CC=5 SCAR=16.2
Pod documented
396 2 ▶ _all_parms_are_fields($new) or croak('Argument must be MARC::Field object');
Branch: 1 branch
BranchTrueFalseTotal
_all_parms_are_fields($new) or croak("Argument must be MARC::Field object") 1 1 2
397  
398 ## try to find the end of the field group and insert it there
399 1 my $limit = int($new->tag() / 100);
400 1 my $found = 0;
401 1 foreach my $field ($self->fields()) {
402 14 ▶ if ( int($field->tag() / 100) > $limit ) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $field->tag / 100 > $limit) 1 13 14
403 1 $self->insert_fields_before($field,$new);
404 1 $found = 1;
405 1 last;
406 }
407 }
408  
409 ## if we couldn't find the end of the group, then we must not have
410 ## any tags this high yet, so just append it
411 1 ▶ if (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 1 1
412 0 $self->append_fields($new);
413 }
414  
415 1 return(1);
416  
417 }
418  
419  
420 =head2 delete_field( $field )
421  
422 Deletes a field from the record.
423  
424 The field must have been retrieved from the record using the
425 C<field()> method. For example, to delete a 526 tag if it exists:
426  
427 my $tag526 = $marc->field( "526" );
428 if ( $tag526 ) {
429 $marc->delete_field( $tag526 );
430 }
431  
432 C<delete_field()> returns the number of fields that were deleted.
433 This shouldn't be 0 unless you didn't get the tag properly.
434  
435 =cut
436  
437 sub delete_field {
438 3 ▶ my $self = shift;
Subroutine delete_field: called CC=1 SCAR=0.0
Pod documented
439 3 my $deleter = shift;
440 3 my $list = $self->{_fields};
441  
442 3 my $old_count = @$list;
443 3 @$list = grep { $_ != $deleter } @$list;
444 3 return $old_count - @$list;
445 }
446  
447 =head2 as_usmarc()
448  
449 This is a wrapper around C<MARC::File::USMARC::encode()> for compatibility with
450 older versions of MARC::Record.
451  
452 =cut
453  
454 sub as_usmarc() {
455 7 ▶ my $self = shift;
Subroutine as_usmarc: called CC=1 SCAR=0.0
Pod documented
456  
457 7 require MARC::File::USMARC;
458  
459 7 return MARC::File::USMARC::encode( $self );
460 }
461  
462 =head2 as_formatted()
463  
464 Returns a pretty string for printing in a MARC dump.
465  
466 =cut
467  
468 sub as_formatted() {
469 14 ▶ my $self = shift;
Subroutine as_formatted: called CC=3 SCAR=11.0
Pod documented
470  
471 14 ▶ my @lines = ( "LDR " . ($self->{_leader} || "") );
Condition50%1/2
$self->{'_leader'} || ''
l!l
140
MC/DC0%0/1
$self->{'_leader'} || ''
$self->{'_leader'}
Truth table50%1/2
A
A= $self->{'_leader'}
Aresult
11
00
472 14 for my $field ( @{$self->{_fields}} ) {
473 193 push( @lines, $field->as_formatted() );
474 }
475  
476 14 return join( "\n", @lines );
477 } # as_formatted
478  
479  
480 =head2 leader()
481  
482 Returns the leader for the record. Sets the leader if I<text> is defined.
483 No error checking is done on the validity of the leader.
484  
485 =cut
486  
487 sub leader {
488 393 ▶ my $self = shift;
Subroutine leader: called CC=3 SCAR=11.0
Pod documented
489 393 my $text = shift;
490  
491 393 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 381 12 393
492 381 ▶ (length($text) eq 24)
Branch: 1 branch
BranchTrueFalseTotal
length $text eq 24 or $self->_warn("Leader must be 24 bytes long") 2 379 381
493 or $self->_warn( "Leader must be 24 bytes long" );
494 381 $self->{_leader} = $text;
495 } # set the leader
496  
497 393 return $self->{_leader};
498 } # leader()
499  
500 =head2 set_leader_lengths( $reclen, $baseaddr )
501  
502 Internal function for updating the leader's length and base address.
503  
504 =cut
505  
506 sub set_leader_lengths {
507 8 ▶ my $self = shift;
Subroutine set_leader_lengths: called CC=1 SCAR=0.0
Pod documented
508 8 my $reclen = shift;
509 8 my $baseaddr = shift;
510 8 substr($self->{_leader},0,5) = sprintf("%05d",$reclen);
511 8 substr($self->{_leader},12,5) = sprintf("%05d",$baseaddr);
512 # MARC21 defaults: http://www.loc.gov/marc/bibliographic/ecbdldrd.html
513 8 substr($self->{_leader},10,2) = '22';
514 8 substr($self->{_leader},20,4) = '4500';
515 }
516  
517 =head2 clone()
518  
519 The C<clone()> method makes a copy of an existing MARC record and returns
520 the new version. Note that you cannot just say:
521  
522 my $newmarc = $oldmarc;
523  
524 This just makes a copy of the reference, not a new object. You must use
525 the C<clone()> method like so:
526  
527 my $newmarc = $oldmarc->clone;
528  
529 You can also specify field specs to filter down only a
530 certain subset of fields. For instance, if you only wanted the
531 title and ISBN tags from a record, you could do this:
532  
533 my $small_marc = $marc->clone( 245, '020' );
534  
535 The order of the fields is preserved as it was in the original record.
536  
537 =cut
538  
539 sub clone {
540 1 ▶ my $self = shift;
Subroutine clone: called CC=5 SCAR=17.1
Pod documented
541 1 my @keeper_tags = @_;
542  
543 # create a new object of whatever type we happen to be
544 1 my $class = ref( $self );
545 1 my $clone = $class->new();
546  
547 1 $clone->{_leader} = $self->{_leader};
548  
549 1 ▶ my $filtered = @keeper_tags ? [$self->field( @keeper_tags )] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@keeper_tags ? : 0 1 1
550  
551 1 for my $field ( $self->fields() ) {
552 18 ▶ if ( !$filtered || (grep {$field eq $_} @$filtered ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $filtered or grep {$field eq $_;} @$filtered) 18 0 18
Condition33%1/3
not $filtered or grep {$field eq $_;} @$filtered
l!l&&r!l&&!r
1800
MC/DC0%0/2
not $filtered or grep {$field eq $_;} @$filtered
not $filtered grep {$field eq $_;} @$filtered
Truth table33%1/3
not(A) or B
A= not $filteredB= grep {$field eq $_;} @$filtered
ABresult
1X1
011
000
553 18 $clone->append_fields( $field->clone );
554 }
555 }
556  
557 # XXX FIX THIS $clone->update_leader();
558  
559 1 return $clone;
560 }
561  
562 =head2 warnings()
563  
564 Returns the warnings (as a list) that were created when the record was read.
565 These are things like "Invalid indicators converted to blanks".
566  
567 my @warnings = $record->warnings();
568  
569 The warnings are items that you might be interested in, or might
570 not. It depends on how stringently you're checking data. If
571 you're doing some grunt data analysis, you probably don't care.
572  
573 A side effect of calling warnings() is that the warning buffer will
574 be cleared.
575  
576 =cut
577  
578 sub warnings() {
579 14 ▶ my $self = shift;
Subroutine warnings: called CC=1 SCAR=0.0
Pod documented
580 14 my @warnings = @{$self->{_warnings}};
581 14 $self->{_warnings} = [];
582 14 return @warnings;
583 }
584  
585 =head2 add_fields()
586  
587 C<add_fields()> is now deprecated, and users are encouraged to use
588 C<append_fields()>, C<insert_fields_after()>, and C<insert_fields_before()>
589 since they do what you want probably. It is still here though, for backwards
590 compatability.
591  
592 C<add_fields()> adds MARC::Field objects to the end of the list. Returns the
593 number of fields added, or C<undef> if there was an error.
594  
595 There are three ways of calling C<add_fields()> to add data to the record.
596  
597 =over 4
598  
599 =item 1 Create a MARC::Field object and add it
600  
601 my $author = MARC::Field->new(
602 100, "1", " ", a => "Arnosky, Jim."
603 );
604 $marc->add_fields( $author );
605  
606 =item 2 Add the data fields directly, and let C<add_fields()> take care of the objectifying.
607  
608 $marc->add_fields(
609 245, "1", "0",
610 a => "Raccoons and ripe corn /",
611 c => "Jim Arnosky.",
612 );
613  
614 =item 3 Same as #2 above, but pass multiple fields of data in anonymous lists
615  
616 $marc->add_fields(
617 [ 250, " ", " ", a => "1st ed." ],
618 [ 650, "1", " ", a => "Raccoons." ],
619 );
620  
621 =back
622  
623 =cut
624  
625 sub add_fields {
626 2068 ▶ my $self = shift;
Subroutine add_fields: called CC=7 SCAR=20.5
Pod documented
627  
628 2068 my $nfields = 0;
629 2068 my $fields = $self->{_fields};
630  
631 2068 ▶ while ( my $parm = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parm = shift()) 2077 1 2078
632 # User handed us a list of data (most common possibility)
633 2077 ▶ if ( ref($parm) eq "" ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $parm eq '') { } 2067 10 2077
elsif (ref $parm eq 'MARC::Field') { } 0 10 10
elsif (ref $parm eq 'ARRAY') { } 10 0 10
634 2067 ▶ my $field = MARC::Field->new( $parm, @_ )
Branch: 1 branch
BranchTrueFalseTotal
my $field = "MARC::Field"->new($parm, @_) or return _gripe($MARC::Field::ERROR) 0 2067 2067
635 or return _gripe( $MARC::Field::ERROR );
636 2067 push( @$fields, $field );
637 2067 ++$nfields;
638 2067 last; # Bail out, we're done eating parms
639  
640 # User handed us an object.
641 } elsif ( ref($parm) eq "MARC::Field" ) {
642 0 push( @$fields, $parm );
643 0 ++$nfields;
644  
645 # User handed us an anonymous list of parms
646 } elsif ( ref($parm) eq "ARRAY" ) {
647 10 ▶ my $field = MARC::Field->new(@$parm)
Branch: 1 branch
BranchTrueFalseTotal
my $field = "MARC::Field"->new(@$parm) or return _gripe($MARC::Field::ERROR) 0 10 10
648 or return _gripe( $MARC::Field::ERROR );
649 10 push( @$fields, $field );
650 10 ++$nfields;
651  
652 } else {
653 0 croak( "Unknown parm of type", ref($parm), " passed to add_fields()" );
654 } # if
655  
656 } # while
657  
658 2068 return $nfields;
659 }
660  
661 # NOTE: _warn is an object method
662 sub _warn {
663 15 ▶ my $self = shift;
Subroutine _warn: called CC=1 SCAR=0.0
664 15 push( @{$self->{_warnings}}, join( "", @_ ) );
665 15 return( $self );
666 }
667  
668  
669 # NOTE: _gripe is NOT an object method
670 sub _gripe {
671 0 ▶ $ERROR = join( "", @_ );
Subroutine _gripe: not called CC=1 SCAR=6.9
672  
673 0 warn $ERROR;
674  
675 0 return;
676 }
677  
678  
679 31 1;
680  
681 __END__
« Field.pm ↑ Summary File/Utils.pm »