[PERL] 使用CPAN安裝模組

URL Link http://n.sfs.tw/10322

2016-11-29 15:35:39 By 張○○

在Linux 上,CPAN 可以用來安裝或管理 perl 的模組

1. 安裝CPAN shell

# yum install gcc* perl-CPAN


2. 進入CPAN Shell

# perl -MCPAN -e shell


# perl -MCPAN -e shell

CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.

Would you like to configure as much as possible automatically? [yes] 你要不要進行自動設定?按ENTER


Warning: You do not have write permission for Perl library directories.

To install modules, you need to configure a local Perl library directory or
escalate your privileges.  CPAN can help you by bootstrapping the local::lib
module or by configuring itself to use 'sudo' (if available).  You may also
resolve this problem manually if you need to customize your setup.

你要用什麼方法管理你的套件?按ENTER。內建的 local::lib可以讓你不需要root的權限就能安授
What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')

Autoconfigured everything but 'urllist'.

Now you need to choose your CPAN mirror sites.  You can let me
pick mirrors for you, you can select them from a list or you
can enter them by hand.

Would you like me to automatically choose some CPAN mirror
sites for you? (This means connecting to the Internet) [yes] 你要自動選擇CPAN 的映射站嗎?按ENTER

Trying to fetch a mirror list from the Internet
Fetching with HTTP::Tiny:

Looking for CPAN mirrors near you (please be patient)
........................... done!


export PERL_MB_OPT="--install_base /root/perl5";
export PERL_MM_OPT="INSTALL_BASE=/root/perl5";
export PERL5LIB="/root/perl5/lib/perl5:$PERL5LIB";
export PATH="/root/perl5/bin:$PATH";

Would you like me to append that to /root/.bashrc now? [yes] 你要把這些設定加到/root/.bashrc 嗎?按ENTER

commit: wrote '/root/.cpan/CPAN/MyConfig.pm'

You can re-run configuration any time with 'o conf init' in the CPAN shell
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v1.9800)
Enter 'h' for help.


安裝模組只要在cpan shell 下打入 install xxx::yyy,例如安裝 Net::IPv4Addr 模組
cpan> install Net::IPv4Addr

要安裝 threads 模組
cpan> install threads


cpan> test DBI

要強制安裝套件-- 有時安裝前會執行檢查,檢查 不過時不給裝,這時可以「硬裝」,個人經驗是硬裝裝完有時程式執行會出錯,找不到原因,所以儘量少用這種方法,硬裝的方法是在 install 前加上 'force'。

cpan> force install Net::DNS

直接使用 cpan 指令安裝

# cpan -i CGI
# cpan -i Digest::MD5
# cpan -i Math::Calc::Units


3. 檢查參數或修改參數

如果要檢查或修改參數,使用 o 這個指令
cpan> o conf

未設定 make 參數安裝會出錯,設定make 的路徑參數

cpan> o conf make /usr/bin/make
make    /usr/bin/make


4. CPAN 臨時目錄及常見錯誤

4.1 CPAN 統會將檔案下載置於 /root/.cpan/build/xxx


4.2 假如出現 Is already unwrapped into director 的錯誤,例如安裝 threads 時:

cpan> install threads
Running install for module threads
Running make for J/JD/JDHEDDEN/threads-1.77.tar.gz
  Is already unwrapped into directory /root/.cpan/build/threads-1.77
  Makefile.PL returned status 512

只要把 /root/.cpan remove 即可

4.3 新裝的系統如果出現 No 'C' compiler found to build 'xxx'  的錯誤,代表你沒有編譯器,請回到系統安裝 gcc或是 gcc-g++

No 'C' compiler found to build 'threads'

# yum install gcc


5. 移除安裝的module

cpan 可以安裝模組,但卻不能移除模組(但cpanm可以,請參看最下面),只能借用別的方法,這裡 https://metacpan.org/pod/ExtUtils::Packlist#EXAMPLE 有移除的程式,只要複製回來建立一個檔案再執行即可,例如 建立一個檔 remove_module.pl 把下面的內容貼入

#!/usr/local/bin/perl -w
use strict;
use IO::Dir;
use ExtUtils::Packlist;
use ExtUtils::Installed;
sub emptydir($) {
    my ($dir) = @_;
    my $dh = IO::Dir->new($dir) || return(0);
    my @count = $dh->read();
    return(@count == 2 ? 1 : 0);
# Find all the installed packages
print("Finding all installed modules...\n");
my $installed = ExtUtils::Installed->new();
foreach my $module (grep(!/^Perl$/, $installed->modules())) {
   my $version = $installed->version($module) || "???";
   print("Found module $module Version $version\n");
   print("Do you want to delete $module? [n] ");
   my $r = <STDIN>; chomp($r);
   if ($r && $r =~ /^y/i) {
      # Remove all the files
      foreach my $file (sort($installed->files($module))) {
         print("rm $file\n");
      my $pf = $installed->packlist($module)->packlist_file();
      print("rm $pf\n");
      foreach my $dir (sort($installed->directory_tree($module))) {
         if (emptydir($dir)) {
            print("rmdir $dir\n");

這個舊型程式的缺點是他會一個模組一個模組的詢問,你得一一確認直到你要刪除的那個為止。(有新版本的程式可用,但缺點是需要更新的perl來執行。)因為我的系統是5.8,又無法更新,只好將就用,如果不小心刪錯的話,重裝即可。但是如果問你 Cwd,千萬不能刪,刪了cpan就壞了....

Do you want to delete Cwd? [n] y   //千萬不能刪,刪了cpan就壞了....


# yum reinstall perl


6. 使用 cpanm

cpanm 可以略過安裝的 shell直接安裝模組


# cd /bin/

# curl -L https://cpanmin.us/ -o cpanm

# chmod +x cpanm

安裝完畢後在 /bin資料匣中會有 cpanm這個可執行檔

使用 cpanm安裝套件

# cpanm Time::Elapse
--> Working on Time::Elapse
Fetching http://www.cpan.org/authors/id/S/SG/SGODIN/Time-Elapse-1.2402.tar.gz ... OK
Configuring Time-Elapse-1.2402 ... OK
Building and testing Time-Elapse-1.2402 ... OK
Successfully installed Time-Elapse-1.2402
1 distribution installed



7. 使用 cpanm 移除模組

如果是由 cpanm 安裝的模組就能由cpanm來移除

# cpanm --uninstall Sub::Uplevel
Sub::Uplevel contains the following files:


Are you sure you want to uninstall Sub::Uplevel? [y] y

Unlink: /root/perl5/lib/perl5/Sub/Uplevel.pm
Unlink: /root/perl5/man/man3/Sub::Uplevel.3pm
Unlink: /root/perl5/lib/perl5/x86_64-linux-thread-multi/auto/Sub/Uplevel/.packlist

Successfully uninstalled Sub::Uplevel



原文 2009-10-21 15:37:26  2018.1.7 再編修