Haiyuan Zhang
Haiyuan Zhang

Reputation: 42822

How can I get the version and location of an installed Perl module?

Is there a smart way to detect whether a certain Perl module has been installed in your system? My old sutpid way is to write a Perl script in which the only thing I do is just to use the module. If nothing croaks when I run the detect script, then I know the module has been installed, although I still don't know which version and where the module has been installed .

thanks in advance.

Upvotes: 5

Views: 4004

Answers (9)

jmcnamara
jmcnamara

Reputation: 41634

The pmvers utility and the other pmtools will do what you need. Otherwise here is a one-liner to find a module version:

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

Upvotes: 1

mklement0
mklement0

Reputation: 439247

If you're looking for a cross-platform CLI (Linux, OSX, Windows), consider my whichpm utility; e.g.:

# Locate the Data::Dumper module, and also print
# version information and core-module status.
$ whichpm -v Data::Dumper
Data::Dumper    2.145   core>=5.005 /usr/lib/perl/5.18/Data/Dumper.pm

It can also find accidental duplicates and list all installed modules.

If you happen to have Node.js / io.js installed, you can install it from the npm registry:

[sudo] npm install whichpm -g

For manual installation instructions and more, see the repo; here's a direct download link to the latest version (will stay current).

Upvotes: 0

Thomas Erskine
Thomas Erskine

Reputation: 59

Here's a program which does that:

#!/usr/bin/perl
# testmod - test to see if a module is available

use strict;
use warnings;

my $mod = (shift @ARGV) || die "usage: $0 module\n";

# convert module-name to path
my $file = $mod;
$file =~ s{::}{/}gsmx;
$file .= '.pm';

# Pull in the module, if it exists
eval { require $file }
    or die "can't find module $mod\n";

# Get the version from the module, if defined
my $ver;
{ no strict 'refs';
    $ver = ${$mod . "::VERSION"} || 'UNKNOWN';
}
# And its location
my $from = $INC{$file};
print "module $mod is version $ver loaded from $from\n";

Upvotes: 3

hdp
hdp

Reputation: 778

The shortest thing I know of that doesn't involve a script or shell alias:

$ perl -MFoo::Bar\ 99
Foo::Bar version 99 required--this is only version 1.234.

(or the usual message about not being in @INC if it's not installed)

For the curious, this is the same as perl -e 'use Foo::Bar 99'.

Upvotes: 6

sateesh
sateesh

Reputation: 28693

I don't know if there is any smart way for this. But what I usually
do is to make use of '-l' or '-m' option of perldoc. For example :

%perldoc -l XML::Simple

and the output is something like below,which is the full path of module file

.../lib/XML/Simple.pm

The advantage with this approach compared to yours is that, if the module is installed
the output contains the path for module location. However when the module is not installed
or if it doesn't has a perldoc the error message shown is "No documentation found for ...",
making it impossible to distinguish if the error is due to missing module or missing
documentation. In such scenario the -m option becomes handy since it prints entire
contents of the file along with the path.

Upvotes: 1

Ether
Ether

Reputation: 53986

I use these bash function/Perl oneliners to find the version number and location of Perl modules:

# equivalent to perldoc -l <module>
perlwhere() {
    perl -wle'eval "require $ARGV[0]" or die; ($mod = $ARGV[0]) =~ s|::|/|g; print $INC{"${mod}.pm"}' $1
}

perlversion() {
    perl -M$1 -wle'print $ARGV[0]->VERSION' $1
}

: [ether ~].2$; perlwhere Test::More
/usr/lib/perl5/5.8.8/Test/More.pm
: [ether ~].2$; perlversion Test::More
0.94

Upvotes: 1

daxim
daxim

Reputation: 39158

Use pmvers. Like the name suggests, it shows the version of an installed module. If a module is not installed, it fails with the familiar error message: Can't locate … in @INC (@INC contains: …)

Use pmpath from the same distribution to find a module's installation path.

Upvotes: 2

Gregory Pakosz
Gregory Pakosz

Reputation: 70234

instmodsh

NAME
       instmodsh - A shell to examine installed modules

SYNOPSIS
           instmodsh

DESCRIPTION
       A little interface to ExtUtils::Installed to examine installed modules, validate your packlists and even create a tarball from an installed module.

SEE ALSO
       ExtUtils::Installed

Upvotes: 3

DMI
DMI

Reputation: 7191

Something like:

perl -MModule -e 'print "$Module::VERSION\n"' 2>/dev/null || echo "Not installed"

would give you the version of a given module, or tell you it isn't installed. Usage would look like:

perl -MXML::Parser -e 'print "$XML::Parser::VERSION\n"' 2>/dev/null || echo "Not installed"

To find the module path, you could examine @INC to find possible locations, or you could perhaps look into perlwhich. There is also pmpath from pmtools.

Upvotes: 10

Related Questions