简体   繁体   English

如何获取已安装 CPAN 模块的列表?

[英]How do I get a list of installed CPAN modules?

Aside from trying除了尝试

perldoc <module name>

individually for any CPAN module that takes my fancy or going through the file system and looking at the directories I have no idea what modules we have installed.单独为任何我喜欢的 CPAN 模块或通过文件系统查看目录我不知道我们安装了哪些模块。

What's the easiest way to just get a big list of every CPAN module installed?获取每个已安装 CPAN 模块的大列表的最简单方法是什么? From the command line or otherwise.从命令行或其他方式。

This is answered in the Perl FAQ, the answer which can be quickly found with perldoc -q installed . Perl FAQ 中回答了这个问题,可以通过perldoc -q installed快速找到答案。 In short, it comes down to using ExtUtils::Installed or using File::Find , variants of both of which have been covered previously in this thread.简而言之,它归结为使用ExtUtils::Installed或使用File::Find ,这两种方法的变体都已在此线程中介绍过。

You can also find the FAQ entry "How do I find which modules are installed on my system?"您还可以找到常见问题解答条目“如何查找我的系统上安装了哪些模块?” in perlfaq3.在 perlfaq3 中。 You can see a list of all FAQ answers by looking in perlfaq您可以通过查看perlfaq查看所有常见问题解答的列表

perldoc perllocal

编辑: CPAN 常见问题解答中有关于它的(少量)更多信息

perldoc -q installed

claims that cpan -l will do the trick, however it's not working for me.声称cpan -l可以解决问题,但它对我不起作用。 The other option:另一种选择:

cpan -a

does spit out a nice list of installed packages and has the nice side effect of writing them to a file.确实吐出了一个很好的已安装软件包列表,并且具有将它们写入文件的不错的副作用。

$ for M in `perldoc -t perllocal|grep Module |sed -e 's/^.*" //'`; do V=`perldoc -t perllocal|awk "/$M/{y=1;next}y" |grep VERSION |head -n 1`; printf "%30s %s\n" "$M" "$V"; done |sort
              Class::Inspector     *   "VERSION: 1.28"
                    Crypt::CBC     *   "VERSION: 2.33"
               Crypt::Rijndael     *   "VERSION: 1.11"
                    Data::Dump     *   "VERSION: 1.22"
                   DBD::Oracle     *   "VERSION: 1.68"
                           DBI     *   "VERSION: 1.630"
                   Digest::SHA     *   "VERSION: 5.92"
           ExtUtils::MakeMaker     *   "VERSION: 6.84"
                       install     *   "VERSION: 6.84"
               IO::SessionData     *   "VERSION: 1.03"
               IO::Socket::SSL     *   "VERSION: 2.016"
                          JSON     *   "VERSION: 2.90"
                  MIME::Base64     *   "VERSION: 3.14"
                  MIME::Base64     *   "VERSION: 3.14"
                   Mozilla::CA     *   "VERSION: 20141217"
                   Net::SSLeay     *   "VERSION: 1.68"
                        parent     *   "VERSION: 0.228"
                  REST::Client     *   "VERSION: 271"
                    SOAP::Lite     *   "VERSION: 1.08"
                  Task::Weaken     *   "VERSION: 1.04"
                 Term::ReadKey     *   "VERSION: 2.31"
                Test::Manifest     *   "VERSION: 1.23"
                  Test::Simple     *   "VERSION: 1.001002"
                  Text::CSV_XS     *   "VERSION: 1.16"
                     Try::Tiny     *   "VERSION: 0.22"
                   XML::LibXML     *   "VERSION: 2.0108"
         XML::NamespaceSupport     *   "VERSION: 1.11"
                XML::SAX::Base     *   "VERSION: 1.08"

It's worth noting that perldoc perllocal will only report on modules installed via CPAN.值得注意的是 perldoc perllocal 只会报告通过 CPAN 安装的模块。 If someone installs modules manually, it won't find them.如果有人手动安装模块,它不会找到它们。 Also, if you have multiple people installing modules and the perllocal.pod is under source control, people might resolve conflicts incorrectly and corrupt the list (this has happened here at work, for example).此外,如果您有多个人安装模块并且 perllocal.pod 处于源代码控制之下,人们可能会错误地解决冲突并破坏列表(例如,这在工作中发生过)。

Regrettably, the solution appears to be walking through @INC with File::Find or something similar.遗憾的是,解决方案似乎是通过 @INC 使用 File::Find 或类似的东西。 However, that doesn't just find the modules, it also finds related modules in a distribution.但是,这不仅会找到模块,还会在分发中找到相关的模块。 For example, it would report TAP::Harness and TAP::Parser in addition to the actual distribution name of Test::Harness (assuming you have version 3 or above).例如,它会报告 TAP::Harness 和 TAP::Parser 以及 Test::Harness 的实际分发名称(假设您有版本 3 或更高版本)。 You could potentially match them up with distribution names and discard those names which don't match, but then you might be discarding locally built and installed modules.您可能会将它们与分发名称匹配并丢弃那些不匹配的名称,但是您可能会丢弃本地构建和安装的模块。

I believe brian d foy's backpan indexing work is supposed to have code to hand it at .pm file and it will attempt to infer the distribution, but even this fails at times because what's in a package is not necessarily installed (see Devel::Cover::Inc for an example).我相信 brian d foy 的 backpan 索引工作应该有代码在 .pm 文件中处理它,它会尝试推断分布,但即使这有时也会失败,因为不一定安装包中的内容(参见 Devel::Cover ::Inc 为例)。

You can try ExtUtils-Installed , but that only looks in .packlist s, so it may miss modules that people moved things into @INC by hand.您可以尝试ExtUtils-Installed ,但它只在.packlist中查找,因此它可能会错过人们手动将东西移动到@INC的模块。

I wrote App-Module-Lister for a friend who wanted to do this as a CGI script on a non-shell web hosting account.我为一位想在非 shell 网络托管帐户上将其作为 CGI 脚本执行此操作的朋友编写了App-Module-Lister You simple take the module file and upload it as a filename that your server will treat as a CGI script.您只需获取模块文件并将其作为文件名上传,您的服务器会将其视为 CGI 脚本。 It has no dependencies outside of the Standard Library.它在标准库之外没有依赖项。 Use it as is or steal the code.按原样使用它或窃取代码。

It outputs a list of the modules and their versions:它输出模块列表及其版本:

Tie::Cycle      1.15
Tie::IxHash     1.21
Tie::Toggle     1.07
Tie::ToObject   0.03
Time::CTime     99.062201
Time::DaysInMonth       99.1117
Time::Epoch     0.02
Time::Fuzzy     0.34
Time::JulianDay 2003.1125
Time::ParseDate 2006.0814
Time::Timezone  2006.0814

I've been meaning to add this as a feature to the cpan tool, so I'll do that too.我一直打算将此作为一项功能添加到cpan工具中,所以我也会这样做。 [Time passes] And, now I have a -l switch in cpan . [时间流逝]而且,现在我在cpan中有一个-l开关。 I have a few other things to do with it before I make a release, but it's in github .在发布之前,我还有其他一些事情要做,但它在 github 中 If you don't want to wait for that, you could just try the -a switch to create an autobundle, although that puts some Pod around the list.如果您不想等待,您可以尝试使用-a开关来创建自动捆绑包,尽管这会将一些 Pod 放在列表中。

Good luck;祝你好运;

Here a script which would do the trick:这是一个可以解决问题的脚本:

use ExtUtils::Installed;

my $inst = ExtUtils::Installed->new();
my @modules = $inst->modules();
foreach $module (@modules){
       print $module ." - ". $inst->version($module). "\n";
}

=head1 ABOUT

This scripts lists installed cpan modules using the ExtUtils modules

=head1 FORMAT

Prints each module in the following format
<name> - <version>

=cut

I like to use the CPAN 'r' command for this.我喜欢为此使用 CPAN 'r' 命令。 You can get into the CPAN shell with the old style:您可以使用旧样式进入 CPAN shell:

sudo perl -MCPAN -e shell

or, on most newer systems, there is a 'cpan' command, so this command will get you to the shell:或者,在大多数较新的系统上,有一个“cpan”命令,所以这个命令会让你进入 shell:

sudo cpan

(You typically have to use 'sudo' to run it as root, or use 'su -' to become root before you run it, unless you have cpan set up to let you run it as a normal user, but install as root. If you don't have root on this machine, you can still use the CPAN shell to find out this information, but you won't be able to install modules, and you may have to go through a bit of setup the first time you run it.) (您通常必须使用 'sudo' 以 root 身份运行它,或者在运行它之前使用 'su -' 成为 root,除非您已将 cpan 设置为让您以普通用户身份运行它,但以 root 身份安装。如果您在这台机器上没有 root,您仍然可以使用 CPAN shell 来查找此信息,但您将无法安装模块,并且您可能需要在第一次安装时进行一些设置运行。)

Then, once you're in the cpan shell, you can use the 'r' command to report all installed modules and their versions.然后,一旦您进入 cpan shell,您可以使用“r”命令报告所有已安装的模块及其版本。 So, at the "cpan>" prompt, type 'r'.因此,在“cpan>”提示符下,输入“r”。 This will list all installed modules and their versions.这将列出所有已安装的模块及其版本。 Use '?'利用 '?' to get some more help.获得更多帮助。

perl -MFile::Find=find -MFile::Spec::Functions -Tlwe 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'

You can get list of perl modules installed in you system by using instmodsh command in your terminal.It will ask you three option in order to enhance the output they are:您可以通过在终端中使用instmodsh命令获取系统中安装的 perl 模块列表。它会询问您三个选项以增强它们的输出:

   l            - List all installed modules
   m <module>   - Select a module
   q            - Quit the program

On Linux/Unix I use this simple command:在 Linux/Unix 上,我使用这个简单的命令:

perl -e 'print qx/find $_ -name "*.pm"/ foreach ( @INC );' 

It scans all folder in @INC and looks for any *.pm file.它扫描@INC中的所有文件夹并查找任何 *.pm 文件。

Here's a really hacky way to do it in *nix, you'll get some stuff you don't really care about (ie: warnings::register etc), but it should give you a list of every .pm file that's accessible via perl.这是在 *nix 中执行此操作的一种非常老套的方法,您会得到一些您并不真正关心的东西(即:warnings::register 等),但它应该为您提供可通过以下方式访问的每个 .pm 文件的列表perl.


for my $path (@INC) {
    my @list = `ls -R $path/**/*.pm`;
    for (@list) {
        s/$path\///g;
        s/\//::/g;
        s/\.pm$//g;
        print;
    }
}

Try the following command试试下面的命令

instmodsh

With l you will List all installed modules .使用l您将列出所有已安装的模块

From man page :手册页

A shell to examine installed modules.用于检查已安装模块的外壳。
A little interface to ExtUtils::Installed to examine installed modules, validate your packlists and even create a tarball from an installed module. ExtUtils::Installed 的一个小接口,用于检查已安装的模块、验证您的包列表,甚至从已安装的模块创建一个 tarball。

Here's a script by @JamesThomasMoon1979 rewritten as a one-liner这是@JamesThomasMoon1979 重写为单行的脚本

perl -MExtUtils::Installed -e '$i=ExtUtils::Installed->new(); 
      print "$_ ".$i->version($_)."\n" for $i->modules();'

All those who can't install perldoc, or other modules, and want to know what modules are available (CPAN or otherwise), the following works for linux and Mingw32/64:所有无法安装 perldoc 或其他模块,并想知道哪些模块可用(CPAN 或其他)的人,以下适用于 linux 和 Mingw32/64:

grep -RhIP '^package [A-Z][\w:]+;' `perl -e 'print join " ",@INC'` | sed 's/package //' | sort | uniq

Yes, it's messy.是的,很乱。 Yes, it probably reports more than you want.是的,它可能报告的比你想要的多。 But if you pipe it into a file, you can easily check for, say, which dbm interfaces are present:但是,如果将其通过管道传输到文件中,则可以轻松地检查存在哪些 dbm 接口:

 grep -RhIP '^package [A-Z][\w:]+;' `perl -e 'print join " ",@INC'` | sed 's/package //' | sort | uniq > modules-installed
 cat modules-installed | grep -i dbm 

AnyDBM_File;
Memoize::AnyDBM_File;
Memoize::NDBM_File;
Memoize::SDBM_File;
WWW::RobotRules::AnyDBM_File;

Which is why I ended up on this page (disappointed)这就是我最终出现在此页面上的原因(失望)

(I realise this doesn't answer the OP's question exactly, but I'm posting it for anybody who ended up here for the same reason I did. That's the problem with stack*** it's almost imposisble to find the question you're asking, even when it exists, yet stack*** is nearly always google's top hit!) (我意识到这并不能完全回答 OP 的问题,但我将它发布给任何最终出于同样原因来到这里的人。这就是堆栈的问题***几乎不可能找到你的问题问,即使它存在,但 stack*** 几乎总是谷歌的热门!)

The answer can be found in the Perl FAQ list .答案可以在Perl FAQ 列表中找到。

You should skim the excellent documentation that comes with Perl你应该浏览一下 Perl 附带的优秀文档

perldoc perltoc

尝试man perllocalperldoc perllocal

To walk through the @INC directory trees without using an external program like ls(1), one could use the File::Find::Rule module, which has a nice declarative interface.要在不使用 ls(1) 之类的外部程序的情况下遍历 @INC 目录树,可以使用File::Find::Rule模块,它有一个很好的声明式接口。

Also, you want to filter out duplicates in case previous Perl versions contain the same modules.此外,如果以前的 Perl 版本包含相同的模块,您希望过滤掉重复项。 The code to do this looks like:执行此操作的代码如下所示:

#! /usr/bin/perl -l

use strict;
use warnings;
use File::Find::Rule;

my %seen;
for my $path (@INC) {
    for my $file (File::Find::Rule->name('*.pm')->in($path)) {
        my $module = substr($file, length($path)+1);
        $module =~ s/.pm$//;
        $module =~ s{[\\/]}{::}g;
        print $module unless $seen{$module}++;
    }
}

At the end of the run, you also have all your module names as keys in the %seen hash.在运行结束时,您还将所有模块名称作为 %seen 哈希中的键。 The code could be adapted to save the canonical filename (given in $file) as the value of the key instead of a count of times seen.可以修改代码以将规范文件名(在 $file 中给出)保存为键的值,而不是看到的次数。

Here is yet another command-line tool to list all installed .pm files:这是另一个列出所有已安装 .pm 文件的命令行工具:

Find installed Perl modules matching a regular expression查找与正则表达式匹配的已安装 Perl 模块

  • Portable (only uses core modules)便携(仅使用核心模块)
  • Cache option for faster look-up's用于更快查找的缓存选项
  • Configurable display options可配置的显示选项

The following worked for me.以下对我有用。

$ perldoc perllocal | grep Module
$ perldoc perllocal | grep -E 'VERSION|Module'

I wrote a perl script just yesterday to do exactly this.我昨天写了一个 perl 脚本来做到这一点。 The script returns the list of perl modules installed in @INC using the '::' as the separator.该脚本使用 '::' 作为分隔符返回安装在 @INC 中的 perl 模块列表。 Call the script using -使用 - 调用脚本

perl perlmod.pl

OR或者

perl perlmod.pl <module name> #Case-insensitive(eg. perl perlmod.pl ftp)

As of now the script skips the current directory('.') since I was having problems with recursing soft-links but you can include it by changing the grep function in line 17 from截至目前,脚本跳过当前目录('.'),因为我在递归软链接方面遇到问题,但您可以通过将第 17 行中的 grep 函数从

  grep { $_ !~ '^\.$' } @INC

to just,只是,

@INC

The script can be found here.可以在此处找到该脚本。

cd /the/lib/dir/of/your/perl/installation
perldoc $(find . -name perllocal.pod)

Windows users just do a Windows Explorer search to find it. Windows 用户只需执行 Windows 资源管理器搜索即可找到它。

Try "perldoc -l":试试“perldoc -l”:

$ perldoc -l Log::Dispatch /usr/local/share/perl/5.26.1/Log/Dispatch.pm $ perldoc -l Log::Dispatch /usr/local/share/perl/5.26.1/Log/Dispatch.pm

the Perl cookbook contains several iterations of a script "pmdesc" that does what you want. Perl 食谱包含脚本“pmdesc”的多次迭代,可以满足您的需求。 Google-search for "Perl Cookbook pmdesc" and you'll find articles on other Q&A Sites , several code listings on the net, a discussion of the solution, and even some refinements .谷歌搜索“Perl Cookbook pmdesc”,你会发现其他问答网站上的文章、网上的一些代码清单、解决方案的讨论,甚至一些改进

Here's a Perl one-liner that will print out a list of installed modules:这是一个 Perl 单行程序,它将打印出已安装模块的列表:

perl -MExtUtils::Installed -MData::Dumper -e  'my ($inst) = ExtUtils::Installed->new(); print Dumper($inst->modules());'

Just make sure you have Data::Dumper installed.只需确保您已安装 Data::Dumper。

使用perldoc -q已安装以获取更多信息或输入

perldoc <Module::Name>

As you enter your Perl script you have all the installed modules as .pm files below the folders in @INC so a small bash script will do the job for you:当您输入 Perl 脚本时,所有已安装的模块都以 .pm 文件的形式出现在 @INC 中的文件夹下,因此一个小的 bash 脚本将为您完成这项工作:

#!/bin/bash

echo -e -n "Content-type: text/plain\n\n"

inc=`perl -e '$, = "\n"; print @INC;'`

for d in $inc
do
   find $d -name '*.pm'
done

对于 Linux,最简单的获取方法是,

dpkg -l | grep "perl"

Aside from trying除了尝试

perldoc <module name>

individually for any CPAN module that takes my fancy or going through the file system and looking at the directories I have no idea what modules we have installed.单独用于任何我喜欢的 CPAN 模块或浏览文件系统并查看目录我不知道我们安装了哪些模块。

What's the easiest way to just get a big list of every CPAN module installed?获取已安装的每个 CPAN 模块的大列表的最简单方法是什么? From the command line or otherwise.从命令行或其他方式。

Aside from trying除了尝试

perldoc <module name>

individually for any CPAN module that takes my fancy or going through the file system and looking at the directories I have no idea what modules we have installed.单独用于任何我喜欢的 CPAN 模块或浏览文件系统并查看目录我不知道我们安装了哪些模块。

What's the easiest way to just get a big list of every CPAN module installed?获取已安装的每个 CPAN 模块的大列表的最简单方法是什么? From the command line or otherwise.从命令行或其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM