Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check whether a Perl module is installed? [duplicate]

Tags:

module

perl

I'm writing a small Perl script that depends on some modules that might be available, so during the installation I would have to check if everythings there. I could just write use some::module and see if an error comes up, but a short message like "You need to install some::module" would be more helpful for endusers.

I also could just search every directory in @INC, but as it's Perl, there has to be an easier way.

like image 691
tstenner Avatar asked Apr 29 '09 12:04

tstenner


People also ask

How do I check if a Perl module is installed in Unix?

If you're running ActivePerl under Windows: C:\>ppm query * to get a list of all installed modules. C:\>ppm query XML-Simple to check if XML::Simple is installed.

How do I check if a Perl module is installed Linux?

If the module is not installed however you'll see something like below output: [root@carbondev ~]# perl -e "use Soap::Lite" Can't locate Soap/Lite.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5 .)

How do I list installed Perl modules?

Answer: To list all the installed Perl modules in the system, use the following command. # perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.


3 Answers

How about:

die "Some::Module missing!" unless(eval{require Some::Module});
like image 147
heeen Avatar answered Oct 25 '22 10:10

heeen


perl -MSome::Module -e ';'

Whoops, misread the question. I thought you wanted to know in a one-off instance, not discovering it in a recoverable manner. I always use something like this:

sub try_load {
  my $mod = shift;

  eval("use $mod");

  if ($@) {
    #print "\$@ = $@\n";
    return(0);
  } else {
    return(1);
  }
}

Which you use like this:

$module = 'Some::Module';
if (try_load($module)) {
  print "loaded\n";
} else {
  print "not loaded\n";
}
like image 30
jj33 Avatar answered Oct 25 '22 09:10

jj33


I have a little script that lists all the Perl modules on my system;

#!/usr/bin/perl

use ExtUtils::Installed;
my $instmod = ExtUtils::Installed->new();
foreach my $module ($instmod->modules()) {
    my $version = $instmod->version($module) || "???";
    print "$module -- $version\n";
}

Inside that foreach loop you might want to do some thing like;

my $match;
if ($module =~ /$match/) {
  print "Found $match: $module\n";
}
like image 37
jeremiah Avatar answered Oct 25 '22 09:10

jeremiah