【发布时间】:2014-08-13 18:58:11
【问题描述】:
下面的代码将找到 Perl 模块的路径并将其存储在 @files 数组中。 我尝试了从 perl 模块路径中删除 @INC 路径的正则表达式,但它没有用。 请告诉我从给定的模块路径中获取 Perl 模块名称的更好方法。
#Get locally installed Perl module path
my @files = ();
find({
wanted => sub {
push @files, $File::Find::fullname
if defined $File::Find::fullname && -f $File::Find::fullname && /\.pm$/
},
follow => 1,
follow_skip => 2,
}, @INC);
# Make the hash with Perl Module Path and Perl Module Name
my ($file_path, $name) = ("", "");
my %modules = ();
foreach $file_path (@files) { #loop through all the perl module (.pms) found
for (@INC) {#replace the path with scope resolution operator
if ($_ !~ /^\.$/) {
if ($file_path =~ /$_/) {
($name = $file_path) =~ s/$_//g;
$name =~ s/\//::/g;
$name =~ s/\.pm$//g;
$modules{$file_path} = $name;
}
}
}
}
【问题讨论】:
标签: perl