Taken from Perl::Critic
:
A common idiom in perl for dealing with possible errors is to use eval
followed by a check of $@
/$EVAL_ERROR
:
eval {
...
};
if ($EVAL_ERROR) {
...
}
There's a problem with this: the value of $EVAL_ERROR
($@
) can change
between the end of the eval
and the if
statement. The issue are object
destructors:
package Foo;
...
sub DESTROY {
...
eval { ... };
...
}
package main;
eval {
my $foo = Foo->new();
...
};
if ($EVAL_ERROR) {
...
}
Assuming there are no other references to $foo
created, when the
eval
block in main
is exited, Foo::DESTROY()
will be invoked,
regardless of whether the eval
finished normally or not. If the eval
in main
fails, but the eval
in Foo::DESTROY()
succeeds, then
$EVAL_ERROR
will be empty by the time that the if
is executed.
Additional issues arise if you depend upon the exact contents of
$EVAL_ERROR
and both eval
s fail, because the messages from both will
be concatenated.
Even if there isn't an eval
directly in the DESTROY()
method code,
it may invoke code that does use eval
or otherwise affects
$EVAL_ERROR
.
The solution is to ensure that, upon normal exit, an eval
returns a
true value and to test that value:
# Constructors are no problem.
my $object = eval { Class->new() };
# To cover the possiblity that an operation may correctly return a
# false value, end the block with "1":
if ( eval { something(); 1 } ) {
...
}
eval {
...
1;
}
or do {
# Error handling here
};
Unfortunately, you can't use the defined
function to test the result;
eval
returns an empty string on failure.
Various modules have been written to take some of the pain out of
properly localizing and checking $@
/$EVAL_ERROR
. For example:
use Try::Tiny;
try {
...
} catch {
# Error handling here;
# The exception is in $_/$ARG, not $@/$EVAL_ERROR.
}; # Note semicolon.
"But we don't use DESTROY() anywhere in our code!" you say. That may be the case, but do any of the third-party modules you use have them? What about any you may use in the future or updated versions of the ones you already use?
CQZQE32VR5JFHR2AQZVPXCGBHLJCLIZ3IX6NA3SWO2R44XVBSAGAC
MIQH5W3F2WJKYDFYAFVI7OUXWZOPYXSO7HCGCHRA2MBTV7POCR6QC
HEZQ273SDMUIQFI3YWPDNSSIOYRFT3KWPOKCDCBYATBMGPLU4F6AC
4ZCUCACYWDLCZUKIH2OLFC4NCPBE2YO7Y7AZ7WL3AESZQ3TSIYXQC
L54KA7IMVLHLHUED7I2K5AGSDR3EHFLMR7H332TCOTZFPV72AUWQC
XCXMK24UK6CIYCJITRERAX3IBAH5EW22SLWD2JPHVWBOSFVTGOQQC
NYTT5WP3XNTWKGTKYLGRHOP7AS7SMH6FIOWHP3JELZC3HJZTRCMAC
OOQ2D3KCLFPYNAN253PHWLBQMB6OMO2KYQWQXLTP65SQAYZWQ5LAC
6WRGCITDYP7JIBYP25QIWCHWRJWFPDP2D3TJS3WO3KUHQQJAWHMQC
JAH3UPWAVSHXIPNGL6PROQPZBYZHPJNFONWBDZX4HCX646USZXUQC
2GK5DOU7ODF4WBSN3QTD3WIO52VTL2LOAXKGCDEMMAQPTEO4A4HAC
3BTJRSU3VWCTVRL7Y2SARN2BZMECUIIVFAJPCPBRIRYCGLXHCBJQC
32KJOERMPFWZZZCIN6TGGVX72GMAJT6VCCIP7S65EHNF3KM42KWAC
IE2PRAQUCQVFPJ4CAIJRPXXEFC5VBAE3EO5I5FG4XWEDRNONNHKQC
NTEDD7T4M7XXYCWNZN57EJOEH3KI3UYMHOANV4MY2LSUPWX3JRDQC
DODOGD7MRCMRVGX23RPY2243WG54HCMCEZ6DCHOGID3GBQB66CXQC
LE4VZIY5VZ52FOP5QQRIJINWIMWTAPRTZTGO77JXUEPGRPRSQYMAC
if ($@) {
die unless $@ eq "timeout\n"; # propagate unexpected errors
return (-1, $stdout, ($stderr // "") . "timeout\n");
} else {
return ($?, $stdout, $stderr);
}
return ($?, $stdout, $stderr);