如何找到已安装的Perl模块的版本?

时间:2022-11-08 06:58:35

How do you find the version of an installed Perl module?

如何找到已安装的Perl模块的版本?

This is in an answer down at the bottom, but I figure it important enough to live up here. With these suggestions, I create a function in my .bashrc

这是在底部的答案,但我认为它足够重要,住在这里。有了这些建议,我在.bashrc中创建了一个函数

function perlmodver {
    perl -M$1 -e 'print "Version " . $ARGV[0]->VERSION . " of " . $ARGV[0] . \
    " is installed.\n"' $1
}

12 个解决方案

#1


53  

Why are you trying to get the version of the module? Do you need this from within a program, do you just need the number to pass to another operation, or are you just trying to find out what you have?

你为什么试图获得该模块的版本?你是否需要在一个程序中使用它,你只需要将数字传递给另一个操作,或者你只是想找出你拥有的东西?

I have this built into the cpan (which comes with perl) with the -D switch so you can see the version that you have installed and the current version on CPAN:

我将这个内置到带有-D开关的cpan(带有perl)中,这样您就可以在CPAN上看到已安装的版本和当前版本:

$ cpan -D Text::CSV_XS

Text::CSV_XS
-------------------------------------------------------------------------
        Fast 8bit clean version of Text::CSV
        H/HM/HMBRAND/Text-CSV_XS-0.54.tgz
        /usr/local/lib/perl5/site_perl/5.8.8/darwin-2level/Text/CSV_XS.pm
        Installed: 0.32
        CPAN:      0.54  Not up to date
        H.Merijn Brand (HMBRAND)
        h.m.brand@xs4all.nl

If you want to see all of the out-of-date modules, use the -O (capital O) switch:

如果要查看所有过时的模块,请使用-O(大写O)开关:

$ cpan -O
Module Name                                Local    CPAN
-------------------------------------------------------------------------
Apache::DB                                0.1300  0.1400
Apache::SOAP                              0.0000  0.7100
Apache::Session                           1.8300  1.8700
Apache::SizeLimit                         0.0300  0.9100
Apache::XMLRPC::Lite                      0.0000  0.7100
... and so on

If you want to see this for all modules you have installed, try the -a switch to create an autobundle.

如果要查看已安装的所有模块的此信息,请尝试使用-a开关创建自动提示。

#2


52  

Most modules (especially ones from The CPAN) have a $VERSION variable:

大多数模块(尤其是来自CPAN的模块)都有一个$ VERSION变量:

perl -MSome::Module -le 'print $Some::Module::VERSION'

#3


29  

VERSION is a UNIVERSAL method of all Perl classes. You can use it to get the module version (if it has been set which it usually has).

VERSION是所有Perl类的UNIVERSAL方法。您可以使用它来获取模块版本(如果已经设置了它通常具有)。

Here is a one liner where you only have to add the module name once:

这是一个单行,您只需添加一次模块名称:

perl -le 'eval "require $ARGV[0]" and print $ARGV[0]->VERSION' Some::Module

#4


16  

There is a less-typing trick, that works provided your module doesn't have something insane like a Unix timestamp as a version number.

有一个打字较少的技巧,如果您的模块没有疯狂的东西,比如Unix时间戳作为版本号。

perl -MFoo::Bar\ 9999

This works because what it translates to is

这是有效的,因为它转化为

use Foo::Bar 9999;

i.e. a version of Foo::Bar that's at least version 9999 or newer. And what you get is

即Foo :: Bar的版本,至少为9999或更新版本。而你得到的是

Foo::Bar version 9999 required--this is only version 1.1.
BEGIN failed--compilation aborted.

(Neat trick I learned from Matt Trout.)

(我从Matt Trout那里学到了很多技巧。)

#5


8  

If you are lucky, the module will have a package variable named $VERSION:

如果幸运的话,该模块将有一个名为$ VERSION的包变量:

$ perl -MCPAN -e 'print "$CPAN::VERSION\n"'
1.9205

This is needed for modules to be distributed on CPAN, but internally developed modules might follow a different convention or none at all.

这需要在CPAN上分发模块,但内部开发的模块可能遵循不同的约定或根本不遵循。

#6


5  

Thanks for the answers! I've created a function in my .bashrc to easily find the version of a Perl module:

谢谢你的答案!我在.bashrc中创建了一个函数,以便轻松找到Perl模块的版本:

function perlmodver {
    perl -M$1 -e 'print $ARGV[0]->VERSION . "\n"' $1
} 

#7


3  

Check out the pmtools scripts on CPAN. If you're using a Debian(-based) distro, there's also a handy pmtools package. This includes a script "pmvers" that tells you a module's version. It's quite handy.

查看CPAN上的pmtools脚本。如果您正在使用Debian(基于)的发行版,那么还有一个方便的pmtools包。这包括一个脚本“pmvers”,它告诉你模块的版本。它非常方便。

It does something similar to the various one-liners folks posted, but it's a bit smarter about error handling, and can give you the version of more than one module at once.

它的功能类似于发布的各种单行人员,但它在错误处理方面更为智能,并且可以同时为您提供多个模块的版本。

#8


2  

I wrote a small script to report that: perlver.

我写了一个小脚本来报告:perlver。

This is a simple little tool that tells you what version of a module you have installed, and where the .pm file is located. It also ensures the module can be loaded successfully. It automatically converts ‘-’, ‘/’, or ‘\’ to ‘::’, so you can use a pathname or distribution name instead of the canonical module name.

这是一个简单的小工具,可以告诉您已安装的模块版本以及.pm文件所在的位置。它还确保可以成功加载模块。它会自动将' - ','/'或'\'转换为'::',因此您可以使用路径名或分布名称而不是规范模块名称。

It assumes that the module defines a $VERSION. If the module doesn't define a $VERSION, it will still tell you where the .pm file is, so you can examine it manually. You can also check several modules at once:

它假定模块定义了$ VERSION。如果模块没有定义$ VERSION,它仍然会告诉你.pm文件的位置,所以你可以手动检查它。您还可以一次检查多个模块:

$ perlver CPAN DBD-Pg Getopt::Long
CPAN 1.7602 is
 /usr/lib/perl5/5.8.8/CPAN.pm
DBD::Pg 1.49 is
 /usr/lib/perl5/vendor_perl/5.8.8/i686-linux/DBD/Pg.pm
Getopt::Long 2.36 is
 /usr/lib/perl5/vendor_perl/5.8.8/Getopt/Long.pm

#9


1  

We have the system perl (/usr/bin/perl) in Solaris 10, and above solutions are useless. Some of them report "module.pm is not installed", some of them have no output.

我们在Solaris 10中有系统perl(/ usr / bin / perl),以上解决方案都没用。其中一些报告“未安装module.pm”,其中一些没有输出。

Here is the code which is helpful, which can list all modules and their version.

这是有用的代码,可以列出所有模块及其版本。

#!/usr/bin/perl

use strict;
use ExtUtils::Installed;

my @modules;
my $installed = ExtUtils::Installed->new();

if (scalar(@ARGV) > 0) {

    @modules = @ARGV;

} else {

    @modules = $installed->modules();

}

print "Module\tVersion\n";

foreach (@modules) {

    print $_ . "\t" . $installed->version($_) . "\n";

}

#10


1  

Easiest to remember and most robust version for me:

对我来说最容易记住和最强大的版本:

perl -e 'use Search::Elasticsearch; print $Search::Elasticsearch::VERSION;'

#11


0  

In addition, for modules that use Exporter.pm, you can get this information with this trick:

此外,对于使用Exporter.pm的模块,您可以使用以下技巧获取此信息:

perl -MSome::Module=99999 -ex
Some::Module version 99999 required--this is only version 1.9205 at ...

For modules that don't use Exporter.pm, a slightly longer trick reports the same information:

对于不使用Exporter.pm的模块,稍微长一点的技巧会报告相同的信息:

perl -e'use Some::Module 99999'
Some::Module version 99999 required--this is only version 1.9205 at ...

#12


0  

You can also take a look at App::module::version

您还可以查看App :: module :: version

$ module-version

The version of App::module::version in /home/yourself/perl5/lib/perl5 is 1.004

#1


53  

Why are you trying to get the version of the module? Do you need this from within a program, do you just need the number to pass to another operation, or are you just trying to find out what you have?

你为什么试图获得该模块的版本?你是否需要在一个程序中使用它,你只需要将数字传递给另一个操作,或者你只是想找出你拥有的东西?

I have this built into the cpan (which comes with perl) with the -D switch so you can see the version that you have installed and the current version on CPAN:

我将这个内置到带有-D开关的cpan(带有perl)中,这样您就可以在CPAN上看到已安装的版本和当前版本:

$ cpan -D Text::CSV_XS

Text::CSV_XS
-------------------------------------------------------------------------
        Fast 8bit clean version of Text::CSV
        H/HM/HMBRAND/Text-CSV_XS-0.54.tgz
        /usr/local/lib/perl5/site_perl/5.8.8/darwin-2level/Text/CSV_XS.pm
        Installed: 0.32
        CPAN:      0.54  Not up to date
        H.Merijn Brand (HMBRAND)
        h.m.brand@xs4all.nl

If you want to see all of the out-of-date modules, use the -O (capital O) switch:

如果要查看所有过时的模块,请使用-O(大写O)开关:

$ cpan -O
Module Name                                Local    CPAN
-------------------------------------------------------------------------
Apache::DB                                0.1300  0.1400
Apache::SOAP                              0.0000  0.7100
Apache::Session                           1.8300  1.8700
Apache::SizeLimit                         0.0300  0.9100
Apache::XMLRPC::Lite                      0.0000  0.7100
... and so on

If you want to see this for all modules you have installed, try the -a switch to create an autobundle.

如果要查看已安装的所有模块的此信息,请尝试使用-a开关创建自动提示。

#2


52  

Most modules (especially ones from The CPAN) have a $VERSION variable:

大多数模块(尤其是来自CPAN的模块)都有一个$ VERSION变量:

perl -MSome::Module -le 'print $Some::Module::VERSION'

#3


29  

VERSION is a UNIVERSAL method of all Perl classes. You can use it to get the module version (if it has been set which it usually has).

VERSION是所有Perl类的UNIVERSAL方法。您可以使用它来获取模块版本(如果已经设置了它通常具有)。

Here is a one liner where you only have to add the module name once:

这是一个单行,您只需添加一次模块名称:

perl -le 'eval "require $ARGV[0]" and print $ARGV[0]->VERSION' Some::Module

#4


16  

There is a less-typing trick, that works provided your module doesn't have something insane like a Unix timestamp as a version number.

有一个打字较少的技巧,如果您的模块没有疯狂的东西,比如Unix时间戳作为版本号。

perl -MFoo::Bar\ 9999

This works because what it translates to is

这是有效的,因为它转化为

use Foo::Bar 9999;

i.e. a version of Foo::Bar that's at least version 9999 or newer. And what you get is

即Foo :: Bar的版本,至少为9999或更新版本。而你得到的是

Foo::Bar version 9999 required--this is only version 1.1.
BEGIN failed--compilation aborted.

(Neat trick I learned from Matt Trout.)

(我从Matt Trout那里学到了很多技巧。)

#5


8  

If you are lucky, the module will have a package variable named $VERSION:

如果幸运的话,该模块将有一个名为$ VERSION的包变量:

$ perl -MCPAN -e 'print "$CPAN::VERSION\n"'
1.9205

This is needed for modules to be distributed on CPAN, but internally developed modules might follow a different convention or none at all.

这需要在CPAN上分发模块,但内部开发的模块可能遵循不同的约定或根本不遵循。

#6


5  

Thanks for the answers! I've created a function in my .bashrc to easily find the version of a Perl module:

谢谢你的答案!我在.bashrc中创建了一个函数,以便轻松找到Perl模块的版本:

function perlmodver {
    perl -M$1 -e 'print $ARGV[0]->VERSION . "\n"' $1
} 

#7


3  

Check out the pmtools scripts on CPAN. If you're using a Debian(-based) distro, there's also a handy pmtools package. This includes a script "pmvers" that tells you a module's version. It's quite handy.

查看CPAN上的pmtools脚本。如果您正在使用Debian(基于)的发行版,那么还有一个方便的pmtools包。这包括一个脚本“pmvers”,它告诉你模块的版本。它非常方便。

It does something similar to the various one-liners folks posted, but it's a bit smarter about error handling, and can give you the version of more than one module at once.

它的功能类似于发布的各种单行人员,但它在错误处理方面更为智能,并且可以同时为您提供多个模块的版本。

#8


2  

I wrote a small script to report that: perlver.

我写了一个小脚本来报告:perlver。

This is a simple little tool that tells you what version of a module you have installed, and where the .pm file is located. It also ensures the module can be loaded successfully. It automatically converts ‘-’, ‘/’, or ‘\’ to ‘::’, so you can use a pathname or distribution name instead of the canonical module name.

这是一个简单的小工具,可以告诉您已安装的模块版本以及.pm文件所在的位置。它还确保可以成功加载模块。它会自动将' - ','/'或'\'转换为'::',因此您可以使用路径名或分布名称而不是规范模块名称。

It assumes that the module defines a $VERSION. If the module doesn't define a $VERSION, it will still tell you where the .pm file is, so you can examine it manually. You can also check several modules at once:

它假定模块定义了$ VERSION。如果模块没有定义$ VERSION,它仍然会告诉你.pm文件的位置,所以你可以手动检查它。您还可以一次检查多个模块:

$ perlver CPAN DBD-Pg Getopt::Long
CPAN 1.7602 is
 /usr/lib/perl5/5.8.8/CPAN.pm
DBD::Pg 1.49 is
 /usr/lib/perl5/vendor_perl/5.8.8/i686-linux/DBD/Pg.pm
Getopt::Long 2.36 is
 /usr/lib/perl5/vendor_perl/5.8.8/Getopt/Long.pm

#9


1  

We have the system perl (/usr/bin/perl) in Solaris 10, and above solutions are useless. Some of them report "module.pm is not installed", some of them have no output.

我们在Solaris 10中有系统perl(/ usr / bin / perl),以上解决方案都没用。其中一些报告“未安装module.pm”,其中一些没有输出。

Here is the code which is helpful, which can list all modules and their version.

这是有用的代码,可以列出所有模块及其版本。

#!/usr/bin/perl

use strict;
use ExtUtils::Installed;

my @modules;
my $installed = ExtUtils::Installed->new();

if (scalar(@ARGV) > 0) {

    @modules = @ARGV;

} else {

    @modules = $installed->modules();

}

print "Module\tVersion\n";

foreach (@modules) {

    print $_ . "\t" . $installed->version($_) . "\n";

}

#10


1  

Easiest to remember and most robust version for me:

对我来说最容易记住和最强大的版本:

perl -e 'use Search::Elasticsearch; print $Search::Elasticsearch::VERSION;'

#11


0  

In addition, for modules that use Exporter.pm, you can get this information with this trick:

此外,对于使用Exporter.pm的模块,您可以使用以下技巧获取此信息:

perl -MSome::Module=99999 -ex
Some::Module version 99999 required--this is only version 1.9205 at ...

For modules that don't use Exporter.pm, a slightly longer trick reports the same information:

对于不使用Exporter.pm的模块,稍微长一点的技巧会报告相同的信息:

perl -e'use Some::Module 99999'
Some::Module version 99999 required--this is only version 1.9205 at ...

#12


0  

You can also take a look at App::module::version

您还可以查看App :: module :: version

$ module-version

The version of App::module::version in /home/yourself/perl5/lib/perl5 is 1.004