Perl, Tutorials

How to find the list of installed modules in Perl

Sending
User Rating 5 (3 votes)

This is one of the questions asked in many Perl related job interviews. 
There will be some situations when you will need to know about installed modules, its version or you may need to check if required module is installed or not with mentioned version or higher than that.

Here are few ways through which you can achieve the result that I found over the internet from different websites.

1. To list all installed Perl modules using perl script

#!/usr/bin/perl 
use strict;
use warnings;
use ExtUtils::Installed; # By default this module will be available in Perl

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

2. To list installed Perl modules using command line in Linux box

Try instmodsh command in linux box
and then type l to list modules or m to give particular module name or q to quit

 $ instmodsh

Output:

Available commands are:
l - List all installed modules
m - Select a module
q - Quit the program
cmd?

At cmd? prompt type l to list all installed modules:

cmd? l

Output:

Installed modules are:
Archive::Tar
CPAN
Compress::Zlib
MIME::Lite
Module::Build
Net::Telnet
PAR::Dist
Perl
Spiffy
Test::Base
Test::Simple
XML::Simple
cmd?

This command itself is a perl script that use ExtUtils::Installed module. Try following command to see its source code:

$ vi $(which instmodsh)

For more details, visit this link: http://perldoc.perl.org/instmodsh.html

3. To compare and check installed Perl modules with respect to given modules with its version

#!/usr/bin/perl
use strict;
use warnings;
my @modules = (['SOAP::Lite', 0.50],
 ['IO::Socket', 0],
 ['HTML::Parser', 3.26],
 ['LWP', 5.65],
 );

print "\n",
 "==========================\n",
 "Testing for the given Perl modules\n",
 "===========================\n";
 
 for my $arr_ref (@modules) {
   my($mod, $ver) = @$arr_ref;
   print "$mod" . ($ver ? " (version >= $ver)" : "") . "\n";
 
   eval("use $mod" . ($ver ? " $ver;" : ";"));
   if ($@) {
     my $msg = $ver ?
     "\tEither it doesn't have $mod installed,\n" .
     "\tor the version that is installed is too old.\n" :
     "\tIt does NOT have $mod installed.\n";
     print "$msg\n";
   }
   else {
     print "\tYour system has $mod installed " .
     "with version @{[ $mod->VERSION ]}\n\n";
   }
 }

print "Module Testing Done!\n";

Note: If you are putting module name dynamically, then use  @{[ $mod->VERSION ]} for version number
else $Module-Name::VERSION ex: $SOAP::Lite::VERSION

print "SOAP::Lite Version is ". $SOAP::Lite::VERSION."\n";
#or
print "$mod version is @{[ $mod->VERSION ]} \n";

4. To list Perl modules that comes preinstalled with the standard Perl package and installed from outside

perldoc perlmodlib #lists all the modules that come with standard Perl package already
perldoc perllocal #lists all modules that is installed from outside

Both modules reveal lots of information about each installed modules like installation date, directory, module version etc.

Share your Thoughts