如何断言脚本是否使用过于现代的模块版本?
#!/usr/bin/perl
use strict;
use warnings;
use Encode;
use version;
# no v5.00; # Works as I expected
# no Encode v2.80; # But this doesn't work
# What I want to do is:
BEGIN {
my $current = version->parse($Encode::VERSION);
my $fatal = version->parse('v2.80');
if ($current >= $fatal) {
die "Encode.pm since $fatal too modern--this is $current";
}
}
似乎我可以使用函数no
来限制模块版本。 但是没有运气。
https://perldoc.perl.org/functions/no.html
无模块版本
>no
执行与use
相同的检查。
例如,如果要禁用自动活力,并且需要模块 0.18 版本中的修复程序之一,则可以使用
no autovivification 0.18;
因此,您确实必须添加自己的支票,如您所显示的那样。
具体说来
use Module v2.80;
相当于
BEGIN {
require Module;
Module->VERSION(v2.80); # Make sure >= v2.80
Module->import();
}
而
no Module v2.80;
相当于
BEGIN {
require Module;
Module->VERSION(v2.80); # Make sure >= v2.80
Module->unimport();
}