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

63,680

Solution 1

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 2

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

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

Solution 3

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

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

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.

Share:
63,680
Drew Stephens
Author by

Drew Stephens

My website

Updated on November 21, 2020

Comments

  • Drew Stephens
    Drew Stephens over 3 years

    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
    }
    
  • Pakage
    Pakage over 15 years
    I've been using this for ages, too. Couldn't remember where I picked it up from, but most likely I got it from the same place.
  • igelkott
    igelkott over 15 years
    Minor change for a Windows command prompt (for whatever reason): perl -M"Foo::bar 9999"
  • Mei
    Mei over 12 years
    (I know this is an old answer!) I tried -D and -O and cpan didn't understand those options. The manpage showed the options but perldoc didn't. What gives?
  • Andy Ray
    Andy Ray about 10 years
    this isn't abstracted to work with any module. not right answer.
  • Peter Mortensen
    Peter Mortensen about 9 years
    This works. However, on Windows it should quoted differently for it to work (example for File::Fetch): perl -MFile::Fetch -le "print $File::Fetch::VERSION" - double quotes instead of single quotes (otherwise, an error message is the result: "Can't find string terminator "'" anywhere before EOF at -e line 1.")
  • Peter Mortensen
    Peter Mortensen about 9 years
    @igelkott: This did work with File::Fetch, perl -M"File::Fetch 9999" (64-bit Perl 5.18). Output: File::Fetch version 9999 required--this is only version 0.46.. However, the second line, "BEGIN failed--compilation aborted.", is confusing at first glance (listed in the answer).
  • brian d foy
    brian d foy over 7 years
    This isn't actually needed for modules to be distributed on CPAN. Many distributions look at that variable to declare the version, but only the main module in the distro needs to do that. Look at any of the modules in Mojolicious that aren't Mojolicuous.pm, for instance.
  • Jacques
    Jacques over 2 years
    Perfect answer.