views:

200

answers:

5

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 Grid Engine system. Sometimes, even run as other username.

I can use CPAN.pm to install packages for myself, but it is not so easy to install for other users without chmod 666 on folders.

+11  A: 

perl -MTime::HiRes -e 'print $INC{"Time/HiRes.pm"}' or perldoc -l Time::HiRes

jrockway
perldoc only works for those setupped.the first works for all, but typing the name 2 times is a bit boring.
Galaxy
On a unixy system, I would just "locate Time/HiRes.pm".
Svante
If the locate database doesn't have it, for whatever reason, `find /usr/ -path '*/Time/HiRes.pm'`.
Svante
That doesn't tell you where Perl is looking for the file, though. My Perl modules are in ~/perl/install, for example.
jrockway
Well, in my situation, user files all in NFS path, and locate just exclude NFS.Since the `PERL5LIB` is a bit long on that system, `find` is not a good way.
Galaxy
+2  A: 

I just find another one: http://www.perlmonks.org/?node%5Fid=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
Galaxy
If you just want the version, it's probably easiest to type `perl -MYour::Module\ 999`. If the version number is less than 999, it will be printed.
jrockway
Okay, but no upvotes unless you can explain what that's doing.
Ether
Might it not be easier to just do this in Perl rather than wrapping a shell script around it?
Chris Lutz
+6  A: 

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)
    [email protected]

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)
    [email protected]

I think maybe I need an XML option like svn.

brian d foy
But, just everything THAT on CPAN.
Galaxy
I don't understand your comment.
brian d foy
What if you write a package such as FOO::Bar and put it in `PERL5LIB` ?
Galaxy
@Galaxy: it's kind of like asking "what happens if I cut my leg off with a hacksaw". You won't have a leg anymore. Duhhh.
jrockway
A: 

I use the script from http://www.perlmonks.org/?node%5Fid=697155

It gives you a page that you can search your @INC by a string, and outputs module information, version number, file location, and links to the appropriate CPAN page.

Oesor
A: 

If need to find which modules are actually used by your script you can use perl debuggers M command:

[ivan@server ~]$ perl -d your_script.pl
...

Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.

DB M
'AutoLoader.pm' => '5.60 from /usr/lib/perl5/5.8.8/AutoLoader.pm'
'Carp.pm' => '1.04 from /usr/lib/perl5/5.8.8/Carp.pm'
...

This will help in case when you have modules with same names but in different folder.

Ivan Nevostruev