How can I tell if a Perl module is core or part of the standard install?

PerlModuleStandard Library

Perl Problem Overview


How can I check if a Perl module is part of the core - i.e. it is part of the standard installation?

I'm looking for:

  • a command-line command:
  • a Perl subroutine/function to check within code

Perhaps the question should be: How can I tell what modules were originally provided with the specific Perl installation on a machine? (Actually, it is now asked as https://stackoverflow.com/questions/2085516/how-can-i-tell-what-modules-were-originally-provided-with-the-specific-perl-insta.)

Given that there now appears to not to be an overall Perl standard installation, at least the answer to this new question will tell me what I originally had in the installation when it was first installed.

With that knowledge and if I keep the original installer image/package OR know how to get the exact thing again online, then I have a repeatable Perl installation for several machines with the knowledge of what modules will be present and what modules will not.

To clarify further: I am looking at what came with the installation originally, what modules were provided as part of that installation, and what was built-in. NOT what has been installed since then.

And I want to be able to do this on the machine that has the installation. So for this I would be relying upon the installation to have a record in some form as to what it has originally.

I asked spin-off question: https://stackoverflow.com/questions/2085516/how-can-i-tell-what-modules-were-originally-provided-with-the-specific-perl-insta (How can I tell what modules were originally provided with the specific Perl installation on a machine?)

Perl Solutions


Solution 1 - Perl

The corelist command from the Module::CoreList module will determine if a module is Core or not.

> corelist Carp

Carp was first release with perl 5

> corelist XML::Twig

XML::Twig was not in CORE (or so I think)

Here is one way to use it in a script. The Module::CoreList POD is too terse -- you have to go hunting through the source code to find what methods to call:

use strict;
use warnings;
use Module::CoreList;

my $mod = 'Carp';
#my $mod = 'XML::Twig';
my @ms = Module::CoreList->find_modules(qr/^$mod$/);
if (@ms) {
    print "$mod in core\n";
}
else {
    print "$mod not in core\n";
}

__END__

Carp in core

Solution 2 - Perl

You could check perlmodlib in a sub:

my %_stdmod;
sub is_standard_module {
  my($module) = @_;

  unless (keys %_stdmod) {
    chomp(my $perlmodlib = `perldoc -l perlmodlib`);
    die "cannot locate perlmodlib\n" unless $perlmodlib;

    open my $fh, "<", $perlmodlib
      or die "$0: open $perlmodlib: $!\n";

    while (<$fh>) {
      next unless /^=head\d\s+Pragmatic\s+Modules/ ..
                  /^=head\d\s+CPAN/;

      if (/^=item\s+(\w+(::\w+)*)/) {
        ++$_stdmod{ lc $1 };
      }
    }
  }

  exists $_stdmod{ lc $module } ? $module : ();
}

Example usage:

die "Usage: $0 module..\n" unless @ARGV;

foreach my $mod (@ARGV) {
  my $stdmod = is_standard_module $mod;
  print "$0: $mod is ", ($stdmod ? "" : "not "), "standard\n";
}

Output:

$ ./isstdmod threads::shared AnyDBM_File CGI LWP::Simple
./isstdmod: threads::shared is standard
./isstdmod: AnyDBM_File is standard
./isstdmod: CGI is standard
./isstdmod: LWP::Simple is not standard

perldoc is most definitely part of the Perl's true core and standard installation. The source distribution for perl-5.10.1, for example, contains

  • perldoc.PL, generates perldoc as part of the standard installation
  • perlmodlib.PL, generates perlmodlib.pod as part of the standard installation

This is not a new addition. Perl-5.6.0, about ten years old, had perlmodlib as part of its true-core, standard installation.

Installations that do not contain these items are non-standard. Yes, I appreciate that it may seem academic from your perspective, but your vendor's packaging permitted a non-standard installation that breaks otherwise working programs.

With Debian's package manager, you can get the standard Perl installation with

$ apt-get --install-recommends install perl

Solution 3 - Perl

There really is no such thing as "core" any more. There used to be a standard Perl distribution, but a lot of people don't have a standard Perl distribution. Operating system distributions modify it by either adding or removing modules, changing modules, and so on. You can't rely on the standard distribution being actually standard. Some Linux distributions don't even include the Perl documentation as part of the base Perl installation.

You mention that you can't use Module::CoreList because it isn't core, but if you can create files, you can install the module. You can even pretend that you wrote it yourself.

Solution 4 - Perl

For the really lazy, there's the Core Modules list on the perldoc.perl.org website.

Solution 5 - Perl

You can use (for example, search for Net::FTP):

perl -MNet::FTP -e 1

If it doesn't have output, then it's installed.

Other resources
perldoc perlmodlib 
perldoc perllocal

A node from perlmonks

Solution 6 - Perl

In a response to a comment of Gbacon's, you say that you want the answer to be platform neutral. I don't know of such a solution, but I wonder if it's even the right way to go.

If your goal is to find out on specific machines, I would use the tools that come with the platform. On Debian, that would include dpkg (pre-installed on any Debian system) or apt-file (not pre-installed necessarily) or other APT tools.

As an example, take a look at the output of this:

dpkg-query -L perl | less

You would obviously need to parse the output, but it strikes me as a start precisely because it is specific to the machine in question.

Solution 7 - Perl

  • From the command-line:

    Let's say that you want to know whether module Tie::Hash is installed.
    To find out, execute the following from the command line:

     perl -MTie::Hash -e 1
    

    If you don't get any output from the above command then the module is installed; if you get an error, it's not installed.

  • For making this check from within the script you can make use of Module::Load::Conditional.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontherobyouknowView Question on Stackoverflow
Solution 1 - PerltoolicView Answer on Stackoverflow
Solution 2 - PerlGreg BaconView Answer on Stackoverflow
Solution 3 - Perlbrian d foyView Answer on Stackoverflow
Solution 4 - PerlPowerlordView Answer on Stackoverflow
Solution 5 - Perlghostdog74View Answer on Stackoverflow
Solution 6 - PerlTelemachusView Answer on Stackoverflow
Solution 7 - PerlcodaddictView Answer on Stackoverflow