开发者

How can I find out where a Perl module is installed?

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 m开发者_StackOverflow社区yself, but it is not so easy to install for other users without chmod 666 on folders.


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


Mostly I use perldoc to get a location:

$ perldoc -l Module

You can also 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.


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

Installation

Prerequisites: Linux, macOS, or Windows, with Perl v5.4.50 or higher installed.

Installation from the npm registry

With Node.js or io.js installed, install the package as follows:

[sudo] npm install whichpm -g

Manual installation (macOS and Linux)

  • Download the CLI as whichpm.
  • Make it executable with chmod +x whichpm.
  • Move it or symlink it to a folder in your $PATH, such as /usr/local/bin (OSX) or /usr/bin (Linux).


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.


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


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


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.


Perldoc -l works for me

perldoc -l "File::Find"
/opt/perl_32/lib/5.8.8/File/Find.pm


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


In OSX you can use:

perl -e 'print join("\n",@INC)'

The result should be the location of your lib.

Then add this code in your Perl code:

use lib '/your/folder/location/to/lib';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜