您如何内省 MooseX::Method::Signatures 方法以了解它们采用哪些参数?

发布于 2024-12-16 02:25:25 字数 389 浏览 2 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(1

机场等船 2024-12-23 02:25:25

首先,获取方法元对象:

my $method = $class->meta->find_method_by_name( $method_name );

然后,确保它是签名方法:

confess "not method with a signature!"
  unless $method->isa('MooseX::Method::Signatures::Meta::Method');

获取其签名:

my $sig = $method->parsed_signature;

然后查看 $signamed_pa​​ramspositional_params 如 中详述解析::方法::签名::Sig 文档

要找到 parsed_signature,我必须查看 MooseX::Method::Signatures::Meta::Method 的来源...所以在执行此操作时要小心。

First, get the method meta object:

my $method = $class->meta->find_method_by_name( $method_name );

Then, make sure it's a signaturey method:

confess "not method with a signature!"
  unless $method->isa('MooseX::Method::Signatures::Meta::Method');

Get its signature:

my $sig = $method->parsed_signature;

Then look at $sig's named_params and positional_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.

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