列出perl程序中所有的子程序名
List all the subroutine names in perl program
我在我的 perl 程序中使用了更多模块。
示例:
use File::copy;
同样,File 模块包含 Basename、Path、stat 等。
我想列出文件包模块中的所有子程序(函数)名称。
在 python 中有 dir(modulename)
它列出了该模块中使用的所有功能....
例子:
#!/usr/bin/python
# Import built-in module math
import math
content = dir(math)
print content
喜欢python 告诉 perl 中的任何代码
请帮忙提前谢谢
如果你只想打印它使用Data::Dumper模块和以下方法,以CGI为例:
use strict;
use warnings;
use CGI;
use Data::Dumper;
my $object = CGI->new();
{
no strict 'refs';
print "Instance METHOD IS " . Dumper( \%{ref ($object)."::" }) ;
}
另请注意,它是 File::Copy,而不是 File::copy。
如果你想在perl中查看命名空间的内容,你可以使用%modulename::
。
对于 main
,这是 %main::
或 %::
。
例如:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub fish {};
sub carrot {};
print "Stuff defined in Dumper:\n";
print Dumper \%Data::Dumper::;
print "Stuff defined:\n";
print Dumper \%::;
虽然这涵盖了很多内容 - 包括 pragma。但是你可以检查例如通过简单地测试它作为代码参考来创建子程序。
foreach my $thing ( keys %:: ) {
if ( defined &$thing ) {
print "sub $thing\n";
}
}
参考上面的示例,打印出:
sub Dumper
sub carrot
sub fish
所以参考你原来的问题:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use File::Copy;
print "File::Copy has subs of:\n";
foreach my $thing ( keys %File::Copy:: ) {
if ( defined &$thing ) {
print "sub $thing\n";
}
}
不幸的是,你不能对整个 File::
命名空间做同样的事情,因为有一大堆不同的模块 可能 是 installed/loaded ,但可能不是。
你必须使用例如CPAN 检查 -
perl -MCPAN -e shell
i /^File::/
它将列出大约 717 个模块,这些模块被分组到 File::
树中。
您可以在 CPAN
上查找。或者,如果您只是追求核心模块,那么使用 Module::CoreList
的某些变体可能会满足您的需求。
像这样:
#!/usr/bin/perl
use strict;
use warnings;
use Module::CoreList;
foreach my $module ( Module::CoreList->find_modules(qr/^File::/) ) {
if ( eval { require $module =~ s|::|/|gr . ".pm" } ) {
print "Module: $module contains\n";
my $key_str = "\%$module\:\:";
my %stuff = eval $key_str;
foreach my $thing ( sort keys %stuff ) {
my $full_sub_path = "$module::$thing";
if ( eval {"defined &$full_sub_path"} ) {
if ( defined &$thing ) {
print "$thing <- $full_sub_path imported by default\n";
}
else {
print "\t$full_sub_path might be loadable\n";
}
}
}
}
else {
print "Module: $module couldn't be loaded\n";
}
}
它有点乱,因为您必须 eval
它的各个部分来测试模块是否确实存在并且可以在运行时加载。奇怪的是,我的 Win32
系统上没有 File::Spec::VMS
。想不出为什么.... :).
应该注意 - 仅仅因为您 可以 从模块(默认情况下不导出)导入子模块并不是一个好主意。按照惯例,任何以 _
为前缀的 sub 都不应该在外部使用,等等
我的Devel::Examine::Subs module can do this, plus much more. Note that whether it's a method or function is irrelevant, it'll catch both. It works purely on subroutines as found with PPI。
use warnings;
use strict;
use Devel::Examine::Subs;
my $des = Devel::Examine::Subs->new;
my $subs = $des->module(module => 'File::Copy');
for (@$subs){
print "$_\n";
}
输出:
_move
move
syscopy
carp
mv
_eq
_catname
cp
copy
croak
或 file/full 目录。对于目录中的所有 Perl 文件(递归),只需将 dir 传递给 file
参数,路径末尾没有文件:
my $des = Devel::Examine::Subs->new(file => '/path/to/file.pm');
my $subs = $des->all;
我在我的 perl 程序中使用了更多模块。 示例:
use File::copy;
同样,File 模块包含 Basename、Path、stat 等。 我想列出文件包模块中的所有子程序(函数)名称。
在 python 中有 dir(modulename) 它列出了该模块中使用的所有功能.... 例子: #!/usr/bin/python
# Import built-in module math
import math
content = dir(math)
print content
喜欢python 告诉 perl 中的任何代码
请帮忙提前谢谢
如果你只想打印它使用Data::Dumper模块和以下方法,以CGI为例:
use strict;
use warnings;
use CGI;
use Data::Dumper;
my $object = CGI->new();
{
no strict 'refs';
print "Instance METHOD IS " . Dumper( \%{ref ($object)."::" }) ;
}
另请注意,它是 File::Copy,而不是 File::copy。
如果你想在perl中查看命名空间的内容,你可以使用%modulename::
。
对于 main
,这是 %main::
或 %::
。
例如:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
sub fish {};
sub carrot {};
print "Stuff defined in Dumper:\n";
print Dumper \%Data::Dumper::;
print "Stuff defined:\n";
print Dumper \%::;
虽然这涵盖了很多内容 - 包括 pragma。但是你可以检查例如通过简单地测试它作为代码参考来创建子程序。
foreach my $thing ( keys %:: ) {
if ( defined &$thing ) {
print "sub $thing\n";
}
}
参考上面的示例,打印出:
sub Dumper
sub carrot
sub fish
所以参考你原来的问题:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use File::Copy;
print "File::Copy has subs of:\n";
foreach my $thing ( keys %File::Copy:: ) {
if ( defined &$thing ) {
print "sub $thing\n";
}
}
不幸的是,你不能对整个 File::
命名空间做同样的事情,因为有一大堆不同的模块 可能 是 installed/loaded ,但可能不是。
你必须使用例如CPAN 检查 -
perl -MCPAN -e shell
i /^File::/
它将列出大约 717 个模块,这些模块被分组到 File::
树中。
您可以在 CPAN
上查找。或者,如果您只是追求核心模块,那么使用 Module::CoreList
的某些变体可能会满足您的需求。
像这样:
#!/usr/bin/perl
use strict;
use warnings;
use Module::CoreList;
foreach my $module ( Module::CoreList->find_modules(qr/^File::/) ) {
if ( eval { require $module =~ s|::|/|gr . ".pm" } ) {
print "Module: $module contains\n";
my $key_str = "\%$module\:\:";
my %stuff = eval $key_str;
foreach my $thing ( sort keys %stuff ) {
my $full_sub_path = "$module::$thing";
if ( eval {"defined &$full_sub_path"} ) {
if ( defined &$thing ) {
print "$thing <- $full_sub_path imported by default\n";
}
else {
print "\t$full_sub_path might be loadable\n";
}
}
}
}
else {
print "Module: $module couldn't be loaded\n";
}
}
它有点乱,因为您必须 eval
它的各个部分来测试模块是否确实存在并且可以在运行时加载。奇怪的是,我的 Win32
系统上没有 File::Spec::VMS
。想不出为什么.... :).
应该注意 - 仅仅因为您 可以 从模块(默认情况下不导出)导入子模块并不是一个好主意。按照惯例,任何以 _
为前缀的 sub 都不应该在外部使用,等等
我的Devel::Examine::Subs module can do this, plus much more. Note that whether it's a method or function is irrelevant, it'll catch both. It works purely on subroutines as found with PPI。
use warnings;
use strict;
use Devel::Examine::Subs;
my $des = Devel::Examine::Subs->new;
my $subs = $des->module(module => 'File::Copy');
for (@$subs){
print "$_\n";
}
输出:
_move
move
syscopy
carp
mv
_eq
_catname
cp
copy
croak
或 file/full 目录。对于目录中的所有 Perl 文件(递归),只需将 dir 传递给 file
参数,路径末尾没有文件:
my $des = Devel::Examine::Subs->new(file => '/path/to/file.pm');
my $subs = $des->all;