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

Pakistan Open Source Organization


MARC / PERL: Koha Integrated Library System

MARC Tutorial

READING

Reading a record from a file

Let's say you have a USMARC record in 'file.dat' and you'd like to read in the record and print out its title.

1 ## Example R1
2
3 ## create a MARC::Batch object.
4 use MARC::Batch;
5 my $batch = MARC::Batch('USMARC', 'file.dat');
6
7 ## get a MARC record from the MARC::Batch object.
8 ## the $record will be a MARC::Record object.
9 my $record = $batch->next();
10
11 ## print the title contained in the record.
12 print $record->title(),"\n";

Using the distribution's 't/camel.usmarc', your result should be:

ActivePerl with ASP and ADO / Tobias Martinsson.

Iterating through a batch file

Now imagine that 'file.dat' actually contains multiple records and we want to print the title for each of them. Our program doesn't have to change very much at all: we just need to add a loop around our call to next().

1 ## Example R2
2
3 ## create a MARC::Batch object.
4 use MARC::Batch;
5 my $batch = MARC::Batch->new('USMARC','file.dat');
6
7 while (my $record = $batch->next()) {
8
9 ## print the title contained in the record.
10 print $record->title(),"\n";
11
12 }

The call to the next() method at line 7 returns the next record from the file. next() returns undef when there are no more records left in the file, which causes the while loop to end. This is a useful idiom for reading in all the records in a file. Your results with 'camel.usmarc' should be:

ActivePerl with ASP and ADO / Tobias Martinsson.
Programming the Perl DBI / Alligator Descartes and Tim Bunce.
.
.
.
Cross-platform Perl / Eric F. Johnson.

Checking for errors

It is a good idea to get in the habit of checking for errors. MARC/Perl has been designed to help you do this. Calls to next() when iterating through a batch file will return undef when there are no more records to return... AND when an error was encountered (see the next recipe to subvert this). You probably want to make sure that you didn't abruptly stop reading a batch file because of an error.

1 ## Example R3
2
3 ## create a MARC::Batch object.
4 use MARC::Batch;
5 my $batch = MARC::Batch->new('USMARC','file.dat');
6
7 ## get a marc record from the MARC::Batch object.
8 ## $record will be a MARC::Record object.
9 while ( my $record = $batch->next() ) {
10 print $record->title(),"\n";
11 }
12
13 ## make sure there weren't any problems.
14 if ( my @warnings = $batch->warnings() ) {
15 print "\nWarnings were detected!\n", @warnings;
16 }

The call to warnings()at line 14 will retrieve any warning messages and store them in @warnings. This allows you to detect when next() has aborted prematurely (before the end of the file has been reached). When a warning is detected, an explanation is sent to STDERR. By introducing an error into 'camel.usmarc', we'll receive the following output to STDOUT:

Warnings were detected!
Invalid indicators "a0" forced to blanks in record 1 for tag 245