How do get the path of a installed Perl module by name,
e.g. Time::HiRes
?
I want this just because I have to run my perl script on different nodes of a SGE
I just find another one: http://www.perlmonks.org/?node_id=568730
#!/bin/sh
echo 'print map { sprintf( "%20s : %s\n", $_, $INC{$_} ) } sort keys %INC; print "\n'$1' version : $'$1'::VERSION\n\n"' | perl "-M$1"
the script just print out everything in %INC when you run perl -MSTH::STH
eg:
$ whichpm CGI
CGI.pm : /System/Library/Perl/5.8.6/CGI.pm
CGI/Util.pm : /System/Library/Perl/5.8.6/CGI/Util.pm
Carp.pm : /System/Library/Perl/5.8.6/Carp.pm
Exporter.pm : /System/Library/Perl/5.8.6/Exporter.pm
constant.pm : /System/Library/Perl/5.8.6/constant.pm
overload.pm : /System/Library/Perl/5.8.6/overload.pm
strict.pm : /System/Library/Perl/5.8.6/strict.pm
vars.pm : /System/Library/Perl/5.8.6/vars.pm
warnings.pm : /System/Library/Perl/5.8.6/warnings.pm warnings/register.pm : /System/Library/Perl/5.8.6/warnings/register.pm
CGI version : 3.05
It seems like the simplest way is perldoc -l Time::HiRes
.
If that isn't available for some reason, here's a pragmatic solution:
Step 1: Instantiate the module in your script...
#! /usr/bin/perl -w
use Time::HiRes();
new Time::HiRes();
Step 2: Execute the script with the Perl graphical debugger...
export PERL5LIB=$PERL5LIB:~/perl ## tell perl where to look for "Devel"/"ptkdb.pm"
perl -d:ptkdb (yourscript.pl)
Step 3: Step in
to the new
call.
The full pathname of the module will be displayed on the title-bar of the debugger window.
Another approach that might be useful would be to search all of the folders in $PERL5LIB
.
You can get module details with the cpan
tool that comes with Perl:
$ cpan -D Time::HiRes
Time::HiRes
-------------------------------------------------------------------------
High resolution time, sleep, and alarm
J/JH/JHI/Time-HiRes-1.9719.tar.gz
/usr/local/perls/perl-5.10.0/lib/5.10.0/darwin-2level/Time/HiRes.pm
Installed: 1.9711
CPAN: 1.9719 Not up to date
Andrew Main (Zefram) (ZEFRAM)
zefram@fysh.org
It even works on modules that you haven't installed:
$ cpan -D Win32::Process
Win32::Process
-------------------------------------------------------------------------
Interface to Win32 Process functions
J/JD/JDB/Win32-Process-0.14.tar.gz
Installed:
CPAN: 0.14 Not up to date
Jan Dubois (JDB)
jand@activestate.com
I think maybe I need an XML option like svn.
To expand on @Ivan's answer that allows this to be run without installing additional software the following will use Perl's debugger to find a specific module (or modules):
perl -de 'use <Module Name>;'
For Example:
perl -de 'use DBD::Oracle;'
Output:
Loading DB routines from perl5db.pl version 1.37
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
DBD::Oracle::CODE(0x27f81d8)(/usr/local/lib64/perl5/DBD/Oracle.pm:113):
113: $ENV{PERL_BADFREE} = 0;
DB<1> q
Note: This solution proposes use of a (self-authored) utility that you must download. While it offers what I believe to be helpful features, installing a third-party solution first is not an option for everyone.
I've created whichpm, a cross-platform CLI (Linux, macOS, Window) that locates installed Perl modules by module (package) name, and optionally reports information about them, including detection of accidental duplicates.
Examples
# Locate the Data::Dumper module.
$ whichpm Data::Dumper
/usr/lib/perl/5.18/Data/Dumper.pm
# 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
# Locate the Data::Dumper module and open it in your system's default text
# editor.
$ whichpm -e Data::Dumper
# Look for accidental duplicates of the Foo::Bar module.
# Normally, only 1 path should be returned.
$ whichpm -a Foo::Bar
/usr/lib/perl/5.18/Foo/Bar.pm
./Foo/Bar.pm
# Print the paths of all installed modules.
$ whichpm -a
Prerequisites: Linux, macOS, or Windows, with Perl v5.4.50 or higher installed.
With Node.js or io.js installed, install the package as follows:
[sudo] npm install whichpm -g
whichpm
.chmod +x whichpm
.$PATH
, such as /usr/local/bin
(OSX) or /usr/bin
(Linux).I like to use the V module.
Just install it from CPAN or by installing the package libv-perl
on Debian or Ubuntu.
Then use it like this:
$ perl -MV=DBI
DBI
/Users/michiel/.plenv/versions/5.24.0/lib/perl5/site_perl/5.24.0/darwin-2level/DBI.pm: 1.636
Other output example:
$ perl -MV=Time::HiRes
Time::HiRes
/usr/lib/perl/5.18/Time/HiRes.pm: 1.9725