How can I find the version of an installed Perl module?

PerlModuleVersionCpan

Perl Problem Overview


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

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

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

Perl Solutions


Solution 1 - Perl

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

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

Solution 2 - Perl

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:

$ 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)
    [email protected]

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

$ 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.

Solution 3 - Perl

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).

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

Solution 4 - Perl

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

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 version 9999 required--this is only version 1.1.
BEGIN failed--compilation aborted.

(Neat trick I learned from Matt Trout.)

Solution 5 - Perl

If you are lucky, the module will have a package variable named $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.

Solution 6 - Perl

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

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

Solution 7 - Perl

Easiest to remember and most robust version for me:

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

Solution 8 - Perl

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.

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.

Solution 9 - Perl

I wrote a small script to report that: 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.

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:

$ 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

Solution 10 - Perl

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

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:

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

Solution 11 - Perl

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.

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";

}

Solution 12 - Perl

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

$ module-version

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrew StephensView Question on Stackoverflow
Solution 1 - PerlzigdonView Answer on Stackoverflow
Solution 2 - Perlbrian d foyView Answer on Stackoverflow
Solution 3 - PerljmcnamaraView Answer on Stackoverflow
Solution 4 - PerlPenfoldView Answer on Stackoverflow
Solution 5 - PerlJon EricsonView Answer on Stackoverflow
Solution 6 - PerlDrew StephensView Answer on Stackoverflow
Solution 7 - PerlJpsyView Answer on Stackoverflow
Solution 8 - PerlDave RolskyView Answer on Stackoverflow
Solution 9 - PerlcjmView Answer on Stackoverflow
Solution 10 - PerltyeView Answer on Stackoverflow
Solution 11 - PerlBMWView Answer on Stackoverflow
Solution 12 - PerlsmonffView Answer on Stackoverflow