Perl常用模块使用例子(转载)3

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

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

#!/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;

——————————————————————–
(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 “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;

————————————————————————
(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
$^O\n”;
}
exit 0;

————————————————————————
(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;

——————————————————————–
(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中调用。

———————————————————————

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

#!/usr/bin/perl
#Another use of Time::HiRes Module.

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;

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

———————————————————————-
(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__

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1 Strict//EN”
“http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”
xml:lang=”en-US”>
<head>
<meta http-equiv=”Content-Type” content=”text/html”/>
<title>CPAN</title>
<!– Copyright Jarkko Hietaniemi <jhi@iki.fi> 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. –>
<!– $Id: cpan-index.html,v 1.7 2003/02/17 10:23:46 jhi Exp $ –>
<link rev=”made”
href=”mailto:cpan@perl.org”></link>
<style type=”text/css”>
<!–

body{
color:black;
background:white;
margin-left:2%;
margin-right:2%;
}

h1{
text-align:center;
}

img {
vertical-align: 50%;
border: 0;
}

.left{
text-align:left;
float:none;
}

.center{
text-align:center;
float:none;
}

.right{
text-align:right;
float:none;
}

–>
</style>
</head>
<body>

<table width=”100%”>
<tr>
<td rowspan=”2″>
<div class=”left”>
<img src=”misc/jpg/cpan.jpg”
alt=”[CPAN Logo]” height=”121″ width=”250″/>
</div>
</td>
<td>
<div class=”right”>
<h1><a id=”top”>Comprehensive Perl Archive
Network</a></h1>
</div>
</td>
</tr>
<tr>
<td>
<div class=”center”>
2003-06-10 online since 1995-10-26<br/>1662 MB 246 mirrors<br/>2903
authors 4767 modules
</div>
</td>
</tr>
<tr>
<td colspan=”2″>
<p class=”left”>
Welcome to CPAN! Here you will find All Things Perl.
</p>
</td>
<td>
</td>
</tr>
</table>

<hr/>

<table width=”100%”>

<tr>

<td>

<h1>Browsing</h1>
<ul>
<li><a href=”modules/index.html”>Perl
modules</a></li>
<li><a href=”scripts/index.html”>Perl
scripts</a></li>
<li><a href=”ports/index.html”>Perl binary distributions
(“ports”)</a></li>
<li><a href=”src/README.html”>Perl source
code</a></li>
<li><a href=”RECENT.html”>Perl recent
arrivals</a></li>
<li><a
href=”http://search.cpan.org/recent”>recent</a> Perl
modules</li>
<li><a href=”SITES.html”>CPAN
_fcksavedurl=”"SITES.html”>CPAN” sites</a>
list</li>
<li><a href=”http://mirrors.cpan.org/”>CPAN
sites</a> map</li>
</ul>

</td>

<td>

<h1>Searching</h1>

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

<h1>FAQ etc</h1>

<ul>
<li><a href=”misc/cpan-faq.html”>CPAN Frequently Asked
Questions</a></li>
<li><a href=”http://lists.cpan.org/”>Perl Mailing
Lists</a></li>
<li><a href=”http://bookmarks.cpan.org/”>Perl
Bookmarks</a></li>
</ul>

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

</td>

</tr>

</table>

<hr/>

<table width=”100%”>
<tr>

<td>
<div class=”left”>
<a
href=”http://validator.w3.org/check?uri=http%3A%2F%2Fwww.cpan.org%2Findex.html”>
<img src=”misc/gif/valid-xhtml10.gif” alt=”Valid XHTML
1.0!” height=”31″ width=”88″/></a>
<a
href=”http://jigsaw.w3.org/css-validator/validator?uri=http%3A%2F%2Fwww.cpan.org%2Findex.html”>
<img src=”misc/gif/vcss.gif” alt=”[Valid CSS]”
height=”31″ width=”88″/></a>
</div>
</td>
<td>
<div class=”right”>

<table width=”100%”>

<tr>
<td class=”right”>
<small>
CPAN master site hosted by
</small>
</td>
</tr>
<tr>
<td class=”right”>
<a href=”http://www.csc.fi/suomi/funet/verkko.html.en/”><img
src=”misc/gif/funet.gif” alt=”FUNET” height=”25″
width=”88″/></a>
</td>
</tr>
</table>

</div>
</td>

</tr>
</table>

</body>
</html>

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

#!/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;

——————————————————————————–
(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 “$line\n”;
}

$gz->gzclose();
exit 0;

#直接使用shell的zmore,
zless, zcat打开文件也不错,但是如果gz文件很大,还是应该选择zlib。

——————————————————————————–
(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 passwd\n”;
my $host = “pop3.web.com”;#pop3 address

my $p = Net::POP3->new($host) or die “Can’t connect $host!\n”;
$p->login($user, $pass) or die “user or passwd error!\n”;
my $title = $p->list or die “No mail for $user\n”;

foreach my $h(keys %$title)
{
my $msg = $p->get($h);
print @$msg;
}
$p->quit;
exit 0;

telnet pop3.web.com 110 也可以直接连到pop3 server上,然后通过pop3命令与邮件服务器交互,

简单的命令有:

USER name
PASS string
STAT
LIST [n]
RETR msg
DELE msg
NOOP
RSET
QUIT

有兴趣的朋友可以试一试。
这样,也就可以利用Net::Telnet来做一个收信件的简单程序。

——————————————————————————–

相关日志

One Response to this post.

  1. Posted by w3cvalidation on 28.01.10 at 4:11 下午

    Nice information, I really appreciate the way you presented.Thanks for sharing..

    [Reply]

Respond to this post