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

Pakistan Open Source Organization


MARC / PERL: Koha Integrated Library System

MARC Tutorial

Looking at repeatable fields

So how do you retrieve data from repeatable fields? The field() method can help you with this as well. In our previous example's line 11, the field() method was used in a scalar context, since the result was being assigned to the variable $field. However in a list context, field() will return all the fields in the record of that particular type. For example:

1 ## Example R6
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','file.dat');
5 my $record = $batch->next();
6
7 ## get all the 650 fields (list context).
8 my @fields = $record->field('650');
9
10 ## examine each 650 field and print it out.
11 foreach my $field (@fields) {
12 print $field->as_string(),"\n";
13 }

Which prints out the following for the first record of 't/camel.usmarc':

Active server pages.
ActiveX.

Looking at a set of related fields

field()also allows you to retrieve similar fields using '.' as a wildcard.

1 ## Example R7
2
3 use MARC::Batch;
4 my $batch = MARC::Batch->new('USMARC','file.dat');
5 my $record = $batch->next();
6
7 # retrieve all title fields in one shot.
8 foreach my $field ($record->field('2..')) {
9 print $field->tag(),' contains ',$field->as_string(),"\n";
10 }

Notice the shorthand in line 8 which compacts lines 7-13 of our previous example. Instead of storing the fields in an array, the field() still returns a list in the for loop. Line 9 uses the tag() method which returns the tag number for a particular MARC field, which is useful when you aren't certain what tag you are currently dealing with. Sample output from this recipe:

245 contains ActivePerl with ASP and ADO / Tobias Martinsson.
260 contains New York : John Wiley & Sons, 2000.

You can also return all tags for a specific record by using '...' in field(though, see the next recipe).

Looking at all the fields in a record

The last example in this section illustrates how to retrieve all the fields in a record using the fields() method. This method is similar to passing '...' as a wildcard (see our previous recipe for alternative access).

1 ## Example R8
2
3 use MARC::Batch;
4 my $file = MARC::Batch->new('USMARC','file.dat');
5 my $record = $batch->next();
6
7 ## get all of the fields using the fields() method.
8 my @fields = $record->fields();
9
10 ## print out the tag, the indicators and the field contents.
11 foreach my $field (@fields) {
12 print
13 $field->tag(), " ",
14 defined $field->indicator(1) ? $field->indicator(1) : "",
15 defined $field->indicator(2) ? $field->indicator(2) : "",
16 " ", $field->as_string, " \n";
17 }

The above code would print the following for the first record of 't/camel.usmarc':

001 fol05731351
003 IMchF
.
.
.
300 xxi, 289 p. : ill. ; 23 cm. + 1 computer laser disc (4 3/4 in.)
500 "Wiley Computer Publishing."
650 0 Perl (Computer program language)
630 00 Active server pages.
630 00 ActiveX.