设为首页 收藏本站
查看: 1407|回复: 0

[经验分享] perl 常用module的使用

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-12-25 14:20:40 | 显示全部楼层 |阅读模式
  一些常用模块的简单描述 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::Dumper, 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::ProcessTable
(15) Shell
(16) Time::HiRes, sleep(), time()
(17) HTML::LinkExtor, links(), parse_file()
(18) Net::Telnet, open(), print(), getline()
(19) Compress::Zlib, gzopen(), gzreadline(), gzclose()
(20) Net::POP3, login(), list(), get()
(21) Term::ANSIColor
(22) Date::Calc Calendar(), Today()
(23) Term::Cap, Tgetend(), Tgoto, Tputs()
(24) HTTPD::Log::Filter
(25) Net::LDAP
(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::DB::GenBank, Bio::SeqIO
(30) Spreadsheet::ParseExcel
(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::Diff, diff()
(36) List::Util, max(), min(), sum(), maxstr(), minstr()...
(37) HTML::Parser
(38) Mail::Sender
(39) Time::HiRes, gettimeofday(), usleep()
(40) Image::Magick
  以下模块在RedHat 9.0 ,perl version v5.8.0 built 通过。
(41) Data::SearchReplace
----------------------------------------------------------
(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: ";
$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";
  -----------------------------------------------------------
(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;
  --------------------------------------------------------------
(3)LWP::Simple, get()
#!/usr/bin/perl -w
use strict;
use LWP::Simple qw(get);
  my $url = shift || "http://www.iyunv.com";
my $content = get($url);
  print $content;
  exit 0;
最简单方便的get网页的方法。
  -------------------------------------------------------------
(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;
  -----------------------------------------------------------------
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;
  ----------------------------------------------------------------
(6) Data::Dumper, Dumper()
  PHP代码:
  #!/usr/bin/perl -w
use strict;
use Data::Dumper;
  print Dumper(@INC);
print Dumper(%ENV);
exit 0;
  -------------------------------------
(7) IO::Socket
  PHP代码:
  #!/usr/bin/perl -w
use strict;
use IO::Socket;
  my $host = "www.iyunv.com";
my $port = "80";
my $http_head = "GET / HTTP/1.0\nHost: $host:$port\n\n";
my $sock = IO::Socket::INET->new("$host:$port")
or die "Socket() error, Reason : $! \n";
  print $sock $http_head;
print <$sock>;
  exit 0;
  ---------------------------------------------------------------
(8) Date::Manip, DateCalc(), UnixDate()
  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;
  --------------------------------------------------------------------
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文件树结构中查找对象。
  ---------------------------------------------------------------
(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;
  --------------------------------------------------------------------
(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;
  ------------------------------------------------------------------------
(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;
  ------------------------------------------------------------------------
(14) Proc::ProcessTable
  #直接访问Unix进程表,类似ps command。
  PHP代码:
  #!/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
  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;
  Shell命令直接做为函数,在Perl中调用。
  ---------------------------------------------------------------------
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;
  
use Time::HiRes后,此模块提供sleep(), alarm(), time()的增强版以
取代perl内置的相应函数。
其中sleep()和alarm()的参数可以是小数。比如sleep(0.1)表示休眠0.1秒,
time()可以返回浮点数。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156237-1-1.html 上篇帖子: perl正则表达式 下篇帖子: Perl引用reference(\运算符)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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