perl脚本检查是否安装了perl模块

时间:2012-02-18 01:13:00

标签: perl loops

我希望能够在列表中的每个模块上运行此测试。不确定如何在每个项目上循环。

use Module::Load;
eval {
  load Image::Magick;
  1;
} or die "you need Module to run this program";
  • 位::矢量
  • 鲤鱼::家族
  • 检查:: ISA
  • DBD ::甲骨文
  • DBI
  • 数据:: GUID
  • 数据:: OptList
  • 数据:: TreeDumper
  • 数据:: UUID
  • 日期::计算值
  • 杰韦利::大小
  • 的ExtUtils :: MakeMaker的
  • 日志::调度
  • 日志::调度::文件::滚动轴承
  • 日志::调度:: FileRotate
  • 日志:: Log4perl
  • PARAMS ::的Util
  • PARAMS ::验证
  • 排序::自然
  • 子::导出
  • 子::安装
  • 子:: Uplevel
  • SYS ::系统日志
  • 期限::大小
  • 测试::异常
  • 测试::简单
  • 测试::使用::确定
  • 树::简单

4 个答案:

答案 0 :(得分:4)

尝试使用:

#!/usr/bin/perl 
use 5.010;
use strict;
use warnings;

my @modules = qw(
    Bit::Vector
    Carp::Clan
    Check::ISA
    DBD::Oracle
    DBI
    Tree::Simple
);

for(@modules) {
    eval "use $_";
    if ($@) {
        warn "Not found : $_" if $@;
    } else {
        say "Found : $_";
    }
}

答案 1 :(得分:2)

如果您不需要使用Perl来执行此操作,则可以在shell脚本中执行此操作:

#!/bin/sh
MODULES="Data::Dumper Foobar::Test"

for i in $MODULES ; do
  if $(perl -M$i -e '1;' >/dev/null 2>&1 ); do
    echo "Ok."
  else
    echo "No." 
  fi
done

除了使用echo之外,您还可以执行其他操作。

代码序列:

perl -MData::Dumper '1;'

将以错误值0(ok)和

退出
perl -MFoobar::Test '1;'

将退出,错误值为2(发生错误)。

答案 2 :(得分:1)

我想回应@daxim的评论,看来你正在寻找你的模块的发行版。为此,我会查看Module::BuildDist::Zilla。我的几乎所有模块都使用这两种机制中的一种,所以如果你需要例子,请随意大胆my GitHub。查找Build.PLdist.ini个文件(分别代表M :: B或D :: Z)。

答案 3 :(得分:0)

full script

  # a around M42's answer with some more user-kindness ...
  use strict ; use warnings ; 
  use 5.10.0 ; 

  #  quick and dirty check for prerequisites perl modules:
  #  courtesy of:http://stackoverflow.com/a/9340304/65706
  #  if you have a calling bash script call by :
  #  perl "/path/to/isg_pub_preq_cheker.pl"
  #  export ret=$?
  #  test $ret -ne 0 && doExit 1 "[FATAL] perl modules not found!!!"


  # check that all the required modules are installed
  my ( $ret , $msg ) = doCheckRequiredModules();

  unless ( $ret == 0 ) {
     print "$msg" ; 
     # give some time for the user to react
     sleep 7;
  }

  exit(0);

  sub doCheckRequiredModules {

     my @modules = qw(
        ExtUtils::MakeMaker
        Test::More
        Test::Deep
        File::Copy::Recursive
        HTML::TreeBuilder
        HTML::TreeBuilder::XPath
        HTML::TableExtract
        HTML::ElementTable
        Data::Printer
     );

     for(@modules) {
         eval "use $_";
         if ($@) {

           my $msg = "\n\n\n [FATAL] did not found the following perl module: $_ " ; 
           $msg .= "\n install it in the shell by running the following command:" ; 
           # if the user knows already the difference between the running the cmd 
           # with sudo or he / she probably knows already how-to install perl modules
           $msg .= "\n sudo perl -MCPAN -e 'install $_'\n\n\n" ; 
           $msg .= "\n if you seem to be stuck in circular reference kind of loop try even :\n" ; 
           $msg .= "\n sudo perl -MCPAN -e 'CPAN::Shell->force(qw( install $_));'\n" ; 
           $msg .= "\n You may end-up now with Ctrl + C \n\n\n" ; 

           return ( 1, "$msg")  if $@;
         } else {
             say "[INFO ] == ok == check for prerequisite perl module : $_";
         }
     }

     return ( 0 , "all required modules found" ) ;   
  }
  #eof sub