How do I get a list of installed CPAN modules?

后端 未结 29 2082
盖世英雄少女心
盖世英雄少女心 2020-12-04 07:41

Aside from trying

perldoc 

individually for any CPAN module that takes my fancy or going through the file system and loo

相关标签:
29条回答
  • 2020-12-04 07:41
    perl -MFile::Find=find -MFile::Spec::Functions -Tlwe 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC'
    
    0 讨论(0)
  • 2020-12-04 07:41

    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.

    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.

    0 讨论(0)
  • 2020-12-04 07:43

    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.

    0 讨论(0)
  • 2020-12-04 07:45

    This works for me

    perl -e 'print join("\n",@INC,"")'
    
    0 讨论(0)
  • 2020-12-04 07:46

    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).

    0 讨论(0)
  • 2020-12-04 07:46

    The following worked for me.

    $ perldoc perllocal | grep Module
    $ perldoc perllocal | grep -E 'VERSION|Module'
    
    0 讨论(0)
提交回复
热议问题