如何在perl脚本中使用perl模块的某个版本(或更高版本)?

Rya*_*son 7 perl perl-module version

我正在使用Term::ANSIColor我的Perl脚本对终端输出进行着色,并且我正在使用该colorstrip函数,该函数仅在Term::ANSIColor版本2.01中添加,根据更改日志.那么,有没有办法让我的脚本自动die使用适当的错误消息,如果它至少找不到该版本的Term::ANSIcolor

yst*_*sth 21

只是:

use Term::ANSIColor 2.01;
Run Code Online (Sandbox Code Playgroud)

perldoc -f use:

使用模块版本列表

如果Module和LIST之间存在VERSION参数,则use将使用给定版本作为参数调用Module类中的VERSION方法.如果给定版本大于变量$ Module :: VERSION的值,则继承自UNIVERSAL类的默认VERSION方法会破坏.


mob*_*mob 7

大多数模块定义包变量$VERSION.

use Term::ANSIColor;
die "Sorry, this program needs Term::ANSIColor >= v2.01!\n"
    unless $Term::ANSIColor::VERSION >= 2.01;
Run Code Online (Sandbox Code Playgroud)

这也是指定模块最大版本的好方法.

use Module::Foo;
die "You need an *older* version of Module::Foo that ",
    "still has the &barbaz method defined"
    if $Module::Foo::VERSION >= 0.47;
Run Code Online (Sandbox Code Playgroud)