Moose 角色修饰函数的失败行为
给出下面的代码示例,当运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
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 aneval
to fail but$@
to be false. It sounds like that must be what's happening here. If theeval
fails, but$@
is false, your error handling code won't run andmain
won't throw an exception. Thus, the aftermain
code will be run.This is one of the things Try::Tiny protects you against.
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().