views:

492

answers:

7

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

Thanks in advance.

Update

Perhaps I should re-write the question to be: "How can I tell what modules were originally provided with the specific Perl installation on a machine?" (update: Actually I have now: http://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 installater 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, 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.

Update 2

Asked spin-off question: http://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? )

A: 
  • From coomand-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

codaddict
Keep in mind that these methods do not distinguish between Core modules and non-Core modules, as defined by perlmodlib. These methods will check for all installed modules.
toolic
A: 

you can use (eg, search for Net::FTP)

perl -MNet::FTP -e 1

if it doesn't have output, then its installed

Other resources

perldoc perlmodlib 
perldoc perllocal

A node from perlmonks

ghostdog74
Also, `perldoc -l Net::FTP` and check if it's in core-dir, site-dir, or vendor-dir?
ephemient
+28  A: 

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
toolic
Nice. I've used the CPAN shell "i" command to get info before, but corelist looks better. The "DSLIP_STATUS" for dual-lived modules doesn't seem trustworthy--some standard modules say "released" instead of "standard". For example, "i CGI" shows Stein's CPAN release (RdpOp released,developer,perl,object-oriented,Standard-Perl) even though it comes standard with Perl.
Ken Fox
Thanks but is Module::CoreList itself a core module that can be guaranteed to be present in any standard Perl installation?
Rob
Module::CoreList is a core module as of 5.009002 (2005-Apr-01), according to http://perldoc.perl.org/perl592delta.html. It should be guaranteed to be part of any properly installed Perl since then, but not before.
toolic
For practical purposes, "since 5.9.2" means "since 5.10.0" for normal people :)
hobbs
Thanks for answering toolic +1 for your comment.
Rob
I would like to accept your answer, toolic, but I am not able to yet as my system (Debian 4 Linux) reports the module as not found, as shown in the /var/log/apache2/error.log entry: [Wed Jan 13 10:40:23 2010] [error] [client 10.0.97.142] Can't locate Module/CoreList.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .) at /usr/lib/cgi-bin/check_dependencies.cgi line 73.It's raising an error about use Module::CoreList which is at line 73.
Rob
The very fact that this fundamental module checking utility function is in a module itself is unfortunate. Ideally this should be in the true core of Perl, in early versions, as a built-in function, like print, open, close, etc. Some background about my setup: It's just a standard Debian 4 installation with all options checked during the install for scripts, web etc. The log snippet shows I am running 5.8.8 which might explain the problem, given hobbs's comment.It's a pity I have to upgrade (some places have a policy not to take upgrading lightly due to (small) risk of breaking applications).
Rob
+1 for hobbs comment too for saying what version of Perl CoreList is in. If I didn't know this then I would be puzzled as to why things weren't working. Thanks.
Rob
Sorry for not being able to say it works. I would like to. But I can't at the moment given the outcomes so far. Something that is checking for whether other things are core/standard or not shouldn't itself be non-core/standard.
Rob
@Rob: It's still the correct answer. I think you misunderstand what that checkmark is there for.
hobbs
I'm accepting this answer. Sorry it took so long! perlmodlib looks like a good method too. But Module::CoreList is very explicit about what it is doing. Also it is clear in what Perl version this facility is available. Te be honest it should have been there at the very beginning, but only Larry Wall will have an answer for that; hindsight is wonderful thing. This is a disadvantage of Perl not being able to precisely know this answer for any version, something I believe other languages do better.
Rob
I have, however, asked a spin-off question that I hope will work with any Perl version: Asked spin-off question: http://stackoverflow.com/questions/2049735 ( How can I tell what modules were originally provided with the specific Perl installation on a machine? )
Rob
link should be: http://stackoverflow.com/questions/2085516/how-can-i-tell-what-modules-were-originally-provided-with-the-specific-perl-insta
Rob
+4  A: 

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
Greg Bacon
This answer looks more favourable than Module::CoreList because I'm assuming that perlmodlib is available as core/standard in earlier Perl versions where Module::CoreList is not available.
Rob
Update: Got error in my Debian 4 Linux /var/log/apache2/error.log log: [Wed Jan 13 11:05:15 2010] [error] [client 10.0.97.142] You need to install the perl-doc package to use this program.[Wed Jan 13 11:05:15 2010] [error] [client 10.0.97.142] cannot locate perlmodlib So I *have* to install something to get this to work! Again ditto my comments for toolic's answer: "Ideally this should be in the true core of Perl, in early versions, as a built-in function, like print, open, close, etc." =
Rob
To add to this: Something that is checking for whether other things are core/standard or not shouldn't itself be non-core/standard.
Rob
Sorry for not being able to say it works. I would like to. But I can't at the moment given the outcomes so far.
Rob
It does work, but it requires the standard installation! :-)
Greg Bacon
gbacon, see what "brian d foy" says below about "standard installation" - apparently this does not exist. Certainly, I've done nothing special with my Perl install that came with Debian 4. Maybe Debian 5 improves on it but I can't assume all the machines I want to use this for will have that.
Rob
Also, see my update about a suggested re-write of the question: "how do I work out what came with a particular installation".
Rob
Perl's source tells us what's in the standard installation. That is beyond dispute, so let's move on to a more relevant question: what are you ultimately trying to do?
Greg Bacon
Fine. So how can I query what's in an installation on a particular machine? I've explained what I am ultimately trying to do in the Update to the original question.
Rob
@gbacon: without getting into heavy semantics, Debian's `perl` package is separate from its `perl-doc`. I think this is a mistake on Debian's part, but that doesn't change the facts. The `perl-doc` package is considered 'suggested'. On a standard Debian machine, packages marked as 'recommended' will automatically be installed, but 'suggested' packages will not. So, passing `--install-recommends` won't help. It's also pointless in general, since installing recommended packages is itself the Debian default. Details of Debian's `perl` package here: http://packages.debian.org/lenny/perl
Telemachus
Argh! With etch, `perl-doc` was recommended: http://packages.debian.org/etch/perl
Greg Bacon
Thank you folks for all your effort answering here with some very good points. I've accepted Module::CoreList as this facility is specific to what I am trying to achieve. It is not perfect though as it is only supported in later versions when it should be in everything.
Rob
+2  A: 

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

R. Bemrose
+1: do the simplest thing that could possibly work...
Telemachus
This answer satisfies neither requirement. It's not even the simplest thing that could work.
brian d foy
@brian d foy: Yes, I deliberately ignored those requirements because they were unnecessary. Now that the question has been updated, this answer still stands, because it still gives you a reasonable expectation of what modules *should* be in a default perl install without requiring extra modules to be installed.
R. Bemrose
+1 @R. Bemrose: I definitely agree that it is a "reasonable expectation"! I may post the updated question as a separate to give more visibility as it is different enough to benefit from some additional posters' answers.
Rob
+3  A: 

There really is no such thing as "core" anymore. There used to be a standard Perl distribution, but a lot of people don't have a standard Perl distribution. There's operating system modifies 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 it you can create files, you can install the the module. You can even pretend that you wrote it yourself.

brian d foy
Thanks +1. From the useful feedback from yourself and others I am beginning to conclude that there isn't a black and white simple answer to my question. I will have to tolerate that and work around it or ask a better question.
Rob
It's not really a case of can't use Module::CoreList it's more the case that I would strongly prefer not to if I can avoid it. I wanted to see if there was a built-in feature and was guaranteed to be present in every Perl installation, like keywords print, open, etc. are. This now appears unlikely.Ideally I would prefer not to have to change the state of the system (i.e. by installing the Module::CoreList) that I am checking core modules for. I don't want to change what I am measuring.
Rob
Certainly also agree that I can't "rely on the standard distribution being actually standard" and that "some linux distributions don't even include the Perl documentation as part of the base Perl installation": My Debian 4 distribution is nothing special, just the straight forward Debian install, which meant I couldn't try out the perlmodlib solution suggested here by another poster.
Rob
If you are writing programs, you are changing the state. Module::CoreList is just code. It doesn't really matter who wrote it.
brian d foy
Also, see my update about a suggested re-write of the question: "how do I work out what came with a particular installation".
Rob
That really should have been a new question.
brian d foy
A: 

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.

Telemachus