我希望能够在列表中的每个模块上运行此测试。不确定如何在每个项目上循环。
use Module::Load;
eval {
load Image::Magick;
1;
} or die "you need Module to run this program";
答案 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::Build
或Dist::Zilla
。我的几乎所有模块都使用这两种机制中的一种,所以如果你需要例子,请随意大胆my GitHub。查找Build.PL
或dist.ini
个文件(分别代表M :: B或D :: Z)。
答案 3 :(得分:0)
# 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