Perl:回退到旧模块
Perl: Fallback to an older Module
我有几个使用 Nagios::Plugin
. The module is now deprecated and was substituted by Monitoring::Plugin
的 Nagios 插件。
由于几个发行版还没有更新,我在代码中检查了哪个模块可用
sub load_module {
my @names = @_;
my $loaded_module;
for my $name (@names) {
my $file = $name;
# requires need either a bare word or a file name
$file =~ s{::}{/}gsxm;
$file .= '.pm';
eval {
require $file;
$name->import();
};
if ( !$EVAL_ERROR ) {
$loaded_module = $name;
last;
}
}
if ( !$loaded_module ) {
# handle error ...
exit 2;
}
return $loaded_module;
}
my $plugin_module = load_module( 'Monitoring::Plugin', 'Nagios::Plugin' );
my $plugin_threshold_module = load_module( 'Monitoring::Plugin::Threshold', 'Nagios::Plugin::Threshold' );
我曾经使用
在 Makefile.PL
文件中检查模块可用性
requires 'Nagios::Plugin' => 0;
requires 'Nagios::Plugin::Threshold' => 0;
并在我的插件中使用该模块。
Makefile.PL 中是否有标准方法来检查模块(即 Monitoring::Plugins
),如果不可用则检查是否有其他选项可用(即 Nagios::Plugin::
)?
Makefile.PL 只是一个程序,您可以在 运行 时进行同样的检查。
my %prereqs = (
...declare your static prereqs...
);
if( eval { require Monitoring::Plugin } ) {
$prereqs{'Monitoring::Plugin'};
}
else {
$prereqs{'Nagios::Plugin'};
}
WriteMakefile(
...blah blah blah...
PREREQ_PM => \%prereqs
);
其他模块构建系统,例如 Module::Install and Dist::Zilla 可能支持插件为您执行此操作,但基本结果是相同的。
请注意,这将为您的分发生成一个 META 文件,描述您的先决条件。它不会有这种逻辑,而是会显示您的模块需要您构建分发时的任何结果,规范中没有办法表达您想要的内容。这不会破坏任何东西,只要 dynamic_config is true (which it is by default). However, the dynamic nature of your requirements will not be shown on things like https://metacpan.org.
,CPAN 模块安装程序就会 运行 Makefile.PL 并相信它在 META 文件中所说的内容
我有几个使用 Nagios::Plugin
. The module is now deprecated and was substituted by Monitoring::Plugin
的 Nagios 插件。
由于几个发行版还没有更新,我在代码中检查了哪个模块可用
sub load_module {
my @names = @_;
my $loaded_module;
for my $name (@names) {
my $file = $name;
# requires need either a bare word or a file name
$file =~ s{::}{/}gsxm;
$file .= '.pm';
eval {
require $file;
$name->import();
};
if ( !$EVAL_ERROR ) {
$loaded_module = $name;
last;
}
}
if ( !$loaded_module ) {
# handle error ...
exit 2;
}
return $loaded_module;
}
my $plugin_module = load_module( 'Monitoring::Plugin', 'Nagios::Plugin' );
my $plugin_threshold_module = load_module( 'Monitoring::Plugin::Threshold', 'Nagios::Plugin::Threshold' );
我曾经使用
在Makefile.PL
文件中检查模块可用性
requires 'Nagios::Plugin' => 0;
requires 'Nagios::Plugin::Threshold' => 0;
并在我的插件中使用该模块。
Makefile.PL 中是否有标准方法来检查模块(即 Monitoring::Plugins
),如果不可用则检查是否有其他选项可用(即 Nagios::Plugin::
)?
Makefile.PL 只是一个程序,您可以在 运行 时进行同样的检查。
my %prereqs = (
...declare your static prereqs...
);
if( eval { require Monitoring::Plugin } ) {
$prereqs{'Monitoring::Plugin'};
}
else {
$prereqs{'Nagios::Plugin'};
}
WriteMakefile(
...blah blah blah...
PREREQ_PM => \%prereqs
);
其他模块构建系统,例如 Module::Install and Dist::Zilla 可能支持插件为您执行此操作,但基本结果是相同的。
请注意,这将为您的分发生成一个 META 文件,描述您的先决条件。它不会有这种逻辑,而是会显示您的模块需要您构建分发时的任何结果,规范中没有办法表达您想要的内容。这不会破坏任何东西,只要 dynamic_config is true (which it is by default). However, the dynamic nature of your requirements will not be shown on things like https://metacpan.org.
,CPAN 模块安装程序就会 运行 Makefile.PL 并相信它在 META 文件中所说的内容