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.
  • The problem of needing to localise special variables is discussed in http://perldoc.perl.org/perlobj.html#Destructors [perl.org].

    There is a P::C policy ErrorHandling::RequireCheckingReturnValueOfEval [cpan.org] that catches this programming mistake. Its documentation also offers an alternative approach where you don't need to mess with other people's destructors to make it work.

    package SomeClass;
    sub new {
        return bless \my $foo => __PACKAGE__;
    }

    sub DESTROY {
        eval {'resetting $@'};
    }

    package main;
    use

    • Did you mean to say

      if (
          eval {
              my $obj = SomeClass->new();
              die "Code Red!\n";
              1;
          }
      ) {
          print "No exception occurred\n";
      } else {
          print "Exception caught: $@\n";
      }

  • > use local($@) to restore the previous value of $@ when your routine completes. How? (Searched web but didn't find an answer)
  • Thanks. (This shows the danger of just mixing in code with plain English when you're explaining things; sometimes people don't recognize that it *is* code (even though it's obvious to others))
  • Following the eval block with an "or" seems to be more reliable than using "if ($@)" to check for an exception in an eval block, because an eval block (or even an eval EXPR) returns undef if there is a syntax error or runtime error or a die statement is executed (according to perldoc -f eval).


            eval {
                    my $obj = SomeClass->new();
                    die "Code Red!\n";