Subject: | MARC::Batch::next() return values |
MARC::Batch::next() returns UNDEF when there is no more data to read from the batch file. Unfortunately, it also returns UNDEF when an error was encountered while reading a record from a batch. As a consequence a loop like this:
while ( $record = $batch->next() ) {
print $record->title();
}
will terminate after an invalid record is found, even if there are more valid records which follow it.
Two suggestions:
1. Perhaps we need a new MARC::Batch method which tells whether there is more data to read from the batch?
while ( $batch->not_empty() ) {
$record = $batch->next();
print $record->title();
}
2. Or maybe next should return UNDEF when there is no more data to read and
0 when an error was encountered.
while ( defined( $record = $batch->next() ) ) {
next if $record == 0;
print $record->title();
}
#1 is probably better since it wouldn't require any legacy code to change,
wouldn't require people to know about the subtle 0/UNDEF return values, and would probably lead to less future programming errors. People could continue programming the way they want to, but could use not_empty() if they want to catch bad records without aborting on the whole batch file.
//Ed