Module::Which - find version and path of locally installed modules
use Module::Which qw/ which /; my $result = which('Module::Which', 'YAML', 'XML::', 'DBI', 'DBD::'); while (my ($module, $info) = each %$result) { print "$module:\n": print " version: $info->{version}\n" if $info->{version}; print " path: $info->{path}\n" if $info->{path}; }
Or you can request an array ref instead of a hash ref:
my $result = which('strict', 'YAML', {return => 'ARRAY'}); foreach my $info (@$result) { print "$info->{pm}:\n": print " version: $info->{version}\n" if $info->{version}; print " path: $info->{path}\n" if $info->{path}; }
Module::Which provides the which() function, which takes the name of one or more modules, and returns information about those modules if they're intalled locally, including the version and the path.
Module::Which
which()
Module::Which is the basis of the script which_pm which displays the retrieved information to STDOUT.
which_pm
Modules are searched by name (like 'YAML') or by subcategories ('DBD::' means all modules under the DBD subdirectories of your Perl installation, matching both 'DBD::Oracle' and 'DBD::ODBC::Changes').
This module is very simple and most won't need it. But it has been instructive for the author to see how many broken modules one can find under your Perl installation (some which don't accept even a 'require' statement), modules with no version number and documentation files (named '.pm') which do not return a true value.
Well, all that said, this module is no more than automating:
perl -MInteresting::Module -e 'print $Interesting::Module::VERSION'
or better the one-liner
perl -e '$pm = shift; eval "require $pm"; print ${"${pm}::VERSION"}' DBI
my $info = which(@pm) my $info = which(@pm, { return => 'ARRAY', verbose => 1 }
Returns an array ref with information about the modules specified (by name or '::*' patterns). This information is a hash ref which actually contains:
pm: the name of the Perl module
version: the installed version
path: the full path to the Perl module
The version is the one found by accessing the scalar variable $VERSION of the package, after a require statement. If the module was not found, 'version' is undef. If the module has no $VERSION, 'version' is 'undef' (the string). If the 'require' statement failed, 'version' is 'unknown'.
$VERSION
undef
'undef'
A hash ref of options can be given as the last argument. The option return can take one of the values: 'ARRAY', 'HASH', 'HASH(FIRST)', 'HASH(MULTI)', 'HASH(LIST)'. 'HASH' is the default and means to return a hash ref. 'HASH' forces the return of a hash ref where the module name is used as key. 'ARRAY' is used to get an array ref.
return
The different strategies for returning a hash are different only if the same module is found twice or more times in the current search path. 'HASH' which is the same as 'HASH(FIRST)' only considers the first occurrence. 'HASH(MULTI)' will store multiple values in an array ref (if needed). The problem with MULTI is that sometimes you get a hash ref and sometimes an array ref of hash refs. If 'HASH(LIST)' is used, an array ref will be stored always, even if there is only one occurrence.
The option verbose can be set to turn on and off warnings on requiring the sought modules.
verbose
which is exported by default.
which
Module::Find was my friend to implement this module as a breeze. But I have found some itches and wrote my own Module::Which::List based on this and Module::List by Andrew Main.
After releasing it into CPAN, I found
Module::InstalledVersion Module::Info Module::List Module::Locate Module::Finder
Module::InstalledVersion has a different approach (it does not run the modules to find out their versions, but extract them via regexes) and does not has a command-line interface which was the main thrust of this distribution. I have been studying the others too.
Known bugs:
When a module is found twice or more in the library path, the version is the one of the first file.
Please report bugs via CPAN RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Which.
https://github.com/neilbowers/Module-Which
Adriano R. Ferreira, <ferreira@cpan.org>
Copyright (C) 2005-2007 by Adriano R. Ferreira
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Module::Which, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::Which
CPAN shell
perl -MCPAN -e shell install Module::Which
For more information on module installation, please visit the detailed CPAN module installation guide.