Few tricks of using cpan

Reinstall CPAN

$ perl -MCPAN -e shell         # or just cpan, to fire up cpan prompt
cpan[1]> o conf init           # initialize the CPAN configuration
cpan[2]> yes                   # configure as much as possible automatically?
cpan[3]> exit                  # exit cpan prompt

The configuration was written into file: $HOME/.cpan/CPAN/MyConfig.pm.

Install modules

Install cpanm, which is a script to get, unpack, build and install modules from CPAN.

$ cpan App::cpanminus

Now we can install modules using cpanm in this format: cpanm Module::Name

$ cpanm Bundle::DBI            # or, (sudo) perl -MCPAN -e 'install Bundle::DBI'

Sometimes when you try to run a Perl script, it raises an error like this:

Can't locate Parallel/ForkManager.pm in @INC (you may need to install the Parallel::ForkManager module) (@INC contains: /home/hou/perl5/lib/perl5/5.20.1 ...

then the quick solution is installing the missing module like this:

cpanm -f Parallel::ForkManager.pm

Turn off proxy

$ cpan
cpan[1]> o conf /proxy/          # config options of proxy
cpan[2]> o conf no_proxy 1       # turn off proxy
cpan[3]> o conf http_proxy ""    # remove http_proxy
cpan[4]> o conf ftp_proxy ""     # remove ftp_proxy
cpan[5]> o conf commit           # commit this change

Alternatively, one can edit the file $HOME/.cpan/CPAN/MyConfig.pm to change the settings.

Leave a Reply