-1
我在perl项目上工作,我必须动态地使用perl模块。我有以下模块称为CT.pm:动态使用perl模块
sub new {
my $class = shift;
my ($debug, $debug_matches,%checkHash) = @_;
my $self = {};
$self->{DEBUG} = shift;
$self->{DEBUG_MATCHES} = shift;
$self->{CHECKRESULT_OK} = "COMPLIANT";
$self->{CHECKRESULT_ERROR} = "NONCOMPLIANT";
%{$self->{checkHash}} = %checkHash;
eval{
use $checkHash{"type"};
$check = $checkHash{"type"}->new($self->{DEBUG},$self->{DEBUG_MATCHES},%checkHash);
};
bless($self,$class);
return $self;
}
此构造函数获取名为%checkHash作为参数的散列。这个散列有一个叫做类型的键。此键映射到我想要动态使用的perl模块的名称的值。
我想出了以下的方法来做到这一点:(我知道不会工作,我也知道人们说,EVAL是坏的):
eval{
use $checkHash{"type"};
$check = $checkHash{"type"}->new($self->{DEBUG},$self->{DEBUG_MATCHES},%checkHash);
};
但这个想法是动态使用perl模块,名称为$ checkHash {“type”}。
如果任何人有任何想法如何做到这一点请帮助:) thx! :D
找到了一种方法 - 不能在8小时内发布任何解决方案。 – Diemauerdk
这已被讨论过:http://stackoverflow.com/questions/251694/how-can-i-check-if-i-have-a-perl-module-before-using-it http:// stackoverflow。 com/questions/1094125/try-to-use-module-in-perl-and-print-message-if-module-not-available http://stackoverflow.com/questions/1917261/how-can-i-dynamically -include-perl-modules http://stackoverflow.com/questions/2855207/how-do-i-load-a-module-at-runtime http://stackoverflow.com/questions/3470706/perl-dynamically-include http://stackoverflow.com/questions/3945583/how-can-i-conditionally-use http://stackoverflow.com/questions/6855970/use-of-eval – daxim