LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 34553|回复: 47

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

[复制链接]
发表于 2003-12-5 21:51:01 | 显示全部楼层 |阅读模式
一些常用模块的简单描述
http://www.perldoc.com/perl5.6/pod/perlmodlib.html

在perl 中使用模块:
模块的下载地址:http://www.cpan.org/modules/01modules.index.html
安装模块:
1. perl Makefile.PL
2. make
3. make test
4. 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:umper, 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:rocessTable
(15) Shell
(16) Time::HiRes, sleep(), time()
(17) HTML:inkExtor, links(), parse_file()
(18) Net::Telnet, open(), print(), getline()
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()
(20) Net:OP3, login(), list(), get()
(21) Term::ANSIColor
(22) Date::Calc Calendar(), Today()
(23) Term::Cap, Tgetend(), Tgoto, Tputs()
(24) HTTPD:og::Filter
(25) Net:DAP
(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:B::GenBank, Bio::SeqIO
(30) Spreadsheet:arseExcel
(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:iff, diff()
(36) List::Util, max(), min(), sum(), maxstr(), minstr()...
(37) HTML:arser
(38) Mail::Sender
(39) Time::HiRes, gettimeofday(), usleep()


这里接着上面的序号:
(40) Image::Magick

以下模块在RedHat 9.0 ,perl version v5.8.0 built 通过。
(41) Data::SearchReplace
 楼主| 发表于 2003-12-5 21:53:43 | 显示全部楼层
(1)Net::FTP


#!/usr/bin/perl -w
# file: ftp_recent.pl
# Figure 6.1: Downloading a single file with Net::FTP

use Net::FTP;

use constant HOST => 'ftp.perl.org';
use constant DIR  => '/pub/CPAN';
use constant FILE => 'RECENT';

my $ftp = Net::FTP->new(HOST) or die "Couldn't 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";
 楼主| 发表于 2003-12-5 21:59:49 | 显示全部楼层
(2)Net::Telnet

#!/usr/bin/perl -w
#file:remoteps.pl
use 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);
my @lines=$telnet->cmd('ps -ef');
print @lines;
 楼主| 发表于 2003-12-6 23:22:54 | 显示全部楼层
(3)LWP::Simple, get()
#!/usr/bin/perl -w
use strict;
use LWP::Simple qw(get);

my $url = shift || "http://www.chinaunix.net";
my $content = get($url);

print $content;

exit 0;
最简单方便的get网页的方法。
 楼主| 发表于 2003-12-6 23:25:09 | 显示全部楼层
(4) Expect [php]
#!/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 "Can't spawn $cmd\n";
$exp->expect($timeout, -re=>'[Pp]assword:');
$exp->send_slow($delay, "$pass\r\n");

$exp->interact();
$exp->hard_close();

exit 0;[/php]
 楼主| 发表于 2003-12-6 23:26:19 | 显示全部楼层
(5) XML::Simple, XMLin()[php]
#!/usr/bin/perl -w
use strict;
use XML::Simple;
my $text = <<xml;
< ?xml version="1.0"? >
<web-app>
  <servlet>
    <servlet-name>php</servlet-name>
    <servlet-class>net.php.servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>php</servlet-name>
    <url-pattern>*.php</url-pattern>
  </servlet-mapping>
</web-app>
xml
my $x = XMLin($text);
foreach my $tag(keys %$x)
{      
   my %h = %{$$x{$tag}};
   foreach(keys %h)
   {      
      print "$tag => ";
      print "$_ => $h{$_}\n";
   }
}
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:33:11 | 显示全部楼层
(6) Data:umper, Dumper()[php]
#!/usr/bin/perl -w
use strict;
use Data:umper;

print Dumper(\@INC);
print Dumper(\%ENV);
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:33:40 | 显示全部楼层
(7) IO::Socket[php]
#!/usr/bin/perl -w
use strict;
use IO::Socket;

my $host = "www.chinaunix.net";
my $port = "80";
my $http_head = "GET / HTTP/1.0\nHost: $hostport\n\n";
my $sock = IO::Socket::INET->new("$hostport")
or die "Socket() error, Reason : $! \n";

print $sock $http_head;
print <$sock>;

exit 0;[/php]
 楼主| 发表于 2003-12-6 23:34:04 | 显示全部楼层
(8) Date::Manip, DateCalc(), UnixDate()[php]
#!/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 "$date\n";
exit 0;[/php]
 楼主| 发表于 2003-12-6 23:34:27 | 显示全部楼层
(9) Date::Manip, Date_Cmp()
#用于时间日期的比较[php]
#!/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($flag<0)
{
   print "date1 is earlier!\n";
}
elsif($flag==0)
{
   print "the two dates are identical!\n";
}
else
{
   print "date2 is earlier!\n";
}
exit 0;[/php]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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