perl 常用模块使用例子------欢迎大家补充。
一些常用模块的简单描述
http://www.perldoc.com/perl5.6/pod/perlmodlib.html
在perl 中使用模块:
模块的下载地址: http://www.cpan.org/modules/01modules.index.html
安装模块:
1. perl Makefile.PL
2. make
3. make test
4. make install
也可以用如下命令安装模块(已知的适用的系统redhat 9.0,其他的我不知道,请大家试试看:).
perl -MCPAN -e shell>
接着输入:install MODEL_NAME
查看模块的帮助:
perldoc MODEL_NAME
例如:
perldoc Net::FTP
已有模块:(以下的内容转自CU,谢谢CU的朋友)
说明:
以下例子 代码的测试是在FreeBSD & Solaris下进行的,Perl版本为5.005_03。
(1) Net::FTP
(2) Net::Telnet
(3) LWP::Simple, get()
(4) Expect
(5) XML::Simple, XMLin()
(6) Data::Dumper, Dumper()
(7) IO::Socket
(8) Date::Manip, DateCalc(), UnixDate()
(9) Date::Manip, Date_Cmp()
(10) File::Find, find()
(11) ExtUtils::Installed, new(), modules(), version()
(12) DBI, connect(), prepare(), execute(), fetchrow_array()
(13) Getopt::Std
(14) Proc::ProcessTable
(15) Shell
(16) Time::HiRes, sleep(), time()
(17) HTML::LinkExtor, links(), parse_file()
(18) Net::Telnet, open(), print(), getline()
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()
(20) Net::POP3, login(), list(), get()
(21) Term::ANSIColor
(22) Date::Calc Calendar(), Today()
(23) Term::Cap, Tgetend(), Tgoto, Tputs()
(24) HTTPD::Log::Filter
(25) Net::LDAP
(26) Net::SMTP mail(), to(), data(), datasend(), auth()
(27) MIME::Base64, encode_base64(), decode_base64()
(28) Net::IMAP::Simple, login(), mailboxes(), select(), get()...
(29) Bio::DB::GenBank, Bio::SeqIO
(30) Spreadsheet::ParseExcel
(31) Text::CSV_XS, parse(), fields(), error_input()
(32) Benchmark
说明:
以下例子 代码的测试是在RH Linux7.2下进行的,Perl版本为5.6.0。
(33) HTTP:: Daemon, accept(), get_request()...
(34) Array::Compare, compare(), full_compare()...
(35) Algorithm::Diff, diff()
(36) List::Util, max(), min(), sum(), maxstr(), minstr()...
(37) HTML::Parser
(38) Mail::Sender
(39) Time::HiRes, gettimeofday(), usleep()
这里接着上面的序号:
(40) Image::Magick
以下模块在RedHat 9.0 ,perl version v5.8.0 built 通过。
(41) Data::SearchReplace
http://www.perldoc.com/perl5.6/pod/perlmodlib.html
在perl 中使用模块:
模块的下载地址: http://www.cpan.org/modules/01modules.index.html
安装模块:
1. perl Makefile.PL
2. make
3. make test
4. make install
也可以用如下命令安装模块(已知的适用的系统redhat 9.0,其他的我不知道,请大家试试看:).
perl -MCPAN -e shell>
接着输入:install MODEL_NAME
查看模块的帮助:
perldoc MODEL_NAME
例如:
perldoc Net::FTP
已有模块:(以下的内容转自CU,谢谢CU的朋友)
说明:
以下例子 代码的测试是在FreeBSD & Solaris下进行的,Perl版本为5.005_03。
(1) Net::FTP
(2) Net::Telnet
(3) LWP::Simple, get()
(4) Expect
(5) XML::Simple, XMLin()
(6) Data::Dumper, Dumper()
(7) IO::Socket
(8) Date::Manip, DateCalc(), UnixDate()
(9) Date::Manip, Date_Cmp()
(10) File::Find, find()
(11) ExtUtils::Installed, new(), modules(), version()
(12) DBI, connect(), prepare(), execute(), fetchrow_array()
(13) Getopt::Std
(14) Proc::ProcessTable
(15) Shell
(16) Time::HiRes, sleep(), time()
(17) HTML::LinkExtor, links(), parse_file()
(18) Net::Telnet, open(), print(), getline()
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()
(20) Net::POP3, login(), list(), get()
(21) Term::ANSIColor
(22) Date::Calc Calendar(), Today()
(23) Term::Cap, Tgetend(), Tgoto, Tputs()
(24) HTTPD::Log::Filter
(25) Net::LDAP
(26) Net::SMTP mail(), to(), data(), datasend(), auth()
(27) MIME::Base64, encode_base64(), decode_base64()
(28) Net::IMAP::Simple, login(), mailboxes(), select(), get()...
(29) Bio::DB::GenBank, Bio::SeqIO
(30) Spreadsheet::ParseExcel
(31) Text::CSV_XS, parse(), fields(), error_input()
(32) Benchmark
说明:
以下例子 代码的测试是在RH Linux7.2下进行的,Perl版本为5.6.0。
(33) HTTP:: Daemon, accept(), get_request()...
(34) Array::Compare, compare(), full_compare()...
(35) Algorithm::Diff, diff()
(36) List::Util, max(), min(), sum(), maxstr(), minstr()...
(37) HTML::Parser
(38) Mail::Sender
(39) Time::HiRes, gettimeofday(), usleep()
这里接着上面的序号:
(40) Image::Magick
以下模块在RedHat 9.0 ,perl version v5.8.0 built 通过。
(41) Data::SearchReplace
(1)Net::FTP
#!/usr/bin/perl -w # file: ftp_recent.pl # Figure 6.1: Downloading a single file with Net::FTP use Net::FTP; use constant HOST => 'ftp.perl.org'; use constant DIR => '/pub/CPAN'; use constant FILE => 'RECENT'; my $ftp = Net::FTP->new(HOST) or die "Couldn't connect: $@/n"; $ftp->login('anonymous') or die $ftp->message; $ftp->cwd(DIR) or die $ftp->message; $ftp->get(FILE) or die $ftp->message; $ftp->quit; warn "File retrieved successfully./n"; |
(2)Net::Telnet
#!/usr/bin/perl -w #file:remoteps.pl use strict; use Net::Telnet; use constant HOST => 'phage.cshl.org'; use constant USER => 'lstein'; use constant PASS => 'xyzzy'; my $telnet=Net::Telnet->new(HOST); $telnet->login(USER,PASS); my @lines=$telnet->cmd('ps -ef'); print @lines; |
(3)LWP::Simple, get()
#!/usr/bin/perl -w use strict; use LWP::Simple qw(get); my $url = shift || "http://www.chinaunix.net"; my $content = get($url); print $content; exit 0; 最简单方便的get网页的方法。 |
(4) Expect
<!-- php buffer start -->
|
(5) XML::Simple, XMLin()
<!-- php buffer start -->
|
(6) Data::Dumper, Dumper()
<!-- php buffer start -->
|
(7) IO::Socket
<!-- php buffer start -->
|
(8) Date::Manip, DateCalc(), UnixDate()
<!-- php buffer start -->
|
(9) Date::Manip, Date_Cmp()
#用于时间日期的比较
<!-- php buffer start -->
|
(10) File::Find, find()
<!-- php buffer start -->
|
(11) ExtUtils::Installed, new(), modules(), version()
查看已经安装的模块的相应信息。
<!-- php buffer start -->
|
(12) DBI, connect(), prepare(), execute(), fetchrow_array()
<!-- php buffer start -->
|
(13) Getopt::Std
命令行参数解析。
<!-- php buffer start -->
|
(14) Proc::ProcessTable
#直接访问Unix进程表,类似ps command。
<!-- php buffer start -->
|
devel | 03-12-06 23:37 |
(15) Shell
Shell命令直接做为函数,在Perl中调用。
<!-- php buffer start -->#!/usr/bin/perl
usestrict;
useShell;
print"nowis:",date();
print"currenttimeis:",date("+%T");
my@dirs=ls("-laF");
foreach(@dirs)
{
printif(//$/);#printdirectory
}
exit0;
<!-- php buffer end -->
Another use of Time::HiRes Module.
(16) Time::HiRes, sleep(), time()
<!-- php buffer start -->
use Time::HiRes后,此模块提供sleep(), alarm(), time()的增强版以 取代perl内置的相应函数。 其中sleep()和alarm()的参数可以是小数。比如sleep(0.1)表示休眠0.1秒, time()可以返回浮点数。 |
(17) HTML::LinkExtor, links(), parse_file()
<!-- php buffer start -->
__DATA__
代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN"
|
18) Net::Telnet, open(), print(), getline()
<!-- php buffer start -->
|
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()
<!-- php buffer start -->
|
(20) Net::POP3, login(), list(), get()
<!-- php buffer start -->
pop3命令与邮件服务器交互, 简单的命令有: USER name PASS string STAT LIST [n] RETR msg DELE msg NOOP RSET QUIT 有兴趣的朋友可以试一试。 这样,也就可以利用Net::Telnet来做一个收信件的简单程序。 |
(21) Term::ANSIColor 例子一
<!-- php buffer start -->
print "/e[34m/n"; 即是改变前景色为blue; shell命令为echo -e "/033[31m";#改变前景色为红色。 (freeBSD,Solaris下此命令测试OK) |
(21) Term::ANSIColor 例子二
<!-- php buffer start -->
转义序列echo -e "/033[20;40H";可以改变光标位置。 perl中就可以:print "/e[20;40H"; |
(22) Date::Calc Calendar(), Today()
<!-- php buffer start -->
本例子打印出一个2003年6月份的日历,当天日期用红色的闪烁数字表示。 Date::Calc提供了时间日期计算的另一种方式(一种是Date::Manip), 大量简单方便的方法(函数)供使用者调用。 在例子中的年和月我是自己指定的,也可以 ($year, $month, $day) = Today(); 颜色和闪烁是用ANSI escape sequences。 详细说明尽在ANSIColor.pm source和perldoc Term::ANSIColor里。 (perldoc Term::ANSIColor其实也在ANSIColor.pm source里) :) |
(23) Term::Cap, Tgetend(), Tgoto, Tputs()
<!-- php buffer start -->
代码效果:一个左右移动的字串 "welcome to chinaunix! " :) |
(24) HTTPD::Log::Filter
<!-- php buffer start -->
创建对象实例以后,用filter方法来过滤,没有正确匹配的行将返回false, 然后用相应的方法print出我们需要的数据。(host,request,date...等等方法, 由capture选项以参数引入) 可以用re方法打印出作者所使用的匹配模式: use HTTPD::Log::Filter; print HTTPD::Log::Filter->new(format=>"CLF",capture=>['request'])->re; 详见perldoc HTTPD::Log::Filter. enjoy it |
提供者:Apile
(25) Net::LDAP
<!-- php buffer start -->
LDAP version 3..可以用於查詢基本資料、驗證密碼之用.. |
(26) Net::SMTP mail(), to(), data(), datasend(), auth()
<!-- php buffer start -->
Debug模式打开,可以看到详细的SMTP命令 代码。也有助于我们排错。 |
(27) MIME::Base64, encode_base64(), decode_base64()
<!-- php buffer start -->
xOO6w6Osu7bTrcC0tb1jaGluYXVuaXguY29tIFtwZXJsXbDmIQo= 1eLKx2Jhc2U2NLHgwuu1xMD919OjrNPJTUlNRTo6QmFzZTY0xKO/6cC0veLC66GjCg== cGVybGRvYyBNSU1FOjpCYXNlNjQgZm9yIGRldGFpbHMsIGVuam95IGl0IDopCg== 用来处理MIME/BASE64编码。 |
(28) Net::IMAP::Simple, login(), mailboxes(), select(), get()...
<!-- php buffer start -->
在取得中文的Folder时,会出现乱码的情况, 这个问题现在没有解决。英文的Folder则没问题。 IMAP协议,默认端口为143,可以用telnet登录。 telnet imap.xxx.com 143 2 login user pass 2 list "" * 2 select inbox ...... |
(29) Bio::DB::GenBank, Bio::SeqIO
bioperl( http://bioperl.org/)模块使用--生物信息学中用的模块
功能:根据核酸的gi号自动从GenBank中提取FASTA格式的序列,可以多序列提取。
<!-- php buffer start -->#!/usr/bin/perl-w
useBio::DB::GenBank;
useBio::SeqIO;
my$gb=newBio::DB::GenBank;
my$seqout=newBio::SeqIO(-fh=>*STDOUT,-format=>'fasta');
#ifyouwanttogetabunchofsequencesusethebatchmethod
my$seqio=$gb->get_Stream_by_id([qw(275014452981014)]);
while(defined($seq=$seqio->next_seq)){
$seqout->write_seq($seq);
}
<!-- php buffer end -->
提供者:flora
(30) Spreadsheet::ParseExcel perl解析Excel文件的例子。
<!-- php buffer start -->
|
(31) Text::CSV_XS, parse(), fields(), error_input()
如果field里面也包含分隔符(比如"tom,jack,jeff","rose mike",O'neil,"kurt,korn"),那么我们解析起来确实有点麻烦, Text::CSV_XS挺方便。
<!-- php buffer start -->
__DATA__ id,compact_sn,name,type,count,price 37,"ITO-2003-011","台式机,compaq","128M","290","1,2900" 35,I-BJ-2003-010,"显示器,硬盘,内存",'三星',480,"1,4800" 55,"C2003-104",笔记本,"Dell,Latitude,X200",13900,"1,13900" |
提供者:Apile
(32) Benchmark
<!-- php buffer start -->
timethese(做幾次iteration,{ 'Algorithm名稱'=>q{ 要計算時間的algorithm }, 'Algorithm名稱'=>q{ 要計算時間的algorithm } }); |
(33) HTTP
::Daemon, accept(), get_request(), send_file_response()
一个简单的,只能处理单一请求的Web服务器模型。 send_file_response()方法能把Client请求的文件传送过去。
<!-- php buffer start -->
|
(34) Array::Compare, compare(), full_compare()
用于数组比较。 本例实现类似shell command - diff的功能。 如果我们要比较的不是文件,而是比如系统信息,远程文件列表,数据库内容变化等,这个模块会给我们提供方便灵活的操作。
<!-- php buffer start -->
|
(35) Algorithm::Diff, diff()
用于文件比较。 实现类似unix command diff的功能。
<!-- php buffer start -->
|
(36) List::Util, max(), min(), sum(), maxstr(), minstr()...
列表实用工具集。
<!-- php buffer start -->
|
(37) HTML::Parser
解析HTML。本例为找出一个html文本中的所有图片的地址。(即IMG标签中的src) 子程序start中的“$tag =~ /^img$/”为过滤出img标签。 如果换为“$tag =~ /^a$/”,即是找出所有的链接地址。 详细的方法介绍,请见`perldoc HTML::Parser`
<!-- php buffer start -->
|
(38) Mail::Sender
(1)发送附件
<!-- php buffer start -->
|
(2)发送html内容
<!-- php buffer start -->
中的“Sending HTML messages with inline images”及相关部分。 |
40 Image::Magick
提供者: http://www.imagemagick.org/www/perl.html
代码:
#!/usr/local/bin/perl
The script reads three images, crops them, and writes a single image as a GIF animation sequence. In many cases you may want to access individual images of a sequence. The next example illustrates how this is done:
代码:
#!/usr/local/bin/perl
|
41.Data::SearchReplace
代码:
use Data::SearchReplace ('sr');
代码:
use Data::SearchReplace qw(sr);
|