[精讚] [會員登入]
9360

[PERL] 使用CPAN安裝模組

在Linux 上,CPAN 可以用來安裝或管理 perl 的模組,此文教你怎麼做。

分享此文連結 //n.sfs.tw/10322

分享連結 [PERL] 使用CPAN安裝模組@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2024-03-26 14:29:14 最後編修
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

 <install_help>

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')
 [local::lib]

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:
http://www.perl.org/CPAN/MIRRORED.BY

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

安裝中…

export PERL_LOCAL_LIB_ROOT="$PERL_LOCAL_LIB_ROOT:/root/perl5";
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[1]>

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

要安裝 threads 模組
cpan> install threads

在測試模組是否能安裝,使用指令test:

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

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();
    $dh->close();
    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");
         unlink($file);
      }
      my $pf = $installed->packlist($module)->packlist_file();
      print("rm $pf\n");
      unlink($pf);
      foreach my $dir (sort($installed->directory_tree($module))) {
         if (emptydir($dir)) {
            print("rmdir $dir\n");
            rmdir($dir);
         }
      }
   }
}

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

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

不過壞了就重裝perl就好了,也不必太緊張啦

# yum reinstall perl

 

6. 使用 cpanm

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

安裝cpanm

# 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:

  /root/perl5/lib/perl5/Sub/Uplevel.pm
  /root/perl5/man/man3/Sub::Uplevel.3pm

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

 

MAC錯誤排除

1. xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

解決:[1]

# xcode-select --install

安裝完畢後即可

 

PERL 安裝DBD:mysql

yum install "perl(DBD::mysql)"
OR
yum install perl-DBD-MySQL

 

參考資料

[1] https://stackoverflow.com/questions/52522565/git-is-not-working-after-macos-update-xcrun-error-invalid-active-developer-pa

[2] https://metacpan.org/dist/DBD-mysql/view/lib/DBD/mysql/INSTALL.pod

 

 


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

END

你可能感興趣的文章

PERL 正規表達式會用到的符號 PERL在比對時常常會用到符號整理

[PERL] split 寫法 Perl 將字串分開可以用 split 這個函數,此函數可以用Regex作分隔判斷

[PERL] 23-多執行緒 而多執行緒的程式,可在一次執行程式時間,同時進行多線程的計算,在效率上可獲得即大的提升。

[PERL] 16-字串取代和置換 Perl 字串比對及置換

[PERL] 11- 雜湊的範例 Perl 的幾個雜湊範例

[PERL] 22-日期和時間 Perl 取得日期時間的方法整理,使用 localtime等多種函數

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

安裝SPHINX支援中文 新版本的 sphinx 和舊版不同,網路上很多範例和教學是不能用的。此文是安裝和設定方法分享

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

此一時彼一時 我是不是易怒的人,其實我也不知道,總之我常會失控,不知道自己在幹嘛。這近生活過得浮浮的,不是很踏實,總會想太多,我甚至會

安裝git centos/freebsd/windows安裝git

Javascript/Jquery 建立、讀取、刪除cookie值 Javascript/Jquery 建立、讀取、刪除cookie值