2009-09-14

Perl - How to install Perl module using CPAN shell

One of Perl's strength is it's repository of modules, thousands of modules. No need to re-invent the wheel, just think of what you wanna do and search for it (http://cpan.perl.org).

The CPAN modules are available through the CPAN shell. In this post, we will talk briefly about how to install the CPAN shell and show an example of installing a module.

First, we will need to install the shell before we can use it :
perl -MCPAN -e install

Since this is the first time installing Perl's CPAN shell, some might get the error (in Ubuntu/Debian) :
perl cpan Can't exec "mysql_config": No such file or directory at Makefile.PL

It basically needs the mysql development files. Install it with :
apt-get install libmysqlclient15-dev

After the installation, retry the installation of CPAN shell again.

If it fails again, look for lines similar to below (take note of the directory mentioned) :
Has already been unwrapped into directory /root/.cpan/build/DBD-mysql-4.012-Lao8Q1
'/usr/bin/perl Makefile.PL INSTALLDIRS=site' returned status 512, won't make

This time, there is some error about about perl was unable fulfill some test on the modules. We will need to install it manually. Execute the following as root:
cd /root/.cpan/build/DBD-mysql-4.012-Lao8Q1
perl Makefile.PL
make
make install

Retry the installation of the CPAN shell again and if it bumps into error again, look for the similiar error message above and repeat the manual installation until all are fine.

After the initial installation, start the CPAN shell by :
perl -MCPAN -e shell

upon starting the CPAN shell for the first time, it would ask whether to auto configure as much as possible of the parameters required to run the shell. Press enter to take the default answer, which is "yes"

Since this is the first time CPAN shell is being started, let's install something useful that at the same time shows how to install a module using CPAN shell.

at the CPAN shell prompt, do :
install Bundle::CPANxxl

this would start installing the common useful modules that make our life easier later. This bundle also install the required modules for history browsing, so that when we press the up/down keys, it shows the previous command executed.

During the installation of the bundle, it might ask a lot of question on whether to install the dependencies in order to proceed. e.g. :
Shall I follow them and prepend them to the queue
of modules we are processing right now? [yes]

It can be quite irritating if a module or bundle requires dozens of modules. This behavior can be alter. Change the parameter
'prerequisites_policy' => q[ask],

to
'prerequisites_policy' => q[follow],

in the perl configuration file "/etc/perl/CPAN/Config.pm". This would take the default answer on every prompt to whether to install the dependencies, which is "yes".

Another thing, the below command is for listing the module(s) with search term specified :
i /search term/

e.g.
i /mysql/

In the above example, it would search for any modules that has "mysql" term in the modules name.

Hasta la vista !!!

No comments: