如何在Perl中有条件地导入包?
发布时间:2020-12-16 06:07:46 所属栏目:大数据 来源:网络整理
导读:我有一个Perl脚本,它使用一个不常见的模块,我希望它可以在没有安装该模块的情况下使用,尽管功能有限.可能吗? 我想到了这样的事情: my $has_foobar;if (has_module "foobar") { use it $has_foobar = true;} else { print STDERR "Warning: foobar not foun
我有一个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; } 解决方法
您可以使用
require在运行时加载模块,使用
eval来捕获可能的异常:
eval { require Foobar; Foobar->import(); }; if ($@) { warn "Error including Foobar: $@"; } 另见perldoc use. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |