Moose 角色修饰函数的失败行为

发布于 2024-12-22 22:36:11 字数 463 浏览 1 评论 0原文

给出下面的代码示例,当运行 Main::main 时,如果 eval 块失败,我会假设角色中的“after”块永远不会运行。然而,我正在调试一个间歇性且难以确定的错误,这表明情况并非如此。

有人可以解释一下“扁平化”过程在这种情况下是如何工作的,以及是否存在 eval 失败但 after 块会运行的任何条件?

谢谢 乔什

package MyRole;
use Moose::Role;

after 'main' => sub {
    #some code that depends on main executing
};

package Main;
use Moose;
with 'MyRole';

sub main {
    eval {
        #main work
    };
    if ($@) {
        die 'what happens now?';
    }
}

Given the below code example, when running Main::main, if the eval block fails, I would assume that the 'after' block in the role would never run. However, I'm in the midst of debugging an intermittent and hard-to-pin-down error that would suggest this is not the case.

Could someone explain how the 'flattening' process works in this context, and if there are any conditions in which the eval would fail but the after block would run?

Thanks
Josh

package MyRole;
use Moose::Role;

after 'main' => sub {
    #some code that depends on main executing
};

package Main;
use Moose;
with 'MyRole';

sub main {
    eval {
        #main work
    };
    if ($@) {
        die 'what happens now?';
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏闹i 2024-12-29 22:36:11

如果 main 死掉,那么 after 修饰符将不会运行。但是,eval 可能会失败,但 $@ 为 false。听起来这一定是这里发生的事情。如果 eval 失败,但 $@ 为 false,则错误处理代码将不会运行,并且 main 也不会引发异常。因此,后面的main代码将被运行。

这是 Try::Tiny 为您提供保护的事情之一。

If main dies, then the after modifier will not run. However, it is possible for an eval to fail but $@ to be false. It sounds like that must be what's happening here. If the eval fails, but $@ is false, your error handling code won't run and main won't throw an exception. Thus, the after main code will be run.

This is one of the things Try::Tiny protects you against.

最笨的告白 2024-12-29 22:36:11
  • MyRole::main() 通常会在 Main::main() 之后执行。

  • 它们并没有完全扁平化为一个接一个的方法,因为您在 Main::main() 中返回的内容仍然是返回的内容,并且您在 MyRole::main() 中返回的任何内容都将被忽略。

  • 如果“main work”失败,MyRole::main() 将不会执行,因为您也会死在 catch (if $@) 块中,这意味着执行流程在它之前就死掉了到达 MyRole::main()。

  • MyRole::main() would normally execute after Main::main().

  • They are not exactly flattened into one method with one after the other in that what you return in Main::main() is still what gets returned and anything you return in MyRole::main() is ignored.

  • If 'main work' fails, MyRole::main() will not execute since you also die in the catch (if $@) block, which means that the flow of execution dies before it reaches MyRole::main().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文