ادارہ پاکستان براےء آزاد مصدر

Pakistan Open Source Organization


MARC / PERL: Koha Integrated Library System

MARC Tutorial

Modifying fields without indicators

MARC::Record and MARC::Field are smart and know that you don't have field indicators with tags less than 010. Here's an example of updating/adding an 005 field to indicate a new transaction time. For a little pizzazz, we use Perl's localtime()to generate the data we need for this field.

1 ## Example U7
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','file.dat');
5 open(OUT,'>new.dat') or die $!;
6
7 while (my $record = $batch->next() ) {
8
9 ## see if there is a 005 field.
10 my $field_005 = $record->field('005');
11
12 ## delete it if we find one.
13 $record->delete_field($field_005) if $field_005;
14
15 ## figure out the contents of our new 005 field.
16 my ($sec,$min,$hour,$mday,$mon,$year) = localtime();
17 $year += 1900; $mon += 1; # catering to offsets.
18 my $datetime = sprintf("%4d%02d%02d%02d%02d%02d.0",
19 $year,$mon,$mday,$hour,$min,$sec);
20
21 ## create a new 005 field using our new datetime.
22 $record->append_fields( MARC::Field->new('005',$datetime) );
23
24 ## save record to a file.
25 print OUT $record->as_usmarc();
26
27 }

Reordering subfields

You may find yourself in the situation where you would like to programmatically reorder, and possibly modify, subfields in a particular field. For example, imagine that you have a batch of records that have 856 fields which contain subfields z, u, and possibly 3... in any order! Now imagine that you'd like to standardize the subfield z, and reorder them so that subfield 3 precedes subfield z, which precedes subfield u. This is tricky but can be done in the following manner: read in a record, extract the existing 856 field, build a new 856 field based on the existing one, replace the existing field with your newly created version.

1 ## Example U8
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','856.dat');
5 open(OUT,'>856_new.dat') or die $!;
6
7 while (my $record = $batch->next()) {
8
9 my $existing = $record->field('856');
10
11 ## make sure 856 exists.
12 if ($existing) {
13
14 ## our ordered subfields.
15 my @subfields = ();
16
17 ## if we have a subfield 3, add it.
18 if (defined($existing->subfield('3'))) {
19 push(@subfields,'3',$existing->subfield('3'));
20 }
21
22 ## now add subfields z and u.
23 push(@subfields,'z','Access restricted',
24 'u',$existing->subfield('u'));
25
26 ## create a new 856.
27 my $new = MARC::Field->new(
28 856’, $existing->indicator(1),
29 $existing->indicator(2), @subfields
30 );
31
32 ## replace the existing subfield.
33 $existing->replace_with($new);
34
35 }
36
37 ## write out the record
38 print OUT $record->as_usmarc();
39
40 }