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.
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.
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 .)
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 /\.
How about:
die "Some::Module missing!" unless(eval{require Some::Module});
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";
}
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With