搜索安装的perl模块

上一篇 / 下一篇  2009-01-06 21:56:30 / 个人分类:点滴积累

    今天下午看Perl Hacks时发现在VIM中编辑perl脚本输入模块名时不用输入全名称,可以通过提供资源文件的方式,使VIM自动联想补全名称,嘿嘿,又可以偷懒了。

 

    共有2种方式来获取本机已经安装的Perl模块:

1、CMD窗口方式

    Google了一下,在CMD窗口中输入perldoc perllocal可以通过输出$PERL(Perl默认库的路径)中perllocal.pod文件内容的方式来查看已经安装的模块。可是很奇怪,查了一下,只有区区的42个module,这也太少了吧。再次Google一下perllocal.pod,发现不少人也有同样的疑问,原来只有通过命令行的cpan->install方式安装的CPAN模块才会记录在该文件中;而通过ppm安装的CPAN模块是不会记录在该文件中的。原来是这样,好怪异=。=

 

2、Perl脚本

    自己动手写了个脚本用来搜索安装在Perl默认库路径下的模块名并计算总数,一看结果,1054个module(包括built-in的module),嘿嘿,这才是对的撒。

use strict;
use warnings;
use File::Find qw(find);
use File::Spec;

my $modules;            #保存模块名
my $total_modules = 0;  #记录模块总数
my $path;
for (@INC) {
    $path = $_;
    find (
        \&wanted, $path
    );
}

sub wanted {
    if ($File::Find::name =~ m/^\./) {}                         #当前目录则不处理
    elsif ($File::Find::name =~ /\.pm$/) {                      #后缀为.pm的为模块
         substr ($File::Find::name, 0, length($path)+1) = '';   #去掉模块绝对路径前的lib path部分
         $File::Find::name =~ s{/}{::}g;                                         #把剩下路径的分隔符换成::
         $File::Find::name =~ s{.pm$}{};                        #去掉后缀名.pm
         $modules .= "$File::Find::name\n";
         $total_modules++;
    }
}

#写入当前目录下的modulesx.txt文件中
my $modules_file_name = File::Spec->curdir();
$modules_file_name = File::Spec->catfile($modules_file_name, "modules.txt");
open my $modules_file_handle, ">", "$modules_file_name"
    or die "Can not open file: $modules_file_name\n$!";
print $modules_file_handle "Total modules is $total_modules.\n$modules";
close $modules_file_handle;

 

 

PS:编码的时候发现@INC中包含当前目录“.”,又是一个诡异的事情,查了一下perldoc,看到了这么一句The array @INC contains the list of places that the do EXPR, require, or use
constructs look for their library files. ......followed by the default Perl library,followed by ".", to represent the current
 directory.("." will not be appended if taintchecks are enabled, either by -T or by -t.)。原来@INC的目的是用来查找执行do、require、use的路径,难怪除了perl default library以外,还包括当前目录,括号里头又说了可以启用taint check来使@INC中不包括“.”,Google之,貌似在脚本中无法控制taint check,只有在命令行中通过输入 perl -t test.pl的方式来启用taint check。


TAG: 点滴积累

 

评分:0

我来说两句

日历

« 2024-05-10  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 6096
  • 日志数: 12
  • 建立时间: 2008-12-22
  • 更新时间: 2009-01-11

RSS订阅

Open Toolbar