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

Pakistan Open Source Organization


MARC / PERL: Koha Integrated Library System

MARC Tutorial

Updating subfields and indicators

If you'd rather not replace an existing field with a new one, you can also edit the contents of the field itself using the update() method. Let's say you've got a batch of records and want to make sure that the 2nd indicator for the 245 field is properly set for titles that begin with 'The' (where the indicator should be '4').

1 ## Example U5
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 ## retrieve the 245 record.
10 my $field_245 = $record->field('245');
11
12 ## if we got 245 and it starts with 'The'...
13 if ($field_245 and $field_245->as_string() =~ /^The /) {
14
15 ## if the 2nd indicator isn't 4, update
16 if ($field_245->indicator(2) != 4) {
17 $field_245->update( ind2 => 4 );
18 }
19
20 }
21
22 print OUT $record->as_usmarc();
23
24 }

In a similar fashion, you can update individual or multiple subfields:

$field_245->update( a => 'History of the World :', b => 'part 1' );

But beware, you can only update the first occurrence of a subfield using update(). If you need to do more finer grained updates, you are advised to build a new field and replace the existing field with replace_with().

Changing a record's leader

The above procedure works for fields, but editing the leader requires that you use the leader() method. When called with no arguments, leader()will return the current leader, and when you pass a scalar value as an argument, the leader will be set to this value. This example shows how you might want to update position 6 of a records leader to reflect a computer file.

1 ## Example U6
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','file.dat');
5 open(OUT,'>new.dat') or die $!;
6 my $record = $batch->next();
7
8 ## get the current leader.
9 my $leader = $record->leader();
10
11 ## replace position 6 with 'm'
12 substr($leader,6,1) = 'm';
13
14 ## update the leader
15 $record->leader($leader);
16
17 ## save the record to a file
18 print OUT $record->as_usmarc();