This code smells… how do I rewrite it better?
my $record;
eval {
while (
# undef $record here, so if getRecord() failed, nothing will be written
# in the reject file
do { undef $record; defined( $record = $dataFile->getRecord ) }
) {
$LT_DataFile->encode($record);
}
1;
};
if ( my $error = $@ ) {
$rejectFile->writeRecord( $error, $record );
}
Thanks.
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:
This way, you’re explicit in how you handle your errors. Of course, with Try::Tiny, this simplifies into the following
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
lastcall:Unfortunately this method won’t work with Try::Tiny, because the blocks passed to try are actually subrefs.