Why is `$@` untrustworthy?

后端 未结 3 1510
悲&欢浪女
悲&欢浪女 2021-01-02 02:27

I seem to recall that it is not safe to trust the value of $@ after an eval. Something about a signal handler having a chance to set $@

相关标签:
3条回答
  • 2021-01-02 03:17

    The Try::Tiny docs have a pretty good list of eval/$@ shortcomings. I think you might be refering to the Localizing $@ silently masks errors section in there.

    0 讨论(0)
  • 2021-01-02 03:24

    $@ has the same problems that every global variable has: when something else sets it, it's reset across the entire program. Any eval might set $@. Even if you don't see an eval nearby, you don't know who else might call one (subroutines, tied variables, and so on).

    0 讨论(0)
  • 2021-01-02 03:27

    The Try::Tiny perldoc has the definitive discussion of the trouble with $@:

    There are a number of issues with eval.

    Clobbering $@

    When you run an eval block and it succeeds, $@ will be cleared, potentially clobbering an error that is currently being caught.

    This causes action at a distance, clearing previous errors your caller may have not yet handled.

    $@ must be properly localized before invoking eval in order to avoid this issue.

    More specifically, $@ is clobbered at the beginning of the eval, which also makes it impossible to capture the previous error before you die (for instance when making exception objects with error stacks).

    For this reason try will actually set $@ to its previous value (before the localization) in the beginning of the eval block.

    Localizing $@ silently masks errors

    Inside an eval block die behaves sort of like:

    sub die {
            $@ = $_[0];
            return_undef_from_eval();
    }
    

    This means that if you were polite and localized $@ you can't die in that scope, or your error will be discarded (printing "Something's wrong" instead).

    The workaround is very ugly:

    my $error = do {
            local $@;
            eval { ... };
            $@;
    };
    
    ...
    die $error;
    

    $@ might not be a true value

    This code is wrong:

    if ( $@ ) {
            ...
    }
    

    because due to the previous caveats it may have been unset.

    $@ could also be an overloaded error object that evaluates to false, but that's asking for trouble anyway.

    The classic failure mode is:

    sub Object::DESTROY {
            eval { ... }
    }
    
    eval {
            my $obj = Object->new;
    
            die "foo";
    };
    
    if ( $@ ) {
    
    }
    

    In this case since Object::DESTROY is not localizing $@ but still uses eval, it will set $@ to "".

    The destructor is called when the stack is unwound, after die sets $@ to "foo at Foo.pm line 42\n", so by the time if ( $@ ) is evaluated it has been cleared by eval in the destructor.

    The workaround for this is even uglier than the previous ones. Even though we can't save the value of $@ from code that doesn't localize, we can at least be sure the eval was aborted due to an error:

    my $failed = not eval {
            ...
    
            return 1;
    };
    

    This is because an eval that caught a die will always return a false value.

    0 讨论(0)
提交回复
热议问题