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
[perl]
#!/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";
}
[/perl]
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
[code] $ instmodsh[/code]
Output:
[code]Available commands are:
l – List all installed modules
m – Select a module
q – Quit the program
cmd?[/code]
At cmd? prompt type l to list all installed modules:
[code]cmd? l[/code]
Output:
[code]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?
[/code]
This command itself is a perl script that use ExtUtils::Installed module. Try following command to see its source code:
[code]$ vi $(which instmodsh)[/code]
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
[perl]
#!/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";
[/perl]
Note: If you are putting module name dynamically, then use @{[ $mod->VERSION ]} for version number
else $Module-Name::VERSION ex: $SOAP::Lite::VERSION
[perl]
print "SOAP::Lite Version is ". $SOAP::Lite::VERSION."\n";
#or
print "$mod version is @{[ $mod->VERSION ]} \n";
[/perl]
4. To list Perl modules that comes preinstalled with the standard Perl package and installed from outside
[perl]
perldoc perlmodlib #lists all the modules that come with standard Perl package already
perldoc perllocal #lists all modules that is installed from outside
[/perl]
Both modules reveal lots of information about each installed modules like installation date, directory, module version etc.
Welcome to Perlistan world! I have been using Perl (Perl 5.10 specifically) from last 1 year and I am really fond of this programming language. There are hell lots of features exist for Perl but I have written only few which I felt is important and interesting for beginners.
Visitor Rating: 5 Stars
Visitor Rating: 5 Stars
Visitor Rating: 5 Stars