[WpProQuiz 3]
[WpProQuiz_toplist 3]
Perl quiz on Operators
Learning through sharing
Here is the collection of most frequently asked Perl interview questions. Perl Basics What is CPAN? Its uses? What does “use strict” , “use vars” […]
This is one of the questions asked in many Perl related job interviews. See the question here: http://www.aliencoders.org/content/interview-questions-perl-freshers-and-experienced
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.
1. To list all installed Perl modules using perl script
[perl]
#!/usr/bin/perl #change the path with your perl location
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.
If you are working on Perl and you need to work on modules which is listed in CPAN. You may need to download it, install it and use it in your box.
For that you need to know how to install Perl modules. This article will show you simple steps to download and install Perl modules in Linux box. I know it’s not the new topic for Perl chapter but I wrote it for our standalone repository on Perl and moreover, all these method with examples and pitfalls are not listed at many places. So, I think it will be worthy for our viewers and Perl mongers 😀
Method 1 using cpan shell(It has advantage over manual installation. Read Note section at the bottom)
1. perl -MCPAN -e shell
cpan> install HTML::Template
or
2. perl -MCPAN -e ‘install HTML::Template’
or
3. cpan -i MIME::Lite
Method 2 Manual module installation
if you’re having problems installing with CPAN. If you’re on the command line, you can use something like wget to grab the file. Next you’ll want to unzip it with something like:
tar -zxvf HTML-Template-2.8.tar.gz
In case above command doesn’t work for you, try this
gzip -d XML-Parser-2.36.tar.gz
tar -xvf XML-Parser-2.36.tar
This will unzip the module into a directory, then you can move in and poke around – look for the README or INSTALL files.
In most cases, installing a module by hand is still pretty easy, though (although not as easy as CPAN). Once you’ve switched into the base directory for the module, you should be able to get it installed by typing:(I assume you are on Linux box)
1. perl Makefile.PL
2. make
3. make test
4. make install
If everything shows fine then That’s it. You are now ready to rock and roll with newly installed Perl modues 😀
But what if cpan command doesn’t work for method 1?
1. Check first if cpan command works in your box
$cpan
cpan: Command not found.
If you get the above result means you need to install cpan module to work from CPAN otherwise, you have cpan installed in your system and you can skim next steps 😀
2. yum install perl-CPAN
3. after successful download and installation. Type cpan for the first time use
and type o conf commit on cpan prompt
then type quit on cpan prompt
You are done! Now Method 1 will work without fail.
Note: If any CPAN module is dependent on the several other modules. CPAN automatically resolves the dependencies and installs other dependent modules.
cpanm module is more advanced for doing all these without hassle. If possible give it a try.
[ADDED LATER]
As it was commented through Facebook by one of our readers, so I thought to better update this article with his comments.
# Install cpanminus (once).
curl -L http://cpanmin.us | perl – –self-upgrade or
cpan App::cpanminus
# Install your CPAN modules.
cpanm HTML::Template
cpanm XML::Parser
Subscribe now to keep reading and get access to the full archive.
Visitor Rating: 1 Stars
Visitor Rating: 5 Stars