Mat*_*ood 5 methods perl moose
我正在使用Moose,我需要在项目中包装方法调用.重要的是我的包装代码是最外层的修饰符.到目前为止我所做的是将我的方法修饰符放在Moose角色中,然后在我的类的末尾应用该角色,如下所示:
use Moose::Util;
Moose::Util::apply_all_roles(__PACKAGE__->meta, ('App:Roles::CustomRole'));
__PACKAGE__->meta->make_immutable;
Run Code Online (Sandbox Code Playgroud)
这让我可以合理地确定我的角色的修饰符是最后定义的,因此给了我"之前"和"之后"的正确行为.(角色中的"之前"和"之后"被称为第一个也是最后一个.)
我原本以为这就足够了,但我现在真的需要用"around"以类似的方式包装方法.构建Moose的Class :: MOP首先应用"around"修饰符,因此它们在"before"之后和"after之后"之后调用.
有关更多详细信息,请参阅我的修饰符的当前调用顺序:
CUSTOM ROLE before
before 2
before 1
CUSTOM ROLE around
around
method
around
CUSTOM ROLE around
after 1
after 2
CUSTOM ROLE AFTER
Run Code Online (Sandbox Code Playgroud)
我真的需要这样的东西:
CUSTOM ROLE before
CUSTOM ROLE around
before 2
before 1
around
method
around
after 1
after 2
CUSTOM ROLE around
CUSTOM ROLE AFTER
Run Code Online (Sandbox Code Playgroud)
关于如何在我想要的地方应用/调用我的"around"修饰符的任何想法?我知道我可以做一些符号表黑客攻击(比如Class :: MOP已经在做)但我真的不愿意.
最简单的解决方案是让CUSTOM ROLE定义一个调用main方法然后包装它的方法.
role MyRole {
required 'wrapped_method';
method custom_role_base_wrapper { $self->wrapped_method(@_) }
around custom_role_base_wrapper { ... }
before custom_role_base_wrapper { ... }
}
Run Code Online (Sandbox Code Playgroud)
您遇到的问题是,你想拥有的绕到一些自定义角色的其他比的方法.这不是它的目的.除了像你所建议的那样编写类似的符号表hackery(可能你可以争论一个Moose人在Class :: MOP中暴露API以帮助实现),我能想到的唯一其他解决方案就是上面的那个.
如果您不想要添加的额外调用堆栈帧custom_role_base_wrapper,您应该查看Yuval Sub::Call::Tail或使用goto操作调用堆栈.