What are the common pitfalls when using Perl's eval?

后端 未结 4 746
天涯浪人
天涯浪人 2021-02-04 06:54

What are the common pitfalls associated with Perl\'s eval, which might make you choose to use a module such as Try::Tiny?

4条回答
  •  深忆病人
    2021-02-04 07:22

    In addition to the answers above, I would add...

    • eval is affected by the global $SIG{__DIE__} handler causing action at a distance.
    • It is easy for a novice to confuse eval BLOCK and eval STRING, since they appear to do the same thing, but one is a security hole.

    Try::Tiny has its own pitfalls, the biggest being that while it looks like a block it is actually a subroutine call. That means this:

    eval {
        ...blah blah...
        return $foo;
    };
    

    and this:

    try {
        ...blah blah...
        return $foo;
    };
    

    do not do the same thing. These are laid out in the CAVEATS section of the Try::Tiny docs. That said, I'd recommend it over eval.

提交回复
热议问题