如何找到Perl模块的安装位置?

时间:2009-10-13 02:39:25

标签: perl path module config

如何按名称获取已安装的Perl模块的路径, 例如Time::HiRes

我想要这只是因为我必须在SGE Grid Engine系统的不同节点上运行我的perl脚本。有时,甚至以其他用户名运行。

我可以使用CPAN.pm为自己安装软件包,但是对于没有文件夹上的chmod 666的其他用户来说安装起来并不容易。

9 个答案:

答案 0 :(得分:72)

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

答案 1 :(得分:21)

您可以使用Perl附带的cpan工具获取模块详细信息:

$ 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

它甚至适用于您尚未安装的模块:

$ 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

我想我可能需要像svn这样的XML选项。

答案 2 :(得分:15)

I've created whichpm, a cross-platform CLI (Linux, OSX, 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, OSX, 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 (OSX 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).

答案 3 :(得分:5)

如果需要查找脚本实际使用的模块,可以使用perl调试器M命令:

[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'
...

如果模块具有相同的名称但位于不同的文件夹中,这将有所帮助。

答案 4 :(得分:4)

我找到另一个: 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" 

当您运行perl -MSTH::STH

时,脚本只会打印%INC中的所有内容

例如:

$ 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

答案 5 :(得分:3)

我喜欢使用V模块。

只需从CPAN安装它,或者在Debian或Ubuntu上安装包libv-perl

然后像这样使用它:

$ perl -MV=DBI
DBI
    /Users/michiel/.plenv/versions/5.24.0/lib/perl5/site_perl/5.24.0/darwin-2level/DBI.pm: 1.636

其他输出示例:

$ perl -MV=Time::HiRes
Time::HiRes
    /usr/lib/perl/5.18/Time/HiRes.pm: 1.9725

答案 6 :(得分:1)

似乎最简单的方法是perldoc -l Time::HiRes

如果由于某种原因无法提供,这是一个务实的解决方案:

步骤1:在脚本中实例化模块......

#! /usr/bin/perl -w
use Time::HiRes();
new Time::HiRes();

步骤2:使用Perl图形化调试器执行脚本...

export PERL5LIB=$PERL5LIB:~/perl ## tell perl where to look for "Devel"/"ptkdb.pm"
perl -d:ptkdb (yourscript.pl)

第3步:Step innew来电。

模块的完整路径名将显示在调试器窗口的标题栏上。

另一种可能有用的方法是搜索$PERL5LIB中的所有文件夹。

答案 7 :(得分:0)

要扩展@Ivan的答案,使之无需安装其他软件即可运行,下面将使用Perl的调试器来查找特定模块(一个或多个):

perl -de 'use <Module Name>;'

例如:

perl -de 'use DBD::Oracle;'

输出:

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

答案 8 :(得分:0)

在OSX中,您可以使用:

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

结果应该是您的lib的位置。

然后将此代码添加到您的Perl代码中:

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