您如何内省 MooseX::Method::Signatures 方法以了解它们采用哪些参数?
我正在使用 MooseX::Declare 和方法,它使用 MooseX::Method::Signatures。假设我有一个带有方法“bar”的类“foo”,我的实现如下:
class foo {
method bar (Str $str, Bool :$flag = 1) {
# ....
}
}
我现在想编写一个前端接口,询问用户他们想要使用什么类,该类上的什么方法他们想要使用什么,然后选择他们想要的方法。我可以做前两件事,所以假设用户现在选择了类 foo 和方法 bar。
但是我如何发现方法 bar 将字符串作为第一个参数,并且标志 =>默认为 1 的 bool 键值对?我的代码需要知道这一点,以便我可以要求用户提供这些东西。
I'm using MooseX::Declare and methods, which uses MooseX::Method::Signatures. Let's say I have a class 'foo' with a method 'bar', and I've implemented it like:
class foo {
method bar (Str $str, Bool :$flag = 1) {
# ....
}
}
I now want to write a front-end interface that asks a user what class they want to use, what method on that class they want to use, and then what options to the method they want. I can do the first two things, so let's say the user has now chosen class foo and method bar.
But how do I find out that method bar takes a string as the first argument, and a flag => bool key value pair that defaults to 1? My code needs to know this so I can then ask the user to supply these things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,获取方法元对象:
然后,确保它是签名方法:
获取其签名:
然后查看
$sig
的named_params
和positional_params 如 中详述解析::方法::签名::Sig 文档。
要找到
parsed_signature
,我必须查看 MooseX::Method::Signatures::Meta::Method 的来源...所以在执行此操作时要小心。First, get the method meta object:
Then, make sure it's a signaturey method:
Get its signature:
Then look at
$sig
'snamed_params
andpositional_params
as detailed in the Parse::Method::Signatures::Sig docs.To find
parsed_signature
, I had to look at the source to MooseX::Method::Signatures::Meta::Method… so be wary when you do this.