NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.
All the Perl that's Practical to Extract and Report
Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.
It's a well-known problem (Score:1)
my $r = eval {
my $error = $@;
$error
if ($error) {
By well-known I mean known to a tiny minority of cognoscenti, while most perl programmers and the documentation are entirely ignorant of it. Which is the default situation for any trap or gotcha in the perl language. But I digress...
The Error module on CPAN provides syntactic sugar 'try', 'catch', and 'except'. It handles the case of eval {} in DESTROY half-correctly: an exception is thrown, but the original message is lost. See <http://rt.cpan.org/Ticket/Display.html?id=38836>.
The good news is that if you use Error everywhere and have try {} in your destructors instead of eval {}, it appears to work correctly.
-- Ed Avis ed@membled.com
Reply to This
Re: (Score:1)
my $r = eval {
my $error = $@;
$error
if ($error) {
Note the trailing '1' in the eval block.
(It doesn't help that the comment engine here is doing something weird so you lose all your text if you go Back and Forward in Firefox. And those submit buttons are not buttons at all but look like links. What's going on?)
-- Ed Avis ed@membled.com
Re: (Score:1)
Yeah, I generally do that, and definitely suggest that anyone else do it, too. What I was wondering about, though, is prevention. People are going to *forget* to do that, and if my destructors localize $@, they won't clobber it. Where else is there clobbering potential?
I'm not even sure the destructor in quesiton did call eval. I guess maybe somewhere down *its* calls. Blech!
rjbs
Re: (Score:1)
I don't like the way that the perl core has all sorts of traps for the unwary and can seemingly never be fixed for fear of breaking backwards compatibility, but perlcritic provides a useful sticking plaster.
-- Ed Avis ed@membled.com