免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: deathcult
打印 上一主题 下一主题

Perl模块使用 => 简短例子代码集合!  关闭 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2003-06-06 18:53 |只看该作者

Perl模块使用 => 简短例子代码集合!

(10) File::Find, find()

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use File::Find;

  4. my $file = "access.log";
  5. my $path = "/";

  6. find(\&process, $path);

  7. sub process{ print $File::Find::dir, "$_\n" if(/$file/); }

  8. exit 0;
复制代码
   

#用于在unix文件树结构中查找对象。

论坛徽章:
0
12 [报告]
发表于 2003-06-09 21:55 |只看该作者

Perl模块使用 => 简短例子代码集合!

(11) ExtUtils::Installed, new(), modules(), version()

查看已经安装的模块的相应信息。


  1. #!/usr/bin/perl
  2. use strict;
  3. use ExtUtils::Installed;

  4. my $inst= ExtUtils::Installed->;new();
  5. my @modules = $inst->;modules();

  6. foreach(@modules)
  7. {
  8.         my $ver = $inst->;version($_) || "???";
  9.         printf("%-12s --  %s\n", $_, $ver);       
  10. }
  11. exit 0;
复制代码

论坛徽章:
0
13 [报告]
发表于 2003-06-09 21:56 |只看该作者

Perl模块使用 => 简短例子代码集合!

(12) DBI, connect(), prepare(), execute(), fetchrow_array()

  1. #!/usr/bin/perl
  2. use strict;
  3. use DBI;

  4. my $dbh = DBI->;connect("dbi:mysql:dbname", 'user','passwd', '')
  5. or die "can't connect!\n";
  6. my $sql = qq/show variables/;
  7. my $sth = $dbh->;prepare($sql);
  8. $sth->;execute();

  9. while(my @array=$sth->;fetchrow_array())
  10. {
  11.         printf("%-35s", $_) foreach(@array);
  12.         print "\n";
  13. }
  14. $dbh ->; disconnect();
  15. exit 0;
复制代码

论坛徽章:
0
14 [报告]
发表于 2003-06-09 21:57 |只看该作者

Perl模块使用 => 简短例子代码集合!

(13) Getopt::Std

命令行参数解析。


  1. #!/usr/bin/perl
  2. use strict;
  3. use Getopt::Std;

  4. my %opts;
  5. getopts("c:hv", \%opts);

  6. foreach(keys %opts)
  7. {
  8.         /c/ && print "welcome to ", $opts{$_} || "ChinaUnix", "!\n";
  9.         /h/ && print "Usage : $0 -[hv] -[c msg] \n";
  10.         /v/ && print "This is demo, version 0.001.001 built for $^O\n";
  11. }
  12. exit 0;
复制代码

论坛徽章:
0
15 [报告]
发表于 2003-06-09 21:58 |只看该作者

Perl模块使用 => 简短例子代码集合!

(14) Proc:rocessTable
#直接访问Unix进程表,类似ps command。

  1. #!/usr/bin/perl
  2. use strict;
  3. use Proc::ProcessTable;

  4. my $pt = new Proc::ProcessTable;

  5. foreach(reverse sort @{$pt->;table})
  6. {
  7.         print $_->;pid, " =>; ";
  8.         print $_->;cmndline, "\n";
  9. }
  10. exit 0;
复制代码

论坛徽章:
0
16 [报告]
发表于 2003-06-09 21:58 |只看该作者

Perl模块使用 => 简短例子代码集合!

(15) Shell

  1. #!/usr/bin/perl
  2. use strict;
  3. use Shell;

  4. print "now is : ", date();
  5. print "current time is : ", date("+%T");

  6. my @dirs = ls("-laF");
  7. foreach(@dirs)
  8. {
  9.         print if(/\/$/);#print directory
  10. }
  11. exit 0;
复制代码
   

Shell命令直接做为函数,在Perl中调用。

论坛徽章:
0
17 [报告]
发表于 2003-06-10 13:59 |只看该作者

Perl模块使用 => 简短例子代码集合!

Another use of Time::HiRes Module.

(16) Time::HiRes, sleep(), time()

  1. #!/usr/bin/perl
  2. use strict;
  3. use Time::HiRes qw(sleep time);

  4. $| = 1;
  5. my $before = time;
  6. for my $i (1..100)
  7. {
  8.         print "$i\n";
  9.         sleep(0.01);       
  10. }
  11. printf("time used : %.5f seconds\n", time - $before);
  12. exit 0;
复制代码
   

use Time::HiRes后,此模块提供sleep(), alarm(), time()的增强版以
取代perl内置的相应函数。
其中sleep()和alarm()的参数可以是小数。比如sleep(0.1)表示休眠0.1秒,
time()可以返回浮点数。

论坛徽章:
0
18 [报告]
发表于 2003-06-10 16:06 |只看该作者

Perl模块使用 => 简短例子代码集合!

(17) HTML::LinkExtor, links(), parse_file()

  1. #!/usr/bin/perl
  2. use strict;
  3. use HTML::LinkExtor;

  4. my $p = new HTML::LinkExtor;
  5. $p->;parse_file(*DATA);

  6. foreach my $links ($p->;links())
  7. {
  8.         map {print "$_ "} @{$links};
  9.         print "\n";
  10. }
  11. exit 0;



  12. __DATA__

  13. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN"
  14.    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">;
  15. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">;
  16. <head>;
  17. <meta http-equiv="Content-Type" content="text/html"/>;
  18. <title>;CPAN</title>;
  19. <!-- Copyright Jarkko Hietaniemi <jhi@iki.fi>; 1998-2002
  20.      All Rights Reserved.
  21.      The CPAN Logo provided by J.C. Thorpe.
  22.      You may distribute this document either under the Artistic License
  23.      (comes with Perl) or the GNU Public License, whichever suits you.

  24.      You are not allowed to remove or alter these comments. -->;
  25. <!-- $Id: cpan-index.html,v 1.7 2003/02/17 10:23:46 jhi Exp $ -->;
  26. <link rev="made" href="mailto:cpan@perl.org">;</link>;
  27. <style type="text/css">;
  28. <!--

  29. body{
  30.   color:black;
  31.   background:white;
  32.   margin-left:2%;
  33.   margin-right:2%;
  34. }

  35. h1{
  36.   text-align:center;
  37. }

  38. img     {
  39.   vertical-align:        50%;
  40.   border:       0;
  41. }

  42. .left{
  43.   text-align:left;
  44.   float:none;
  45. }

  46. .center{
  47.   text-align:center;
  48.   float:none;
  49. }

  50. .right{
  51.   text-align:right;
  52.   float:none;
  53. }

  54. -->;
  55. </style>;
  56. </head>;
  57. <body>;

  58. <table width="100%">;
  59. <tr>;
  60.   <td rowspan="2">;
  61.    <div class="left">;
  62.     <img src="misc/jpg/cpan.jpg"
  63.          alt="[CPAN Logo]" height="121" width="250"/>;
  64.    </div>;
  65.   </td>;
  66.   <td>;
  67.    <div class="right">;
  68.     <h1>;<a id="top">;Comprehensive Perl Archive Network</a>;</h1>;
  69.    </div>;
  70.   </td>;
  71. </tr>;
  72. <tr>;
  73.   <td>;
  74.     <div class="center">;
  75.     2003-06-10 online since 1995-10-26<br/>;1662 MB 246 mirrors<br/>;2903 authors 4767 modules
  76.     </div>;
  77.   </td>;
  78. </tr>;
  79. <tr>;
  80.   <td colspan="2">;
  81.    <p class="left">;
  82. Welcome to CPAN! Here you will find All Things Perl.
  83.    </p>;
  84.   </td>;
  85.   <td>;
  86.   </td>;
  87. </tr>;
  88. </table>;

  89. <hr/>;

  90. <table width="100%">;

  91. <tr>;

  92. <td>;

  93. <h1>;Browsing</h1>;
  94. <ul>;
  95.   <li>;<a href="modules/index.html">;Perl modules</a>;</li>;
  96.   <li>;<a href="scripts/index.html">;Perl scripts</a>;</li>;
  97.   <li>;<a href="ports/index.html">;Perl binary distributions ("ports")</a>;</li>;
  98.   <li>;<a href="src/README.html">;Perl source code</a>;</li>;
  99.   <li>;<a href="RECENT.html">;Perl recent arrivals</a>;</li>;
  100.   <li>;<a href="http://search.cpan.org/recent">;recent</a>; Perl modules</li>;
  101.   <li>;<a href="SITES.html">;CPAN sites</a>; list</li>;
  102.   <li>;<a href="http://mirrors.cpan.org/">;CPAN sites</a>; map</li>;
  103. </ul>;

  104. </td>;

  105. <td>;

  106. <h1>;Searching</h1>;

  107. <ul>;
  108. <li>;<a href="http://kobesearch.cpan.org/">;Perl core and CPAN modules documentation </a>; (Randy Kobes)</li>;
  109. <li>;<a href="http://www.perldoc.com/">;Perl core documentation</a>; (Carlos Ramirez)</li>;
  110. <li>;<a href="http://search.cpan.org/">;CPAN modules, distributions, and authors</a>; (search.cpan.org)</li>;
  111. <li>;<a href="http://wait.cpan.org/">;CPAN modules documentation</a>; (Ulrich Pfeifer)</li>;
  112. </ul>;

  113. <h1>;FAQ etc</h1>;

  114. <ul>;
  115. <li>;<a href="misc/cpan-faq.html">;CPAN Frequently Asked Questions</a>;</li>;
  116. <li>;<a href="http://lists.cpan.org/">;Perl Mailing Lists</a>;</li>;
  117. <li>;<a href="http://bookmarks.cpan.org/">;Perl Bookmarks</a>;</li>;
  118. </ul>;

  119. <p>;<small>;
  120. Yours Eclectically, The Self-Appointed Master Librarian (OOK!) of the CPAN<br/>;
  121. <i>;Jarkko Hietaniemi</i>;
  122. <a href="mailto:cpan@perl.org">;cpan@perl.org</a>;
  123. <a href="disclaimer.html">;[Disclaimer]</a>;
  124. </small>;
  125. </p>;

  126. </td>;

  127. </tr>;

  128. </table>;

  129. <hr/>;

  130. <table width="100%">;
  131. <tr>;

  132. <td>;
  133. <div class="left">;
  134. <a href="http://validator.w3.org/check?uri=http%3A%2F%2Fwww.cpan.org%2Findex.html">;
  135. <img src="misc/gif/valid-xhtml10.gif" alt="Valid XHTML 1.0!" height="31" width="88"/>;</a>;
  136. <a href="http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.cpan.org%2Findex.html">;
  137. <img src="misc/gif/vcss.gif" alt="[Valid CSS]" height="31" width="88"/>;</a>;
  138. </div>;
  139. </td>;
  140. <td>;
  141. <div class="right">;

  142. <table width="100%">;

  143. <tr>;
  144. <td class="right">;
  145. <small>;
  146. CPAN master site hosted by
  147. </small>;
  148. </td>;
  149. </tr>;
  150. <tr>;
  151. <td class="right">;
  152. <a href="http://www.csc.fi/suomi/funet/verkko.html.en/">;<img src="misc/gif/funet.gif" alt="FUNET" height="25" width="88"/>;</a>;
  153. </td>;
  154. </tr>;
  155. </table>;

  156. </div>;
  157. </td>;

  158. </tr>;
  159. </table>;

  160. </body>;
  161. </html>;
复制代码

论坛徽章:
0
19 [报告]
发表于 2003-06-11 11:47 |只看该作者

Perl模块使用 => 简短例子代码集合!

(18) Net::Telnet, open(), print(), getline()

  1. #!/usr/bin/perl
  2. use strict;
  3. use Net::Telnet;

  4. my $p = Net::Telnet->;new();
  5. my $h = shift || "www.chinaunix.net";

  6. $p->;open(Host =>; $h, Port =>; 80);
  7. $p->;print("GET /\n");
  8. while(my $line = $p->;getline())
  9. {
  10.         print $line;
  11. }
  12. exit 0;
复制代码

论坛徽章:
0
20 [报告]
发表于 2003-06-11 14:21 |只看该作者

Perl模块使用 => 简短例子代码集合!

(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()

  1. #!/usr/bin/perl
  2. use strict;
  3. use Compress::Zlib;

  4. my $gz = gzopen("a.gz", "rb");

  5. while( $gz->;gzreadline(my $line) >; 0 )
  6. {
  7.         chomp $line;
  8.         print "$line\n";
  9. }

  10. $gz->;gzclose();
  11. exit 0;
复制代码


#直接使用shell的zmore, zless, zcat打开文件也不错,
但是如果gz文件很大,还是应该选择zlib。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP