我有一个Perl脚本,它使用了一个不那么常见的模块,我希望它可以在未安装该模块的情况下使用,尽管功能有限。可能吗?如何在Perl中有条件地导入包?
我认为是这样的:
my $has_foobar;
if (has_module "foobar") {
<< use it >>
$has_foobar = true;
} else {
print STDERR "Warning: foobar not found. Not using it.\n";
$has_foobar = false;
}
如果您打算调用'import',那么您可能想要将其封装在'BEGIN'块中,以便在编译时发生。否则,'import'不太可能有用。 – cjm 2010-10-18 08:56:53