http://qs321.pair.com?node_id=37237

How many times have you wondered what version of a module you had installed, just to check it using perl -e. Why bother? Just put this little snippet in '/usr/local/bin' or some other accessible place. (Win32 users can wrap it in a batch file.)

perlmodver Template DBI File::Spec

Results in:

Template : 2.00-beta5 DBI : 1.13 File::Spec : 0.6
#!/usr/bin/perl use strict; foreach my $module ( @ARGV ) { eval "require $module"; printf( "%-20s: %s\n", $module, $module->VERSION ) unless ( $@ ); }

Replies are listed 'Best First'.
RE: Find perl module version from command-line
by Fastolfe (Vicar) on Oct 18, 2000 at 02:27 UTC
    Things like this are frequently done in conjunction with CPAN lookups. Many people don't realize that the CPAN module itself can be used in your own scripts. You can extend lachoy's example a lot further by using CPAN, at the expense of a lot of time:
    #!/usr/bin/perl use CPAN; printf("%-20s %10s %10s\n", "Module", "Installed", "CPAN"); foreach $a (@ARGV) { foreach $mod (CPAN::Shell->expand("Module", $a)){ printf("%-20s %10s %10s %s\n", $mod->id, $mod->inst_version eq "undef" || !defined($mod->inst_version) ? "-" : $mod->inst_version, $mod->cpan_version eq "undef" || !defined($mod->cpan_version) ? "-" : $mod->cpan_version, $mod->uptodate ? "" : "*" ); } }
    Running with arguments: DBI /DBD::/
    Module Installed CPAN DBI 1.13 1.14 * DBD::ADO 0.14 1.17 * DBD::ASAny - 1.09 * DBD::Adabas - 0.2003 * DBD::Altera - - * DBD::CSV - 0.1024 *
    ...etc. Though if all you're interested in is the installed version of modules, you're FAR better off going with lachoy's script, since the code above will rely upon CPAN data, which will require time to fetch, extract and browse.
RE: Find perl module version from command-line
by KM (Priest) on Oct 18, 2000 at 16:53 UTC
    This is how I do it for checking one module.. I use CPAN to check multiple:

    perl -MMODULE -e 'print $MODULE::VERSION';

    Of course, if the module author doesn't put the suggested $VERSION variable in, this won't work :)

    Cheers,
    KM

      Right! This is what I meant by perl -e in my writeup. but I thought it was getting kind of tedious to type this in every time... TMTOWDI.

      A nice shortcut is

      perl -e 'use Some::Module 9e9'
      It is worth noting that perl -MSome::Module=9e9 -e1 works only if that module is Exporter-based, because -M passes that number as an import argument instead of a version number, but then Exporter interprets it as a version number.
Re: Find perl module version from command-line
by jmcnamara (Monsignor) on May 01, 2001 at 16:20 UTC

    I was going to post the following to the Snippets section when I came across your nicer version:
    perl -le 'eval "require $ARGV[0]" and print ${"$ARGV[0]::VERSION"} +' Module

    Your method of printing the version number is cleaner as well:
    perl -le 'eval "require $ARGV[0]" and print $ARGV[0]->VERSION' Mod +ule

    I'm glad that I searched before posting. ;-)

    John.
    --

      When I run the perl script provided by Fastolfe I get some extra output in the results:
      dal1:/home/user1 % perlmodver.pl CPAN Module Installed CPAN CPAN: Storable loaded ok (v2.13) Going to read /home/user1/.cpan/Metadata Database was generated on Thu, 03 Jan 2008 05:38:06 GMT CPAN 1.9205 1.9205 dal1:/home/user1 %
      Is there a simple way to discard this additional text?

        The extra output is being generated by the CPAN module itself. I checked out the code on mine and I don't see any conditionals or anything that could be passed to supress that output. So, the simple answer is to grep for what you want or use perlmodver.pl CPAN | tail +5 because I don't think there's much you can do from the perl side that doesn't require putting more effort into it than it's worth.

        --
        naChoZ

        Therapy is expensive. Popping bubble wrap is cheap. You choose.

Re: Find perl module version from command-line
by xorl (Deacon) on Apr 11, 2013 at 17:24 UTC
    Yet another way this can be done.
    #!/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"; }