Stories
Slash Boxes
Comments
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Yes, this is a well-known problem with eval. You cannot, as the manual page suggests, do eval { ... } and then test $@ to see whether there was an error. The correct incantation is something like

    my $r = eval { ... };
    my $error = $@;
    $error //= 'unknown error' if not $r;
    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
    --
    -- Ed Avis ed@membled.com
    • 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
      • For prevention, you just have to run perlcritic over your code. If you're calling other people's code that isn't perlcritic-clean, then 'local $@' in every DESTROY method might save you. I suppose there should be a perlcritic policy for that too.

        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