How do I use a certain version (or higher) of a perl module in my perl script?
I'm using Term::ANSIColor开发者_如何学运维
in my Perl script to do colorization of terminal output, and I am using the colorstrip
function, which was only added in Term::ANSIColor
version 2.01, according to the changelog. So, is there a way to make my script automatically die
with an appropriate error message if it doesn't find at least that version of Term::ANSIcolor
?
Just:
use Term::ANSIColor 2.01;
See perldoc -f use
:
use Module VERSION LIST
If the VERSION argument is present between Module and LIST, then the use will call the VERSION method in class Module with the given version as an argument. The default VERSION method, inherited from the UNIVERSAL class, croaks if the given version is larger than the value of the variable $Module::VERSION .
Most modules define the package variable $VERSION
.
use Term::ANSIColor;
die "Sorry, this program needs Term::ANSIColor >= v2.01!\n"
unless $Term::ANSIColor::VERSION >= 2.01;
This is also a good way to specify a maximum version of a module.
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;
Simply specify the version you want to use:
use Some::Module 2.13;
If the version is not at least 2.13, the operation will fail.
You can experiment with the version that is available on the command line:
perl -MSome::Module=9999 -e 'exit 0'
This will usually die with the wrong version number shown in the error message (unless the module you're trying to use happens to have a 5 digit or longer version number, or unless the module is like strict
and doesn't like being loaded via the command line with a version number or like URI
(see the comments for details)).
$ perl -MFile::Find=999 -e 'exit 0'
File::Find version 999 required--this is only version 1.07 at
/usr/perl5/5.8.4/lib/Exporter/Heavy.pm line 121.
BEGIN failed--compilation aborted.
$ perl -e 'use File::Find 999; exit 0'
File::Find version 999 required--this is only version 1.07 at -e line 1.
BEGIN failed--compilation aborted at -e line 1.
$
Run on a machine I don't normally use, hence the antiquated version of Perl.
精彩评论