Rethrowing an exception with die $@ if (some expression involving $@) (an idiom mentioned at least 6 times in the perl documentation) has a problem when the expression involving $@ ends up doing anything that inadvertently clears $@. Consider:
Here, we die with an exception object that should stringify to "whoops", but $@ ends up being cleared before the rethrow. (To highlight the maybe-expected vs. observed results, change eval { "whoops" } to just "whoops" or remove the if $@.) The better way to do it would beuse overload q!""!=>sub{ eval { "whoops" } }; eval { die bless {} }; die $@ if $@;
use overload q!""!=>sub{ eval { "whoops" } }; eval { die bless {} }; my $exception = $@; die $exception if $exception;
Back to
Meditations