如何在 Perl 中列出给定对象或包的可用方法?

发布于 2024-07-22 13:58:34 字数 31 浏览 5 评论 0原文

如何在 Perl 中列出给定对象或包的可用方法?

How do I list available methods on a given object or package in Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

ま柒月 2024-07-29 13:58:34

在 Perl 中(相当多)有很多方法可以做到这一点,因为在 Perl 中有很多方法可以做事情。 正如有人评论的那样,自动加载的方法总是有点棘手。 但是,我建议您不要采用自己的方法,而是查看 Class::Inspector。 这会让你做类似的事情:

my $methods =   Class::Inspector->methods( 'Foo::Class', 'full', 'public' );

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:

my $methods =   Class::Inspector->methods( 'Foo::Class', 'full', 'public' );
跨年 2024-07-29 13:58:34

一个很好的答案如何获取结构和继承历史
可以使用以下命令找到对象类当前继承的类:

use mro          qw( );
use Scalar::Util qw( blessed );
say join ", ", @{ mro::get_linear_isa(blessed($o)) };

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:

use mro          qw( );
use Scalar::Util qw( blessed );
say join ", ", @{ mro::get_linear_isa(blessed($o)) };
失与倦" 2024-07-29 13:58:34

一般来说,您无法使用像 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?

说不完的你爱 2024-07-29 13:58:34

如果您有一个使用 Moose 的软件包,

print PackageNameHere->meta->dump;

那么它相当简单: 对于更完整的数据:

use Data::Dumper;
print Dumper( PackageNameHere->meta ); 

将帮助您入门。 对于其他所有内容,->meta 上显示的方法记录在 Class::MOP::Class

您可以对没有它的包进行一些 AdHoc 伪装,使用:

use Class::MOP::Class;
my $meta = Class::MOP::Class->initialize( PackageNameHere );

,然后继续使用 Class::MOP 方法,就像使用 Moose 一样。

对于初学者:

 $meta->get_method_map(); 

使用 Moose; #,它太棒了

if you have a package that is using Moose its reasonably simple:

print PackageNameHere->meta->dump;

And for more complete data:

use Data::Dumper;
print Dumper( PackageNameHere->meta ); 

Will get you started. For everything else, theres the methods that appear on ->meta that are documented in Class::MOP::Class

You can do a bit of AdHoc faking of moose goodness for packages without it with:

use Class::MOP::Class;
my $meta = Class::MOP::Class->initialize( PackageNameHere );

and then proceed to use the Class::MOP methods like you would with Moose.

For starters:

 $meta->get_method_map(); 

use Moose; #, its awesome.

末骤雨初歇 2024-07-29 13:58:34

如果您有一个名为 Foo 的包,应该这样做:

no strict 'refs';
for(keys %Foo::) { # All the symbols in Foo's symbol table
  print "$_\n" if exists &{"Foo::$_"}; # check if symbol is method
}
use strict 'refs';

或者,获取包 Foo 中所有方法的列表:

no strict 'refs';
my @methods = grep { defined &{"Foo::$_"} } keys %Foo::;
use strict 'refs';

If you have a package called Foo, this should do it:

no strict 'refs';
for(keys %Foo::) { # All the symbols in Foo's symbol table
  print "$_\n" if exists &{"Foo::$_"}; # check if symbol is method
}
use strict 'refs';

Alternatively, to get a list of all methods in package Foo:

no strict 'refs';
my @methods = grep { defined &{"Foo::$_"} } keys %Foo::;
use strict 'refs';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文