清新的Perl(十一)——CPAN
2020年了,Perl语言在网络上并没有多少拥趸者,甚至有些人拿着Python同Perl比较并带有诋毁之意。但是,这并不妨碍Perl语言散发它的魅力,也不妨碍我们继续使用它。
我们之所以至今还在使用perl,不仅仅是正则表达式的魅力,也不仅仅是Perl语言性能丝毫不逊于Python,最重要的是Perl语言有一个强大的第三方模块库。我们可以这里找到所有我们想要的模块,去构建强大功能的程序,为我们的工作服务。
讲CPAN,离不开CPAN程序以及MetaCPAN网站。
Linux系统不需多讲,本身自带Perl语言,往往也带有CPAN工具。而windows,主要以Strawberry Perl和ActivePerl为主。我做网络工程师的,还是在windows用的比较多,而比较两款windows Perl,我推荐用Strawberry,不仅仅因为它保持了Linux的原汁原味,还有ActivePerl的包管理工具在国内很慢,可以说是没法用的。
下面,我们需要根据国内网络情况,修改cpan设置,让他能够迅速下载我们想要的配置模块(虽然按照windows讲的,但是Linux同样适用)。
当我们安装好Perl语言后,可以在命令行内调用cpan命令,进入cpan工具:
Loading internal logger. Log::Log4perl recommended for better logging
Unable to get Terminal Size. The Win32 GetConsoleScreenBufferInfo call didn't work. The COLUMNS and LINES environment variables didn't work. at C:\Strawberry\perl\vendor\lib/Term/ReadLine/readline.pm line 410.
There seems to be running another CPAN process (pid 8604). Contacting...
Other job not responding. Shall I overwrite the lockfile 'C:\STRAWB~1\cpan\.lock'? (Y/n) [y] y
cpan shell -- CPAN exploration and modules installation (v2.27)
Enter 'h' for help.
cpan>
首先,通过o conf命令查看cpan配置文件:
cpan> o conf
$CPAN::Config options from C:\Strawberry\perl\lib/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]
allow_installing_module_downgrades undef
allow_installing_outdated_dists undef
applypatch []
auto_commit [1]
build_cache [50]
build_dir [C:\STRAWB~1\cpan\build]
build_dir_reuse [0]
build_requires_install_policy [yes]
bzip2 [ ]
cache_metadata [1]
check_sigs [0]
cleanup_after_install undef
colorize_debug undef
colorize_output [0]
colorize_print [bold green]
colorize_warn [bold red]
commandnumber_in_prompt [0]
commands_quote undef
connect_to_internet_ok [1]
cpan_home [C:\STRAWB~1\cpan]
curl []
dontload_hash undef
dontload_list undef
ftp [C:\Windows\system32\ftp.exe]
ftp_passive [1]
ftp_proxy []
ftpstats_period undef
ftpstats_size undef
getcwd [cwd]
gpg []
gzip [ ]
halt_on_failure [1]
histfile [C:\STRAWB~1\cpan\histfile]
histsize [1000]
http_proxy []
inactivity_timeout [0]
index_expire [1]
inhibit_startup_message [0]
keep_source_where [C:\STRAWB~1\cpan\sources]
load_module_verbosity [none]
lynx []
make [C:\STRAWB~1\c\bin\gmake.exe]
make_arg []
make_install_arg [UNINST=1]
make_install_make_command [C:\STRAWB~1\c\bin\gmake.exe]
makepl_arg []
mbuild_arg []
mbuild_install_arg [--uninst 1]
mbuild_install_build_command undef
mbuildpl_arg []
ncftp []
ncftpget []
no_proxy []
pager [C:\Windows\system32\more.COM]
password undef
patch [C:\STRAWB~1\c\bin\patch.exe]
patches_dir undef
perl5lib_verbosity [none]
plugin_list undef
prefer_external_tar [0]
prefer_installer [MB]
prefs_dir [C:\STRAWB~1\cpan\prefs]
prerequisites_policy [follow]
proxy_pass undef
proxy_user undef
randomize_urllist undef
recommends_policy [1]
scan_cache [atstart]
shell [C:\Windows\system32\cmd.exe]
show_unparsable_versions [0]
show_upload_date [1]
show_zero_versions [0]
suggests_policy [0]
tar [ ]
tar_verbosity [none]
term_is_latin [1]
term_ornaments [1]
test_report [0]
trust_test_report_history [0]
unzip []
urllist
0 [http://mirrors.163.com/cpan/]
urllist_ping_external undef
urllist_ping_verbose undef
use_prompt_default [0]
use_sqlite [1]
username undef
version_timeout [15]
wait_list undef
wget []
yaml_load_code [0]
yaml_module [YAML::XS]
cpan>
这里,我们主要看配置文件倒数第6行,urllist一项,当然我们也可以仅仅敲o conf urllist来直接查看配置:
cpan> o conf urllist
urllist
0 [http://mirrors.163.com/cpan/]
Type 'o conf' to view all configuration items
上面的配置文件,是我已经修改好的。在国内实际上,你只要百度“镜像站”一词,任何镜像站都会提供CPAN的镜像库。我比较常用的有两个中国科学技术大学USTC镜像站,还有网易开源镜像站:
http://mirrors.ustc.edu.cn/CPAN/
http://mirrors.163.com/cpan/
使用 o conf urrlist unshift 网址 来将镜像站加入:
cpan> o conf urllist unshift http://mirrors.ustc.edu.cn/CPAN/
commit: wrote 'C:\Strawberry\perl\lib/CPAN/Config.pm'
cpan> o conf urllist unshift http://mirrors.163.com/cpan/
commit: wrote 'C:\Strawberry\perl\lib/CPAN/Config.pm'
cpan> o conf urllist
urllist
0 [http://mirrors.163.com/cpan/]
1 [http://mirrors.ustc.edu.cn/CPAN/]
Type 'o conf' to view all configuration items
这样,我们就完成CPAN下载资源的修改。
使用install 模块名称 来安装我们想要的模块,例如安装Net::Telnet模块:
cpan> install Net::Telnet
Fetching with LWP:
http://mirrors.163.com/cpan/authors/01mailrc.txt.gz
Fetching with LWP:
http://mirrors.163.com/cpan/modules/02packages.details.txt.gz
Fetching with LWP:
http://mirrors.163.com/cpan/modules/03modlist.data.gz
Database was generated on Tue, 18 Feb 2020 07:30:08 GMT
Updating database file ... Done!
Net::Telnet is up to date (3.04).
剩下最后一个问题,我们如何找到想要的模块呢?通常来说,就是百度,CSDN博客之类的博文站通常会有各种材料,我们仅需要记住模块名字,使用cpan install命令安装即可。
而更加权威的方式,是访问metacpan搜索想要的模块,当然这种方式前提是英文过关,每一个模块都有详尽的英语说明文档。