I'm working on the "Error Handling and Reporting" chapter of Mastering Perl. In perlvar's entry for $@
, it says:
The Perl syntax error message from the last eval() operator. If $@ is the null string, the last eval() parsed and executed correctly (although the operations you invoked may have failed in the normal fashion).
Now I'm wondering when an eval might not execute correctly, leaving $@
with an undefined value. Are there any such cases?
Here's one way to do it (but sit down before you read it. ;))
$@ = 123;
eval q{
$@ = 456;
print ">>>$@<<<\n";
goto SKIP;
};
SKIP:
print ">>>$@<<<\n";
The BACKGROUND section of the Try::Tiny docs contain some info on how $@
can be clobbered and why Try::Tiny takes special care to avoid clobberage, and always tests the return value of eval
rather than testing $@
for truth. All of them are in there because someone ran into them at some point, but I think that the eval
-in-DESTROY
scenario is the one most likely to trip someone up. Basically, if die
causes some object to go out of scope, and that object has a DESTROY
that calls eval
, then the value that you die
d with to begin with is irretrievably lost. Assuming the eval
in the DESTROY
doesn't throw an error, $@
will be ""
following the outer eval
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With