Relocating CPAN Modules in Perl
I’ve always found CPAN modules troubling.
CPAN is a great, easy way to quickly get a Perl module onto the system. But unless you’re lucky and that module is available elsewhere using the native systems package manager (i.e rpm, ports or similar) then the CPAN installs become divorced from the system perl as time goes on. Ending up in conflicts and pain. It’s not the end of the world to keep these two in step but sometimes simpler is better and it’s nicer, as a user, to just drop the perl modules from CPAN you need into your own home directory (or other, non system, work area).
Of course, sometimes you just don’t have root! And need that module right now!
It turns out this is easy to do with a little digging through the Perl and CPAN docs.
- Set the CPAN makepl_arg flag to take a PREFIX= argument.
- Install your modules
- Point Perl to include your new module location in it’s search path
Setting the makepl_arg flag can be done during the first run of CPAN when it asks you a whole bunch of questions most of which, at the time, don’t mean much to you. The alternative way to do it (which is reproducible) is to run CPAN and configure it from within.
cpan[1]> o conf makepl_arg PREFIX=~/perl_modules
makepl_arg [PREFIX=/usr/home/<user>/perl_modules]
Then commit the change to make CPAN write out your updated config.
cpan[2]> o conf commit
commit: wrote ‘/usr/home/<user>/.cpan/CPAN/MyConfig.pm’
Now we can just install CPAN modules as normal. Now they’ll be built and installed into the directory we’ve configured above.
The last thing we have to do is to tell perl that, whenever we run it, it should look in this new directory to find modules as well as in the global system module include paths.
We can do this by manipulating the PERL5LIB environment variable.
export PERL5LIB=/usr/home/<user>/perl_modules/lib/perl5/site_perl/5.8.8
The example above is in bash – adjust according to your favorite shell! It is probably also worth setting this in your environment at login using whatever method your shell uses.
That’s it! You can verify things look better by using the two commands :
perl -V
Which will display the @INC path perl has for finding modules. Your new local module directory should show up there.
You can also query CPAN to make sure that it’s happy that the modules you’ve install using it are actually installed.
perl -MCPAN -e autobundle
This just shows it’s own library but is useful for confirmation.
Tags: CPAN, FreeBSD, Linux, modules, PerlPosted in FreeBSD, Linux, Perl | No Comments »





