轉自:php
http://www.cnblogs.com/itech/archive/2009/08/10/1542832.htmlhtml
http://www.mike.org.cn/blog/index.php?load=read&id=643web
Perl 到了第五版增長了模塊的概念,用來提供面向對象編程的能力。這是 Perl 語言發展史上的一個里程碑。此後,廣大自由軟件愛好者開發了大量功能強大、構思精巧的 Perl 模塊,極大地擴展了 Perl 語言的功能。CPAN(Comprehensive Perl Archive Network)是Perl模塊最大的集散地,包含了現今公佈的幾乎全部的perl模塊。shell
安裝方法編程
我在這裏介紹一下各類平臺下 perl 模塊的安裝方法。以安裝Net-Server模塊爲例。windows
一 Linux/Unix下安裝Perl模塊有兩種方法:手工安裝和自動安裝。ide
第一種方法是從CPAN上下載您須要的模塊,手工編譯、安裝。第二種方法是使用CPAN模塊自動完成下載、編譯、安裝的全過程。測試
A、手工安裝的步驟:ui
從 CPAN(http://search.cpan.org/)下載了Net-Server模塊0.97版的壓縮文件Net-Server-0.97.tar.gz,假設放在/usr/local/src/下。spa
cd /usr/local/src
解壓縮這個文件,這時會新建一個Net-Server-0.97的目錄。
tar xvzf Net-Server-0.97.tar.gz
換到解壓後的目錄:
cd Net-Server-0.97
生成 makefile:
perl Makefile.PL
生成模塊:make
測試模塊(這步無關緊要):
make test
若是測試結果報告「all test ok」,您就能夠放心地安裝編譯好的模塊了。
安裝模塊前,先要確保您對 perl5 安裝目錄有可寫權限(一般以 su 命令得到),執行:
make install
如今,試試 DBI 模塊吧。若是下面的命令沒有給出任何輸出,那就沒問題。
$>perl -MNet::Server -e1
上述步驟適合於 Linux/Unix下絕大多數的Perl模塊。可能還有少數模塊的安裝方法略有差異,因此最好先看看安裝目錄裏的 README 或 INSTALL。
有的時候若是是build.pl的須要如下安裝步驟:(須要Module::Build模塊支持)
perl Build.PL
./Build
./Build test
./Build install
B、使用CPAN模塊自動安裝方法一:
安裝前須要先聯上網,而且您須要取得root權限。
perl -MCPAN -e shell
初次運行CPAN時須要作一些設置,若是您的機器是直接與因特網相聯(撥號上網、專線,etc.),那麼一路回車就好了,只須要在最後一步選一個離您最近的 CPAN 鏡像站點。例如我選的是位於國內的http://www.cnblogs.com/itech/admin/ftp://www.perl87.cn/CPAN/ 。不然,若是您的機器位於防火牆以後,還須要設置ftp代理或http代理。下面是經常使用 cpan 命令。
得到幫助
cpan>help
列出CPAN上全部模塊的列表
cpan>m
安裝模塊,自動完成Net::Server模塊從下載到安裝的全過程。
cpan>install Net::Server
退出
cpan>quit
C、使用CPAN模塊自動安裝方法二:
cpan -i 模塊名
例如:cpan -i Net::Server
二 windows上perl模塊安裝
A 手動(跟Linux相似) [解壓後 perl makefile.pl nmake/dmake nmake/dmake install]
nmake須要cd C:\Program Files\Microsoft Visual Studio X\VC\bin and execute vcvars32.bat;而後執行nmake;
dmake 貌似是cpan環境配置好就有了在C:\Perl\site\bin下。
B Cpan (安裝前須要對cpan配置,cpan須要安裝其餘的模塊dmake和MinGw gcc compiler) (跟Linux相似)
C 若是使用ActivePerl,可使用PPM(巧記Perl install PM module)來安裝,使用PPM GUI或PPM Commandline。通常安裝在site\lib下
PPM commandline實例以下:
a) add correct repositories..
c:\perl\bin\ppm repo add http://theoryx5.uwinnipeg.ca/ppms/package.lst
c:\perl\bin\ppm repo add http://www.roth.net/perl/packages/
b) add the packages
c:\perl\bin\ppm install Carp-Assert
c:\perl\bin\ppm install Log-Log4perl
c:\perl\bin\ppm install YAML-Syck
或
ppm install Path::Class
三 幾個主要的CPAN站點有:
國內:
最新更新請查閱 http://cpan.org/SITES.html
http://www.perl87.cn/CPAN/ 網頁鏡像
http://www.cnblogs.com/itech/admin/ftp://www.perl87.cn/CPAN/ 模塊鏡像
國外:http://www.cpan.org/
四 使用cpan和ppm安裝時要注意模塊名字的大小寫
五 To get a list of the standard pragmatics(至關於c的宏定義) and modules, see perldoc perlmodlib.
咱們能夠直接輸入 perldoc perlmodlib 來查看pragmatics和modules
Pragmatic Modules They work somewhat like compiler directives (pragmata) in that they tend to affect the compilation of your program, and thus will usually work well only when used within a "use", or "no". Most of these are lexically scoped, so an inner BLOCK may countermand them by saying: no integer; no strict 'refs'; no warnings; which lasts until the end of that BLOCK. Some pragmas are lexically scoped--typically those that affect the $^H hints variable. Others affect the current package instead, like "use vars" and "use subs", which allow you to predeclare a variables or subroutines within a particular file rather than just a block. Such declarations are effective for the entire file for which they were declared. You cannot rescind them with "no vars" or "no subs".
Standard Modules Standard, bundled modules are all expected to behave in a well-defined manner with respect to namespace pollution because they use the Exporter module. See their own documentation for details. To find out all modules installed on your system, including those without documentation or outside the standard release, just use the following command (under the default win32 shell, double quotes should be used instead of single quotes). % perl -MFile::Find=find -MFile::Spec::Functions -Tlwe \ 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' (The -T is here to prevent '.' from being listed in @INC.) They should all have their own documentation installed and accessible via your system man(1) command. Note also that the command "perldoc perllocal" gives you a (possibly incomplete) list of the modules that have been further installed on your system. (The perllocal.pod file is updated by the standard MakeMaker install process.)
Extension Modules Extension modules are written in C (or a mix of Perl and C). They are usually dynamically loaded into Perl if and when you need them, but may also be linked in statically. Supported extension modules include Socket, Fcntl, and POSIX. Many popular C extension modules do not come bundled (at least, not completely) due to their sizes, volatility, or simply lack of time for adequate testing and configuration across the multitude of platforms on which Perl was beta-tested. You are encouraged to look for them on CPAN (described below), or using web search engines like Alta Vista or Google.