Oct 122014
 
Article Perl

Installing a perl CPAN module in the default location requires superuser privileges. But non-root users can also install modules in a different location, namely in a directory under the login directory.

This post explains how to do it.

1. CPAN configuration

The firt step is creating the directory where we want to install the CPAN modules. For instance, create a “perl5” directory under the login directory:

Next, create an empty CPAN configuration file specific to the unix user we are logged in with:

The ‘cpan’ command looks for this file and uses it if it exists. Otherwise, it uses the generic system configuration file for CPAN (usually ‘/root/.cpan/CPAN/Config.pm’).

Next, execute cpan, and run the command “o conf init” to perform an initial configuration:

During the configuration process, cpan requests the values of several parameters, and writes them to the configuration file. Among these parameters, some of them specify several directories that will be used in the compilation of the modules.

After the initial configuration is complete, we can have a look at the MyConfig.pm file to check that those parameters have been configured as follows (replacing ‘/home/username’ with the actual login directory of the user):

2. Configure the module installation directory: INSTALL_BASE

To tell cpan to install modules under the directory ‘~/perl5’ previously created, there are two options:

The recommended method is to define an environment variable PERL_MM_OPT as follows:

There are also two parameters makepl_arg and mbuildpl_arg that can be specified in the CPAN configuration file, depending on whether the module uses ‘Makefile.PL’ or ‘Build.PL’:

Alternatively, a “PREFIX” parameter can be defined in the CPAN configuration file.

After INSTALL_BASE or PREFIX have been defined, as shown, the modules will be installed under a subdirectory ‘~/perl5/lib/perl5’.  Man pages will be installed under ‘~/perl5/man’, and binaries will be installed under ‘~/perl5/bin’.

3. Using the installed modules in perl command-line scripts

For perls scripts to be able to locate and use the modules installed under the login directory, an environment variable PERL5LIB must be defined:

cpan can be executed with the command:

4. Using the installed modules in perl CGI scripts

If a perl script that needs to use the locally installed modules is run as a CGI in a web server, we need to define PERL5LIB in the profile of the user that runs the web server.

But, in some cases we may not have the possibility to alter the execution environment of the web server (for instance, in a shared hosting).

In that case, the location of the modules can be explicitly configured in the script, adding at the beginning of the script the sentence:

References:

ExtUtils::MakeMaker::FAQ – perldoc.perl.org

Index of posts about perl programming

 Posted by at 12:19 pm

 Leave a Reply

(required)

(required)