如何检查是否安装了Perl模块? [英] How do I check whether a Perl module is installed?
问题描述
我正在编写一个小的Perl脚本,该脚本取决于可能可用的某些模块,因此在安装过程中,我将必须检查其中是否包含所有内容.我可以只写use some::module
并查看是否出现错误,但是诸如您需要安装some :: module"之类的简短消息对最终用户会更有用.
I'm writing a small Perl script that depends on some modules that might be available, so during the installation I would have to check if everythings there. I could just write use some::module
and see if an error comes up, but a short message like "You need to install some::module" would be more helpful for endusers.
我也可以只搜索@INC
中的每个目录,但是因为它是Perl,所以必须有一种更简单的方法.
I also could just search every directory in @INC
, but as it's Perl, there has to be an easier way.
推荐答案
perl -MSome::Module -e ';'
哎呀,看错了问题.我以为您想一次性了解,而不是以可恢复的方式发现它.我总是这样:
Whoops, misread the question. I thought you wanted to know in a one-off instance, not discovering it in a recoverable manner. I always use something like this:
sub try_load {
my $mod = shift;
eval("use $mod");
if ($@) {
#print "\$@ = $@\n";
return(0);
} else {
return(1);
}
}
您这样使用:
$module = 'Some::Module';
if (try_load($module)) {
print "loaded\n";
} else {
print "not loaded\n";
}
这篇关于如何检查是否安装了Perl模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!