1、perl 常用模块使用例子- 欢迎大家补充。一些常用模块的简单描述http:/ perl 中使用模块:模块的下载地址:http:/www.cpan.org/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:
2、FTP已有模块:(以下的内容转自 CU,谢谢 CU 的朋友)说明:以下例子代码的测试是在 FreeBSD use constant HOST = ftp.perl.org;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 $ft
3、p-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 = phage.cshl.org;use constant USER = lstein;use constant PASS = xyzzy;my $telnet=Net:Telnet-new(HOST);$telnet-login(USER,PASS);m
4、y 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 | “http:/“;my $content = get($url);print $content;exit 0; 最简单方便的 get 网页的方法。 devel 03-12-06 23:25 -(4) Expect -#!/usr/bin/perl use strict; use Expect;
5、 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;
6、-devel 03-12-06 23:26 -(5) XML:Simple, XMLin() -#!/usr/bin/perl -w use strict; use XML:Simple; my $text = 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:D
7、umper, 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:Sock
8、et: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=#yesterday my $date= print “$daten“; exit 0; -devel 03-12-06 23:34 -(9) Date:
9、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= if($flagnew(); my modules = $inst-modules(); foreach(modules) my $ver = $inst-version($_) | “?“; printf(“%-12s - %sn“, $_, $ver); exit 0; -devel 03-
10、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_ar
11、ray() 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/ /h/ /v/ exit 0; -devel 03-12-06 23:36 -(14) Proc:ProcessTable#直接访问 U
12、nix 进程表,类似 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 “c
13、urrent 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 $befor
14、e = time; for my $i (1100) 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()
15、, 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_代码:-CPAN1998-2002All Rights Reserved.The CPAN Logo provided by J.C. Thorpe.You may distribute this documen
16、t 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 Network2003-06-10 online since 1995-10-261662 MB 246 mirrors2903 authors 4767 modulesWelcome to CPAN! Here you will fi
17、nd All Things Perl.BrowsingPerl modulesPerl scriptsPerl binary distributions (“ports“)Perl source codePerl recent arrivalsrecent Perl modulesCPAN sites listCPAN sites mapSearchingPerl core and CPAN modules documentation (Randy Kobes)Perl core documentation (Carlos Ramirez)CPAN modules, distributions
18、, and authors (search.cpan.org)CPAN modules documentation (Ulrich Pfeifer)FAQ etcCPAN Frequently Asked QuestionsPerl Mailing ListsPerl BookmarksYours Eclectically, The Self-Appointed Master Librarian (OOK!) of the CPANJarkko Hietaniemicpanperl.orgDisclaimerCPAN master site hosted by-devel 03-12-06 2
19、3: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(), gzrea
20、dline(), 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(),
21、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-lis
22、t or die “No mail for $usern“; foreach my $h(keys %$title) my $msg = $p-get($h); print $msg; $p-quit; exit 0; -telnet 110 也可以直接连到 pop3 server 上,然后通过pop3 命令与邮件服务器交互,简单的命令有:USER namePASS stringSTATLIST nRETR msgDELE msgNOOPRSETQUIT有兴趣的朋友可以试一试。这样,也就可以利用 Net:Telnet 来做一个收信件的简单程序。 devel 03-12-06 23:40 -(
23、21) Term:ANSIColor 例子一 -#!/usr/bin/perl use strict; use Term:ANSIColor qw(:constants); $Term:ANSIColor:AUTORESET = 1; $| = 1; my $str = “Welcome to chinaunix _!n“; for my $i(0length($str)-1) print BOLD RED substr($str, $i, 1); select(undef, undef, undef, 0.3); exit 0; -查看 ANSIColor.pm 可以得知作者是利用 ANSI
24、 转义序列,改变终端字符颜色的。print “e34mn“;即是改变前景色为 blue;shell 命令为 echo -e “03331m“;#改变前景色为红色。(freeBSD,Solaris 下此命令测试 OK) devel 03-12-06 23:41 -(21) Term:ANSIColor 例子二 -#!/usr/bin/perl use strict; use Term:ANSIColor qw(:constants); $Term:ANSIColor:AUTORESET = 1; $| = 1; print “e20;40H“; my $str = “Welcome to chi
25、naunix _!n“; print BOLD BLINK $str; exit 0; -转义序列 echo -e “03320;40H“;可以改变光标位置。perl 中就可以:print “e20;40H“; devel 03-12-06 23:42 -(22) Date:Calc Calendar(), Today() -#!/usr/bin/perl use strict; use Date:Calc qw(Calendar Today); my $year = “2003“; my $month = “6“; my $day; my $cal = Calendar($year, $mo
26、nth); (undef, undef, $day) = Today(); $cal = s/$day/e5me31m$daye0m/; print $cal; exit 0; -本例子打印出一个 2003 年 6 月份的日历,当天日期用红色的闪烁数字表示。Date:Calc 提供了时间日期计算的另一种方式(一种是 Date:Manip) ,大量简单方便的方法(函数)供使用者调用。在例子中的年和月我是自己指定的,也可以($year, $month, $day) = Today();颜色和闪烁是用 ANSI escape sequences。详细说明尽在 ANSIColor.pm source
27、和 perldoc Term:ANSIColor 里。(perldoc Term:ANSIColor 其实也在 ANSIColor.pm source 里) :) devel 03-12-06 23:42 -(23) Term:Cap, Tgetend(), Tgoto, Tputs() -#!/usr/bin/perl use strict; use Term:Cap; $| = 1; my $i = 1; my $flag = 0; my $tcap = Term:Cap-Tgetent(TERM = undef, OSPEED = 1); $tcap-Tputs(cl, 1, *STDO
28、UT);#clear screen while($i) if($i 50 | $flag = 1) $i -; $flag = 1; $flag = 0 if($i = 1); else $i +; $flag = 0; $tcap-Tgoto(cm, $i, 15, *STDOUT);#move cursor print “ welcome to chinaunix! “; select(undef, undef, undef, 0.02); exit 0; -Term:Cap 终端控制模块。代码效果:一个左右移动的字串 “welcome to chinaunix! “ :) devel 0
29、3-12-06 23:43 -(24) HTTPD:Log:Filter -#!/usr/bin/perl use strict; use HTTPD:Log:Filter; my $filter = HTTPD:Log:Filter-new(format = “CLF“, capture = request, host); foreach(cat access_log) chomp; unless( $filter-filter($_) ) print “$_n“; next; print $filter-request, “n“; exit 0; -如果我们工作中经常需要分析 Apache
30、 日志,这个模块可以提供一些方便。创建对象实例以后,用 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 devel 03-12-06 23:54 -提供者:Apile
31、(25) Net:LDAP -#!/usr/bin/perl use Net:LDAP; # get a object of ldap $ldap = Net:LDAP-new(“1.1.1.1“, port =“389“, version = 3) or die “$“; # object of Net:LDAP:Message $mesg = $ldap-bind($_cer_id, password = $_cer_pw); # 查詢用的 ID/PASSWD if($mesg-is_error) die $mesg-error; $mesg = $ldap-search( base = “o=abc,c=tt“, # 起始點 scope = “sub“, # 範圍 filter = “(uid=apile)“, # 條件 attrs = “cn“, # 要取得的 attribute typesonly = 0 ); my $max