perl常用模块使用例子.doc_第1页
perl常用模块使用例子.doc_第2页
perl常用模块使用例子.doc_第3页
perl常用模块使用例子.doc_第4页
perl常用模块使用例子.doc_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

perl 常用模块使用例子-欢迎大家补充。 一些常用模块的简单描述/perl5.6/pod/perlmodlib.html在perl 中使用模块:模块的下载地址:/modules/01modules.index.html安装模块:1. perl Makefile.PL2. make3. make test4. 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:SearchReplacedevel 03-12-05 21:53 -(1)Net:FTP#!/usr/bin/perl -w# file: ftp_recent.pl# Figure 6.1: Downloading a single file with Net:FTPuse Net:FTP;use constant HOST = ;use constant DIR = /pub/CPAN;use constant FILE = RECENT;my $ftp = Net:FTP-new(HOST) or die Couldnt 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; devel 03-12-05 21:59 -(2)Net:Telnet#!/usr/bin/perl -w#file:remoteps.pluse strict;use Net:Telnet;use constant HOST = ;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; devel 03-12-06 23:22 -(3)LWP:Simple, get()#!/usr/bin/perl -wuse strict;use LWP:Simple qw(get);my $url = shift | ;my $content = get($url);print $content;exit 0; 最简单方便的get网页的方法。 devel 03-12-06 23:25 -(4) Expect -#!/usr/bin/perl use strict; use Expect; my $timeout = 2; my $delay = 1; my $cmd = ssh; my params = qw/202.108.xx.xx -lusername -p22/; my $pass = passwd; my $exp = Expect-spawn($cmd, params) or die Cant spawn $cmdn; $exp-expect($timeout, -re=Ppassword:); $exp-send_slow($delay, $passrn); $exp-interact(); $exp-hard_close(); exit 0; - devel 03-12-06 23:26 -(5) XML:Simple, XMLin() -#!/usr/bin/perl -w use strict; use XML:Simple; my $text = xml; php net.php.servlet php *.php xml my $x = XMLin($text); foreach my $tag(keys %$x) my %h = %$x$tag; foreach(keys %h) print $tag = ; print $_ = $h$_n; exit 0; - devel 03-12-06 23:33 -(6) Data:Dumper, Dumper() -#!/usr/bin/perl -w use strict; use Data:Dumper; print Dumper(INC); print Dumper(%ENV); exit 0; - devel 03-12-06 23:33 -(7) IO:Socket -#!/usr/bin/perl -w use strict; use IO:Socket; my $host = ; my $port = 80; my $http_head = GET / HTTP/1.0nHost: $host:$portnn; my $sock = IO:Socket:INET-new($host:$port) or die Socket() error, Reason : $! n; print $sock $http_head; print ; exit 0; - devel 03-12-06 23:34 -(8) Date:Manip, DateCalc(), UnixDate() -#!/usr/bin/perl use strict; use Date:Manip; my $date=&DateCalc(today,-1 days, 0);#yesterday my $date=&UnixDate($date, %Y-%m-%d %T); print $daten; exit 0; - devel 03-12-06 23:34 -(9) Date:Manip, Date_Cmp()#用于时间日期的比较 -#!/usr/bin/perl use strict; use Date:Manip; my $date1 = Fri Jun 6 18:31:42 GMT 2003; my $date2 = 2003/05/06; my $flag=&Date_Cmp($date1,$date2); if($flagnew(); my modules = $inst-modules(); foreach(modules) my $ver = $inst-version($_) | ?; printf(%-12s - %sn, $_, $ver); exit 0; - devel 03-12-06 23:35 -(12) DBI, connect(), prepare(), execute(), fetchrow_array() -#!/usr/bin/perl use strict; use DBI; my $dbh = DBI-connect(dbi:mysql:dbname, user,passwd, ) or die cant connect!n; my $sql = qq/show variables/; my $sth = $dbh-prepare($sql); $sth-execute(); while(my array=$sth-fetchrow_array() printf(%-35s, $_) foreach(array); print n; $dbh - disconnect(); exit 0; - devel 03-12-06 23:36 -(13) Getopt:Std命令行参数解析。 -#!/usr/bin/perl use strict; use Getopt:Std; my %opts; getopts(c:hv, %opts); foreach(keys %opts) /c/ & print welcome to , $opts$_ | ChinaUnix, !n; /h/ & print Usage : $0 -hv -c msg n; /v/ & print This is demo, version 0.001.001 built for $On; exit 0; - devel 03-12-06 23:36 -(14) Proc:ProcessTable#直接访问Unix进程表,类似ps command。 -#!/usr/bin/perl use strict; use Proc:ProcessTable; my $pt = new Proc:ProcessTable; foreach(reverse sort $pt-table) print $_-pid, = ; print $_-cmndline, n; exit 0; devel 03-12-06 23:37 -(15) Shell -#!/usr/bin/perl use strict; use Shell; print now is : , date(); print current time is : , date(+%T); my dirs = ls(-laF); foreach(dirs) print if(/$/);#print directory exit 0; -Shell命令直接做为函数,在Perl中调用。devel 03-12-06 23:37 -Another use of Time:HiRes Module.(16) Time:HiRes, sleep(), time() -#!/usr/bin/perl use strict; use Time:HiRes qw(sleep time); $| = 1; my $before = time; for my $i (1.100) print $in; sleep(0.01); printf(time used : %.5f secondsn, time - $before); exit 0; -use Time:HiRes后,此模块提供sleep(), alarm(), time()的增强版以取代perl内置的相应函数。其中sleep()和alarm()的参数可以是小数。比如sleep(0.1)表示休眠0.1秒,time()可以返回浮点数。 devel 03-12-06 23:38 -(17) HTML:LinkExtor, links(), parse_file() -#!/usr/bin/perl use strict; use HTML:LinkExtor; my $p = new HTML:LinkExtor; $p-parse_file(*DATA); foreach my $links ($p-links() map print $_ $links; print n; exit 0; -_DATA_代码:-CPAN!- Copyright Jarkko Hietaniemi 1998-2002 All Rights Reserved. The CPAN Logo provided by J.C. Thorpe. You may distribute this document either under the Artistic License (comes with Perl) or the GNU Public License, whichever suits you. You are not allowed to remove or alter these comments. - Comprehensive Perl Archive Network 2003-06-10 online since 1995-10-261662 MB 246 mirrors2903 authors 4767 modules Welcome to CPAN! Here you will find All Things Perl. Browsing Perl modules Perl scripts Perl binary distributions (ports) Perl source code Perl recent arrivals recent Perl modules CPAN sites list CPAN sites mapSearchingPerl core and CPAN modules documentation (Randy Kobes)Perl core documentation (Carlos Ramirez)CPAN modules, distributions, and authors ()CPAN modules documentation (Ulrich Pfeifer)FAQ etcCPAN Frequently Asked QuestionsPerl Mailing ListsPerl BookmarksYours Eclectically, The Self-Appointed Master Librarian (OOK!) of the CPANJarkko HDisclaimerCPAN master site hosted by - devel 03-12-06 23:38 -18) Net:Telnet, open(), print(), getline() -#!/usr/bin/perl use strict; use Net:Telnet; my $p = Net:Telnet-new(); my $h = shift | ; $p-open(Host = $h, Port = 80); $p-print(GET /n); while(my $line = $p-getline() print $line; exit 0; - devel 03-12-06 23:39 -(19) Compress:Zlib, gzopen(), gzreadline(), gzclose() -#!/usr/bin/perl use strict; use Compress:Zlib; my $gz = gzopen(a.gz, rb); while( $gz-gzreadline(my $line) 0 ) chomp $line; print $linen; $gz-gzclose(); exit 0; #直接使用shell的zmore, zless, zcat打开文件也不错,但是如果gz文件很大,还是应该选择zlib。 - devel 03-12-06 23:39 -(20) Net:POP3, login(), list(), get() -#!/usr/bin/perl use strict; use Net:POP3; use Data:Dumper; my $user = user; my $pass = shift or die Usage : $0 passwdn; my $host = ;#pop3 address my $p = Net:POP3-new($host) or die Cant connect $host!n; $p-login($user, $pass) or die user or passwd error!n; my $title = $p-list or die No mail for $usern; foreach my $h(keys %$title) my $msg = $p-get($h); print $msg; $p-quit; exit 0; -telnet 11

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论