代码之家  ›  专栏  ›  技术社区  ›  Nifle Hassan Syed

如何找到Perl脚本中使用的所有模块并安装它们?

  •  6
  • Nifle Hassan Syed  · 技术社区  · 17 年前

    我得到了一些要部署的Perl脚本。

    查找和安装这些脚本使用的所有模块的最简单方法是什么?

    编辑:

    据我所知,evals中没有条件include或include。

    4 回复  |  直到 17 年前
        1
  •  12
  •   brian d foy    9 年前

    Module::Extract::Use 帮助有一个 extract_modules program 在 例子 目录:

    $ examples/extract_modules -l some_program
     File::Spec
     File::Spec::Functions
     strict
     warning
    

    您可以将该列表传送到 cpan .

    $ examples/extract_modules -l some_program | xargs cpan
    

    一旦您有了该列表(这只是依赖项的第一级),您就可以创建一个 script distribution 这允许人们使用普通的CPAN工具链来安装所有东西。

    如果有不适合你的东西,修改程序来处理。如果您认为它对其他人有用,请发送拉取请求。:)

        2
  •  3
  •   Sinan Ünür    17 年前

    我希望 Module::ScanDeps 它提供了命令行实用程序 scandeps.pl Module::ScanDeps scandeps.pl 忽略缺少的模块或(使用 -c -x )脚本使用未安装的模块时会发出咯咯声。

    下面是一个快速的“n”脏Perl脚本,它尝试使用 do

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Term::Prompt;
    
    my ($script) = @ARGV;
    
    die "Provide script file name on the command line\n"
        unless defined $script;
    
    until ( do $script ) {
        my $ex = $@;
        if ( my ($file) = $ex =~ /^Can't locate (.+?) in/ ) {
            my $module = $file;
            $module =~ s/\.(\w+)$//;
            $module = join('::', split '/', $module);
            print "Attempting to install '$module' via cpan\n";
            system(cpan => $module);
            last unless prompt(y => 'Try Again?', '', 'n');
        }
        else {
            die $ex;
        }
    }
    

    如果不希望运行脚本,可以运行 perl -c $script 俘虏 stderr 输出并解析丢失的模块消息和调用 cpan 对于找到的每个此类模块,直到 perl-c$脚本

    您可能会错过使用此技术在运行时加载的依赖项。

        3
  •  2
  •   Peter Mortensen Pieter Jan Bonestroo    16 年前

    这是 我解决了这个问题。

    在bash shell中:

    cat *.pl  | grep "^use " | tr ';' ' ' | while read a b c; do echo $b; done | sort -iu > modules.txt 
    

    这给了我一个只有模块名的文件,每行一个。

    然后我用了这个

    cat modules.txt | while read a; do cpan $a; done 
    

    不漂亮,但这次它完成了任务。

        4
  •  0
  •   Sinan Ünür    17 年前

    或者让 PAR's pp 在一个可执行文件中收集所需的一切,为您完成这项工作。

    推荐文章