使用CPAN安裝Perl模組

season0891發表於2010-08-16

Perl到了第五版增加了模組的概念,用來提供物件導向程式設計的能力。這是Perl語言發展史上 
的一個里程碑。此後,廣大自由軟體愛好者開發了大量功能強大、構思精巧的Perl模組,極大地 
擴充套件了Perl語言的功能。CPAN(Comprehensive Perl Archive Network)是internet上Perl模組 
最大的集散地,包含了現今公佈的幾乎所有的perl模組。 

    幾個主要的CPAN站點有: 
    國內:ftp://freesoft.cgi.gov.cn/pub/languages/perl/CPAN 
           
    國外: 
           

    我在這裡介紹一下各種平臺下perl模組的安裝方法。以安裝DBI模組為例。 

1、Linux/Unix下的安裝方法: 

    Linux/Unix下安裝Perl模組有兩種方法:手工安裝和自動安裝。第一種方法是從CPAN上下載 
您需要的模組,手工編譯、安裝。第二種方法是聯上internet,使用一個叫做CPAN的模組自動完 
成下載、編譯、安裝的全過程。 

a、手工安裝的步驟: 

從CPAN下載了DBI模組1.13版的壓縮檔案DBI-1.13.tar.gz,假設放在/usr/local/src/下。 
cd /usr/local/src 
解壓縮這個檔案: 
tar xvzf DBI-1.13.tar.gz 
這時會新建一個DBI-1.13的目錄。 
cd DBI-1.13 

生成makefile: 
perl Makefile.PL 
建立模組 
make 
測試模組 
make test 
如果測試結果報告“all test ok”,您就可以放心地安裝編譯好的模組了。安裝模組前,先要 
確保您對perl5安裝目錄有可寫許可權(通常以su命令獲得),執行: 
make install 

現在,寫個程式試試吧。 

#!/usr/bin/perl -w 
use strict; 
use DBI; 
..

    上述步驟適合於Linux/Unix下絕大多數的Perl模組。可能還有少數模組的安裝方法略有差別, 
所以最好先看看安裝目錄裡的README或INSTALL。另外,上述過程是針對動態連結的Perl編譯 
器(所有Linux下預安裝的Perl都是動態連結的),如果您在使用一個靜態連結的Perl,您需要將 
新的模組靜態連結到perl編譯器中,可能還需要重啟機器。

b、使用CPAN模組自動安裝: 

安裝前需要先聯上線,並且您需要取得root許可權。 
perl -MCPAN -e shell 
初次執行CPAN時需要做一些設定,如果您的機器是直接與internet相聯(撥號上網、專線,etc.), 
那麼一路回車就行了,只需要在最後選一個離您最近的CPAN映象站點。例如我選的是位於國內的 
中國自由軟體庫ftp://freesoft.cgi.gov.cn/pub/languages/perl/CPAN 。否則,如果您的機器 
位於防火牆之後,還需要設定ftp代理或http代理。 

獲得幫助 
cpan>h 

列出CPAN上所有模組的列表 
cpan>m 

安裝模組 
cpan>install DBI 
自動完成DBI模組從下載到安裝的全過程。 

退出 
cpan>q 

2、Win32下的安裝方法: 

    在Win32下,建議使用ActiveState的Perl for Win32,即ActivePerl。ActivePerl的主頁在: 
 。在Win32下,最好也使用專門為ActivePerl定製的Perl模組。從 
CPAN下載的Perl模組不能很好地在ActivePerl下使用。類似於CPAN模組,ActiveState也開發 
了一個自動安裝工具叫做PPM(Perl Package Manager)。 

最通常的安裝方法是: 
首先聯上線 
在dos命令列下啟動ppm 
ppm 

獲得幫助 
PPM>h 

列出ActiveState站點上所有為Perl模組的清單。 
PPM>search 

安裝模組 
PPM>install DBI 
自動完成DBI模組從下載到安裝的全過程。 

退出 
PPM>q 

如果您的機器位於防火牆之後,透過http代理上網,那麼可以使用下面的方法。 

對於老的基於perl 5.005的ActivePerl版本(ActivePerl 522以下版本,不包括ActivePerl 522),從 
下載您需要的Perl模組,例如DBI.zip。 
將其解壓縮在C:\TEMP\DBI下 
cd \TEMP\DBI 
ppm install DBI.PPD 

最新的基於perl 5.6的ActivePerl版本(ActivePerl 613及以上版本)的安裝方法略有不同。安 
裝ActivePerl 613前需要先安裝Microsoft的Windows Installer。 
Windows Installer從這裡下載: 
 
ActivePerl 613從這裡下載: 
… ePerl-5.6.0.613.msi 

對於基於perl 5.6的ActivePerl版本,從 
PPMPackages/5.6/ 
下載您需要的Perl模組的安裝描述檔案,例如DBI.PPD 
安裝模組前需要先聯上線 
ppm install DBI.PPD。 

If you are already familiar with installing CPAN modules, go-perl is no different from any other. If you aren’t, you may wish to experiment now.

Try typing the following:

perl -MCPAN -e shell
install GO::Parser

The following may also work:

cpan GO::Parser

If this is the first time using the CPAN installer, you will be asked a barrage of questions. You are best to answer with the defaults – you can always change this later.

If you do NOT have access to the system-wide perl directories on your system (these will typically be in /Library or /usr/local), then you should either get some help setting up CPAN for use with your system, OR you should follow an alternate protocol below. (You may wish to modify the value of PREFIX to ~ in the CPAN setup, as detailed in protocol III)

If you are on OS X you may need to do this:

sudo cpan GO::Parser

Providing you do have write-access to these directories, the above commands should hopefully go smoothly. When installing go-perl, you may be asked if you wish to also install Data::Stag and IO::String – you should answer yes

After this, you should be ready to use go-perl! The scripts should be in your path and the libraries should be in the standard place. You may wish to start by trying “map2slim” or “go2obo_xml”

There are certain applications that require additional third-party modules. All these are available from CPAN, and can be installed in the above manner. For example, if you try and use the go-filter-subset.pl script. to generate a PNG image of an ontology subgraph, the system will complain you do not have the module “GraphViz” installed. To install this, simply type:

perl -MCPAN -e shell
install GraphViz

Depending on what you use go-perl for, you may not ever need to install other CPAN modules

Trackback : http://wanguan2000.blog.ubuntu.org.cn/2009/02/12/%E4%BD%BF%E7%94%A8cpan%E5%AE%89%E8%A3%85perl%E6%A8%A1%E5%9D%97/

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-671119/,如需轉載,請註明出處,否則將追究法律責任。

相關文章