perl 常用module的使用

來源:互聯網
上載者:User

一些常用模組的簡單描述 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.chinaunix.net";
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.chinaunix.net";
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()可以返回浮點數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.