安装perl下的很多模块文件时,比较快捷的方法是使用cpan工具。默认cpan shell 使用的是cpan.org的源,在国内使用的话速度会非常的慢。如果更换为国内的如阿里或网易等公司的源的话,速度会明显提高。修改方法如下:

1、执行cpan命令确认存在该命令

如果cpan执行后提示找到该命令,可以通过yum -y install perl-CPAN 或手动安装该模块。

 1[root@localhost CPAN]# cpan
 2CPAN is the world-wide archive of perl resources. It consists of about
 3300 sites that all replicate the same contents around the globe. Many
 4countries have at least one CPAN site already. The resources found on
 5CPAN are easily accessible with the CPAN.pm module. If you want to use
 6CPAN.pm, lots of things have to be configured. Fortunately, most of
 7them can be determined automatically. If you prefer the automatic
 8configuration, answer 'yes' below.
 9If you prefer to enter a dialog instead, you can answer 'no' to this
10question and I'll let you configure in small steps one thing after the
11other. (Note: you can revisit this dialog anytime later by typing 'o
12conf init' at the cpan prompt.)
13Would you like me to configure as much as possible automatically? [yes] yes
14………… 省略
15commit: wrote '/usr/share/perl5/CPAN/Config.pm'
16Terminal does not support AddHistory.
17cpan shell -- CPAN exploration and modules installation (v1.9402)
18Enter 'h' for help.

首次执行会出现yes/no的界面,输入yes会自动配置,输入no会让手动的配置一些参数。这里我选择了yes,配置好后的配置文件存放在/usr/share/perl5/CPAN/Config.pm。

2、查看当前源配置

 1cpan[1]> o conf
 2$CPAN::Config options from '/usr/share/perl5/CPAN/Config.pm':
 3    commit             [Commit changes to disk]
 4    defaults           [Reload defaults from disk]
 5    help               [Short help about 'o conf' usage]
 6    init               [Interactive setting of all options]
 7    applypatch         []
 8    auto_commit        [0]
 9    build_cache        [100]
10    build_dir          [/root/.cpan/build]
11    build_dir_reuse    [0]
12    build_requires_install_policy [ask/yes]
13    bzip2              [/usr/bin/bzip2]
14    cache_metadata     [1]
15    check_sigs         [0]
16cpan[2]> o conf urllist
17    urllist
18Type 'o conf' to view all configuration items

默认没有配置源,也即使用的官方源,所以这里查不到源信息。

3、增加源或移出源并提交

1cpan[3]> o conf urllist push http://mirrors.aliyun.com/CPAN/
2Please use 'o conf commit' to make the config permanent!
3cpan[4]> o conf commit
4commit: wrote '/usr/share/perl5/CPAN/Config.pm'

也可以一次增加多个源:

1cpan[4]> o conf urllist ftp://mirrors.sohu.com/CPAN/ http://mirrors.163.com/cpan/

移出一个源可以使用pop函数,如下:

1cpan[3]> o conf urllist pop http://mirrors.163.com/cpan/ ftp://mirrors.sohu.com/CPAN/

再次查看当前源信息

1cpan[5]> o conf urllist
2    urllist
3        0 [http://mirrors.aliyun.com/CPAN/]
4Type 'o conf' to view all configuration items

也可以为本地用户自定义一个配置文件,可以配置为root的环境变量文件(原理类似于profile 与 ~/.bash_profile)

1# mkdir -p /root/.cpan/CPAN/
2# cp /usr/share/perl5/CPAN/Config.pm /root/.cpan/CPAN/MyConfig.pm

4、重新初始化配置

如果对当前的当前所有的cpan shell 的配置不满意,也可以通过命令重新初始化该文件,如下:

1cpan> o conf init

写在最后,如果没有外网环境,或者想手动安装模块的,也可以参看我之前的博文 CPAN模块的安装