MARC/Perl
MARC Tutorial
Recovering from errors
You may want to keep reading a batch file even after an error has been encountered. If so, you will want to turn strict mode off using the strict_off() method. You can also prevent warnings from being printed to STDERRusing the warnings_off() method. By default, strict is on as a safety precaution to prevent you from using corrupt MARC data. Once off, you can turn both strict and warnings back on again with the strict_on()and warnings_on()methods.
| 1 | ## Example R4 |
|---|---|
| 2 | |
| 3 | use MARC::Batch; |
| 4 | my $batch = MARC::Batch->new('USMARC', 'file.dat'); |
| 5 | $batch->strict_off(); |
| 6 | |
| 7 | while ( my $record = $batch->next() ) { |
| 8 | print $record->title(),"\n"; |
| 9 | } |
| 10 | |
| 11 | ## make sure there weren't any problems. |
| 12 | if ( my @warnings = $batch->warnings() ) { |
| 13 | print "\nWarnings were detected!\n", @warnings; |
| 14 | } |
Introducing a second error to the 'camel.usmarc' file gives the following:
ActivePerl with ASP and ADO / Tobias Martinsson.
Programming the Perl DBI / Alligator Descartes and Tim Bunce.
.
.
.
Cross-platform Perl / Eric F. Johnson.
Warnings were detected!
Invalid indicators "a0" forced to blanks in record 1 for tag
245
Invalid indicators "a0" forced to blanks in record 5 for tag
245
Looking at a field
Our previous examples use MARC::Record's title() method to easily access the 245 field, but you will probably want programs that access lots of other MARC fields. MARC::Record's field() method gives you complete access to the data found in any MARC field. The field() method returns a MARC::Field object which can be used to access the data, indicators, and even the individual subfields. Our next example shows how this is done.
| 1 | ## Example R5 |
|---|---|
| 2 | |
| 3 | ## open a file. |
| 4 | use MARC::Batch; |
| 5 | my $batch = MARC::Batch->new('USMARC','file.dat'); |
| 6 | |
| 7 | ## read a record. |
| 8 | my $record = $batch->next(); |
| 9 | |
| 10 | ## get the 100 field as a MARC::Field object. |
| 11 | my $field = $record->field('100'); |
| 12 | print "The 100 field contains: ",$field->as_string(),"\n"; |
| 13 | print "The 1st indicator is ",$field->indicator(1),"\n"; |
| 14 | print "The 2nd indicator is ",$field->indicator(2),"\n"; |
| 15 | print "Subfield d contains: ",$field->subfield('d'),"\n"; |
Which results in something like:
The 100 field contains: Martinsson, Tobias, 1976-
The 1st indicator is 1
The 2nd indicator is
Subfield d contains: 1976-
As before, use a whileloop to iterate through all the records in a batch.
