LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: devel

perl 常用模块使用例子------欢迎大家补充。

[复制链接]
 楼主| 发表于 2003-12-6 23:35:03 | 显示全部楼层
(10) File::Find, find()[php]
#!/usr/bin/perl -w
use strict;
use File::Find;

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

find(\&process, $path);

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

exit 0;

#用于在unix文件树结构中查找对象。[/php]
 楼主| 发表于 2003-12-6 23:35:31 | 显示全部楼层
(11) ExtUtils::Installed, new(), modules(), version()

查看已经安装的模块的相应信息。[php]
#!/usr/bin/perl
use strict;
use ExtUtils::Installed;

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

foreach(@modules)
{
        my $ver = $inst->version($_) || "???";
        printf("%-12s --  %s\n", $_, $ver);   
}
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:35:49 | 显示全部楼层
(12) DBI, connect(), prepare(), execute(), fetchrow_array()[php]
#!/usr/bin/perl
use strict;
use DBI;

my $dbh = DBI->connect("dbi:mysql:dbname", 'user','passwd', '')
or die "can't 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;[/php]
 楼主| 发表于 2003-12-6 23:36:13 | 显示全部楼层
(13) Getopt::Std

命令行参数解析。[php]
#!/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 $^O\n";
}
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:36:53 | 显示全部楼层
(14) Proc:rocessTable
#直接访问Unix进程表,类似ps command。[php]
#!/usr/bin/perl
use strict;
use Proc:rocessTable;

my $pt = new Proc:rocessTable;

foreach(reverse sort @{$pt->table})
{
   print $_->pid, " => ";
   print $_->cmndline, "\n";
}
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:37:23 | 显示全部楼层
(15) Shell[php]
#!/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;[/php]
Shell命令直接做为函数,在Perl中调用。
 楼主| 发表于 2003-12-6 23:37:46 | 显示全部楼层
Another use of Time::HiRes Module.

(16) Time::HiRes, sleep(), time()[php]
#!/usr/bin/perl
use strict;
use Time::HiRes qw(sleep time);

$| = 1;
my $before = time;
for my $i (1..100)
{
   print "$i\n";
   sleep(0.01);   
}
printf("time used : %.5f seconds\n", time - $before);
exit 0;[/php]


use Time::HiRes后,此模块提供sleep(), alarm(), time()的增强版以
取代perl内置的相应函数。
其中sleep()和alarm()的参数可以是小数。比如sleep(0.1)表示休眠0.1秒,
time()可以返回浮点数。
 楼主| 发表于 2003-12-6 23:38:17 | 显示全部楼层
(17) HTML:inkExtor, links(), parse_file()[php]
#!/usr/bin/perl
use strict;
use HTML:inkExtor;

my $p = new HTML:inkExtor;
$p->parse_file(*DATA);

foreach my $links ($p->links())
{
   map {print "$_ "} @{$links};
   print "\n";
}
exit 0;[/php]



__DATA__

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN"
  2.    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html"/>
  6. <title>CPAN</title>
  7. <!-- Copyright Jarkko Hietaniemi <jhi@iki.fi> 1998-2002
  8.      All Rights Reserved.
  9.      The CPAN Logo provided by J.C. Thorpe.
  10.      You may distribute this document either under the Artistic License
  11.      (comes with Perl) or the GNU Public License, whichever suits you.

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

  17. body{
  18.   color:black;
  19.   background:white;
  20.   margin-left:2%;
  21.   margin-right:2%;
  22. }

  23. h1{
  24.   text-align:center;
  25. }

  26. img     {
  27.   vertical-align:        50%;
  28.   border:       0;
  29. }

  30. .left{
  31.   text-align:left;
  32.   float:none;
  33. }

  34. .center{
  35.   text-align:center;
  36.   float:none;
  37. }

  38. .right{
  39.   text-align:right;
  40.   float:none;
  41. }

  42. -->
  43. </style>
  44. </head>
  45. <body>

  46. <table width="100%">
  47. <tr>
  48.   <td rowspan="2">
  49.    <div class="left">
  50.     <img src="misc/jpg/cpan.jpg"
  51.          alt="[CPAN Logo]" height="121" width="250"/>
  52.    </div>
  53.   </td>
  54.   <td>
  55.    <div class="right">
  56.     <h1><a id="top">Comprehensive Perl Archive Network</a></h1>
  57.    </div>
  58.   </td>
  59. </tr>
  60. <tr>
  61.   <td>
  62.     <div class="center">
  63.     2003-06-10 online since 1995-10-26<br/>1662 MB 246 mirrors<br/>2903 authors 4767 modules
  64.     </div>
  65.   </td>
  66. </tr>
  67. <tr>
  68.   <td colspan="2">
  69.    <p class="left">
  70. Welcome to CPAN! Here you will find All Things Perl.
  71.    </p>
  72.   </td>
  73.   <td>
  74.   </td>
  75. </tr>
  76. </table>

  77. <hr/>

  78. <table width="100%">

  79. <tr>

  80. <td>

  81. <h1>Browsing</h1>
  82. <ul>
  83.   <li><a href="modules/index.html">Perl modules</a></li>
  84.   <li><a href="scripts/index.html">Perl scripts</a></li>
  85.   <li><a href="ports/index.html">Perl binary distributions ("ports")</a></li>
  86.   <li><a href="src/README.html">Perl source code</a></li>
  87.   <li><a href="RECENT.html">Perl recent arrivals</a></li>
  88.   <li><a href="http://search.cpan.org/recent">recent</a> Perl modules</li>
  89.   <li><a href="SITES.html">CPAN sites</a> list</li>
  90.   <li><a href="http://mirrors.cpan.org/">CPAN sites</a> map</li>
  91. </ul>

  92. </td>

  93. <td>

  94. <h1>Searching</h1>

  95. <ul>
  96. <li><a href="http://kobesearch.cpan.org/">Perl core and CPAN modules documentation </a> (Randy Kobes)</li>
  97. <li><a href="http://www.perldoc.com/">Perl core documentation</a> (Carlos Ramirez)</li>
  98. <li><a href="http://search.cpan.org/">CPAN modules, distributions, and authors</a> (search.cpan.org)</li>
  99. <li><a href="http://wait.cpan.org/">CPAN modules documentation</a> (Ulrich Pfeifer)</li>
  100. </ul>

  101. <h1>FAQ etc</h1>

  102. <ul>
  103. <li><a href="misc/cpan-faq.html">CPAN Frequently Asked Questions</a></li>
  104. <li><a href="http://lists.cpan.org/">Perl Mailing Lists</a></li>
  105. <li><a href="http://bookmarks.cpan.org/">Perl Bookmarks</a></li>
  106. </ul>

  107. <p><small>
  108. Yours Eclectically, The Self-Appointed Master Librarian (OOK!) of the CPAN<br/>
  109. <i>Jarkko Hietaniemi</i>
  110. <a href="mailto:cpan@perl.org">cpan@perl.org</a>
  111. <a href="disclaimer.html">[Disclaimer]</a>
  112. </small>
  113. </p>

  114. </td>

  115. </tr>

  116. </table>

  117. <hr/>

  118. <table width="100%">
  119. <tr>

  120. <td>
  121. <div class="left">
  122. <a href="http://validator.w3.org/check?uri=http%3A%2F%2Fwww.cpan.org%2Findex.html">
  123. <img src="misc/gif/valid-xhtml10.gif" alt="Valid XHTML 1.0!" height="31" width="88"/></a>
  124. <a href="http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.cpan.org%2Findex.html">
  125. <img src="misc/gif/vcss.gif" alt="[Valid CSS]" height="31" width="88"/></a>
  126. </div>
  127. </td>
  128. <td>
  129. <div class="right">

  130. <table width="100%">

  131. <tr>
  132. <td class="right">
  133. <small>
  134. CPAN master site hosted by
  135. </small>
  136. </td>
  137. </tr>
  138. <tr>
  139. <td class="right">
  140. <a href="http://www.csc.fi/suomi/funet/verkko.html.en/"><img src="misc/gif/funet.gif" alt="FUNET" height="25" width="88"/></a>
  141. </td>
  142. </tr>
  143. </table>

  144. </div>
  145. </td>

  146. </tr>
  147. </table>

  148. </body>
  149. </html>
复制代码
 楼主| 发表于 2003-12-6 23:38:58 | 显示全部楼层
18) Net::Telnet, open(), print(), getline()[php]
#!/usr/bin/perl
use strict;
use Net::Telnet;

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

$p->open(Host => $h, Port => 80);
$p->print("GET /\n");
while(my $line = $p->getline())
{
   print $line;
}
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:39:19 | 显示全部楼层
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()[php]
#!/usr/bin/perl
use strict;
use Compress::Zlib;

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

while( $gz->gzreadline(my $line) > 0 )
{
   chomp $line;
   print "$line\n";
}

$gz->gzclose();
exit 0;

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

本版积分规则

快速回复 返回顶部 返回列表