tags:

views:

4867

answers:

10

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.

What's the easiest way to just get a big list of every CPAN module installed? From the command line or otherwise.

+13  A: 
perldoc perllocal

Edit: There's a (little) more info about it in the CPAN FAQ

Dan
Thanks for the link to the FAQ. Unfortunately not all the modules I know are installed are returning. Date::Calc doesn't even show up there.
David McLaughlin
Did you install that with your OS package management?
Dan
I don't install the modules. I'm just the software dev in a pretty tightly controlled enterprise perl set-up.
David McLaughlin
+3  A: 

I like to use the CPAN 'r' command for this. You can get into the CPAN shell with the old style:

sudo perl -MCPAN -e shell

or, on most newer systems, there is a 'cpan' command, so this command will get you to the 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.)

Then, once you're in the cpan shell, you can use the 'r' command to report all installed modules and their versions. So, at the "cpan>" prompt, type 'r'. This will list all installed modules and their versions. Use '?' to get some more help.

amoore
That's pretty cool - never knew it existed.
Dan
actually 'r' gives you the reinstall recommendations - ie all the modules on your install that have a newer version of CPAN. Unless your install is very out of date this will not be a complete list.
EvdB
My 'r' always reports next to nothing because I upgrade compulsively.Which reminds me ... I haven't upgraded today, yet.
skiphoppy
The -r recompiles stuff. To get a list, try -a or download the latest sources and play with the new -l switch, added for just this answer. :)
brian d foy
Why the `sudo` here? It’s completely unnecessary.
Aristotle Pagaltzis
note the difference between the '-r' commandline argument to 'cpan', and the 'r' command in the cpan shell :)
EvdB
+3  A: 

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.


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

shelfoo
This will not give him what he wants. It won't group related files by the distribution they are part of, and it will list all the core *.pm files from Perl itself.
rjray
+10  A: 

It's worth noting that perldoc perllocal will only report on modules installed via 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).

Regrettably, the solution appears to be walking through @INC with File::Find or something similar. 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). 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).

Ovid
I don't think I need to pull out the BackPAN catalog guns for that one. Something like I do with Test::Prereq to collapse it by what distro it finds in 02packages might be enough. That is a little tricker than just listing the module files though, and the catalog isn't close to handling that yet.
brian d foy
From a rudimentary test (doing a "make -n install" in some dirs I have laying around), the "make install" step of any MakeMaker-based module will update perllocal. It's not specific to installing via CPAN directly.
rjray
+8  A: 
brian d foy
+1  A: 

To walk through the @INC directory trees without using an external program like ls(1), one could use the File::File::Rule module, which has a nice declarative interface.

Also, you want to filter out duplicates in case previous perl versions contain the same modules. 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. 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.

dland
+15  A: 

This is answered in the Perl FAQ, the answer which can be quickly found with 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.

You can also find the FAQ entry "How do I find which modules are installed on my system?" in perlfaq3. You can see a list of all FAQ answers by looking in perlfaq

pjf
+1  A: 

Try perldiver. It's a CGI script that will list all the Perl modules.

+1  A: 

Here is yet another command-line tool to list all installed .pm files:

Find installed Perl modules matching a regular expression

  • Portable (only uses core modules)
  • Cache option for faster look-up's
  • Configurable display options
toolic
+1  A: 

I can suggest using the pmtools, especially pminst which accepts regular expressions.

ssspiochld