Ok, reworked my answer.
I think the REAL problem here is how you handle the errors. It's confusing at first glance to see a single error handler when you have multiple places where things could go wrong. I see two alternatives.
First, keep it mostly the same as you have now, but specifically check for each type of error:
my $record;
eval {
while (defined( $record = $dataFile->getRecord )) {
$LT_DataFile->encode($record);
}
};
if (my $error = $@) {
given ($error) {
when (/get record error/) { $rejectFile->writeRecord($_, undef); }
when (/encode error/) { $rejectFile->writeRecord($_, $record); }
}
}
This way, you're explicit in how you handle your errors. Of course, with Try::Tiny, this simplifies into the following
my $record;
try {
while (defined( $record = $dataFile->getRecord )) {
$LT_DataFile->encode($record);
}
} catch {
when (/get record error/) { $rejectFile->writeRecord($_, undef); }
when (/encode error/) { $rejectFile->writeRecord($_, $record); }
}
Alternatively, you could add the lexical record per Daxim's answer. This requires a second eval or try, closer to the problem and adding a last
call:
eval {
while (defined( my $record = $dataFile->getRecord )) {
eval { $LT_DataFile->encode($record) };
if (my $error = $@) { $rejectFile->writeRecord($error, $record); last }
}
};
if (my $error = $@) {
$rejectFile->writeRecord($error, undef);
}
Unfortunately this method won't work with Try::Tiny, because the blocks passed to try are actually subrefs.