安装perl下的很多模块文件时,比较快捷的方法是使用cpan工具。默认cpan shell 使用的是cpan.org的源,在国内使用的话速度会非常的慢。如果更换为国内的如阿里或网易等公司的源的话,速度会明显提高。修改方法如下:
1、执行cpan命令确认存在该命令
如果cpan执行后提示找到该命令,可以通过yum -y install perl-CPAN 或手动安装该模块。
[root@localhost CPAN]# cpan
CPAN is the world-wide archive of perl resources. It consists of about
300 sites that all replicate the same contents around the globe. Many
countries have at least one CPAN site already. The resources found on
CPAN are easily accessible with the CPAN.pm module. If you want to use
CPAN.pm, lots of things have to be configured. Fortunately, most of
them can be determined automatically. If you prefer the automatic
configuration, answer 'yes' below.
If you prefer to enter a dialog instead, you can answer 'no' to this
question and I'll let you configure in small steps one thing after the
other. (Note: you can revisit this dialog anytime later by typing 'o
conf init' at the cpan prompt.)
Would you like me to configure as much as possible automatically? [yes] yes
………… 省略
commit: wrote '/usr/share/perl5/CPAN/Config.pm'
Terminal does not support AddHistory.
cpan shell -- CPAN exploration and modules installation (v1.9402)
Enter 'h' for help.
首次执行会出现yes/no的界面,输入yes会自动配置,输入no会让手动的配置一些参数。这里我选择了yes,配置好后的配置文件存放在/usr/share/perl5/CPAN/Config.pm。
2、查看当前源配置
cpan[1]> o conf
$CPAN::Config options from '/usr/share/perl5/CPAN/Config.pm':
commit [Commit changes to disk]
defaults [Reload defaults from disk]
help [Short help about 'o conf' usage]
init [Interactive setting of all options]
applypatch []
auto_commit [0]
build_cache [100]
build_dir [/root/.cpan/build]
build_dir_reuse [0]
build_requires_install_policy [ask/yes]
bzip2 [/usr/bin/bzip2]
cache_metadata [1]
check_sigs [0]
cpan[2]> o conf urllist
urllist
Type 'o conf' to view all configuration items
默认没有配置源,也即使用的官方源,所以这里查不到源信息。
3、增加源或移出源并提交
cpan[3]> o conf urllist push http://mirrors.aliyun.com/CPAN/
Please use 'o conf commit' to make the config permanent!
cpan[4]> o conf commit
commit: wrote '/usr/share/perl5/CPAN/Config.pm'
也可以一次增加多个源:
cpan[4]> o conf urllist ftp://mirrors.sohu.com/CPAN/ http://mirrors.163.com/cpan/
移出一个源可以使用pop函数,如下:
cpan[3]> o conf urllist pop http://mirrors.163.com/cpan/ ftp://mirrors.sohu.com/CPAN/
再次查看当前源信息
cpan[5]> o conf urllist
urllist
0 [http://mirrors.aliyun.com/CPAN/]
Type 'o conf' to view all configuration items
也可以为本地用户自定义一个配置文件,可以配置为root的环境变量文件(原理类似于profile 与 ~/.bash_profile)
# mkdir -p /root/.cpan/CPAN/
# cp /usr/share/perl5/CPAN/Config.pm /root/.cpan/CPAN/MyConfig.pm
4、重新初始化配置
如果对当前的当前所有的cpan shell 的配置不满意,也可以通过命令重新初始化该文件,如下:
cpan> o conf init
写在最后,如果没有外网环境,或者想手动安装模块的,也可以参看我之前的博文 CPAN模块的安装 。