如何在 Perl 中列出给定对象或包的可用方法?
如何在 Perl 中列出给定对象或包的可用方法?
How do I list available methods on a given object or package in Perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 Perl 中列出给定对象或包的可用方法?
How do I list available methods on a given object or package in Perl?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
在 Perl 中(相当多)有很多方法可以做到这一点,因为在 Perl 中有很多方法可以做事情。 正如有人评论的那样,自动加载的方法总是有点棘手。 但是,我建议您不要采用自己的方法,而是查看 Class::Inspector。 这会让你做类似的事情:
There are (rather too) many ways to do this in Perl because there are so many ways to do things in Perl. As someone commented, autoloaded methods will always be a bit tricky. However, rather than rolling your own approach I would suggest that you take a look at Class::Inspector on CPAN. That will let you do something like:
一个很好的答案如何获取结构和继承历史
可以使用以下命令找到对象类当前继承的类:
A good answer from How to get structure and inheritance history
The classes from which an object's class currently inherits can be found using the following:
一般来说,您无法使用像 Perl 这样的动态语言来做到这一点。 该包可能定义了一些您可以找到的方法,但它也可以动态地构建在您使用它们之前没有定义的方法。 此外,即使调用一个方法(有效)也可能无法定义它。 这就是让动态语言变得美好的原因。 :)
你想解决什么任务?
In general, you can't do this with a dynamic language like Perl. The package might define some methods that you can find, but it can also make up methods on the fly that don't have definitions until you use them. Additionally, even calling a method (that works) might not define it. That's the sort of things that make dynamic languages nice. :)
What task are you trying to solve?
如果您有一个使用 Moose 的软件包,
那么它相当简单: 对于更完整的数据:
将帮助您入门。 对于其他所有内容,
->meta
上显示的方法记录在 Class::MOP::Class您可以对没有它的包进行一些 AdHoc 伪装,使用:
,然后继续使用 Class::MOP 方法,就像使用 Moose 一样。
对于初学者:
使用 Moose; #,它太棒了。
if you have a package that is using Moose its reasonably simple:
And for more complete data:
Will get you started. For everything else, theres the methods that appear on
->meta
that are documented in Class::MOP::ClassYou can do a bit of AdHoc faking of moose goodness for packages without it with:
and then proceed to use the Class::MOP methods like you would with Moose.
For starters:
use Moose; #, its awesome.
如果您有一个名为
Foo
的包,应该这样做:或者,获取包
Foo
中所有方法的列表:If you have a package called
Foo
, this should do it:Alternatively, to get a list of all methods in package
Foo
: