免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: deathcult
打印 上一主题 下一主题

Perl模块使用 => 简短例子代码集合!  关闭 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2003-08-01 10:44 |只看该作者

Perl模块使用 => 简短例子代码集合!

提供者:flora

(29) Bio::DB::GenBank, Bio::SeqIO

bioperl(http://bioperl.org/)模块使用--生物信息学中用的模块
功能:根据核酸的gi号自动从GenBank中提取FASTA格式的序列,可以多序列提取。
代码如下:


  1. #!/usr/bin/perl -w

  2. use Bio::DB::GenBank;
  3. use Bio::SeqIO;
  4. my $gb = new Bio::DB::GenBank;

  5. my $seqout = new Bio::SeqIO(-fh =>; \*STDOUT, -format =>; 'fasta');


  6. # if you want to get a bunch of sequences use the batch method
  7. my $seqio = $gb->;get_Stream_by_id([ qw(27501445 2981014)]);

  8. while( defined ($seq = $seqio->;next_seq )) {
  9.         $seqout->;write_seq($seq);
  10. }
复制代码

论坛徽章:
0
32 [报告]
发表于 2003-08-01 12:25 |只看该作者

Perl模块使用 => 简短例子代码集合!

提供者:flora

(30) Spreadsheet:arseExcel
perl解析Excel文件的例子。


  1. #!/usr/bin/perl -w

  2. use strict;
  3. use Spreadsheet::ParseExcel;
  4. use Spreadsheet::ParseExcel::FmtUnicode; #gb support

  5. my $oExcel = new Spreadsheet::ParseExcel;

  6. die "You must provide a filename to $0 to be parsed as an Excel file" unless @ARGV;
  7. my $code = $ARGV[1] || "CP936"; #gb support
  8. my $oFmtJ = Spreadsheet::ParseExcel::FmtUnicode->;new(Unicode_Map =>; $code); #gb support
  9. my $oBook = $oExcel->;Parse($ARGV[0], $oFmtJ);
  10. my($iR, $iC, $oWkS, $oWkC);
  11. print "FILE  :", $oBook->;{File} , "\n";
  12. print "COUNT :", $oBook->;{SheetCount} , "\n";

  13. print "AUTHOR:", $oBook->;{Author} , "\n"
  14. if defined $oBook->;{Author};

  15. for(my $iSheet=0; $iSheet < $oBook->;{SheetCount} ; $iSheet++)
  16. {
  17. $oWkS = $oBook->;{Worksheet}[$iSheet];
  18. print "--------- SHEET:", $oWkS->;{Name}, "\n";
  19. for(my $iR = $oWkS->;{MinRow} ;
  20.      defined $oWkS->;{MaxRow} && $iR <= $oWkS->;{MaxRow} ;
  21.      $iR++)
  22. {
  23.   for(my $iC = $oWkS->;{MinCol} ;
  24.       defined $oWkS->;{MaxCol} && $iC <= $oWkS->;{MaxCol} ;
  25.       $iC++)
  26.   {
  27.    $oWkC = $oWkS->;{Cells}[$iR][$iC];
  28.    print "( $iR , $iC ) =>;", $oWkC->;Value, "\n" if($oWkC);
  29.   }
  30. }
  31. }
复制代码

论坛徽章:
0
33 [报告]
发表于 2003-08-08 15:31 |只看该作者

Perl模块使用 => 简短例子代码集合!

(31) Text::CSV_XS, parse(), fields(), error_input()

如果field里面也包含分隔符(比如"tom,jack,jeff","rose mike",O'neil,"kurt,korn",那么我们解析起来确实有点麻烦,
Text::CSV_XS挺方便。


  1. #!/usr/bin/perl

  2. use strict;
  3. use Text::CSV_XS;

  4. my @columns;
  5. my $csv = Text::CSV_XS->;new({
  6.                                                         'binary' =>; 1,
  7.                                                         'quote_char'  =>; '"',
  8.                                         'sep_char'    =>; ','               
  9.                                                         });  

  10. foreach my $line(<DATA>;)
  11. {
  12.         chomp $line;
  13.         if($csv->;parse($line))
  14.         {
  15.                 @columns = $csv->;fields();
  16.         }
  17.         else
  18.         {
  19.                 print "[error line : ", $csv->;error_input, "]\n";
  20.         }

  21.         map {printf("%-14s\t", $_)} @columns;
  22.         print "\n";
  23. }
  24. exit 0;

  25. __DATA__
  26. id,compact_sn,name,type,count,price
  27. 37,"ITO-2003-011","台式机,compaq","128M","290","1,2900"
  28. 35,I-BJ-2003-010,"显示器,硬盘,内存",'三星',480,"1,4800"
  29. 55,"C2003-104",笔记本,"Dell,Latitude,X200",13900,"1,13900"
复制代码

论坛徽章:
0
34 [报告]
发表于 2003-08-15 18:34 |只看该作者

Perl模块使用 => 简短例子代码集合!

提供者:Apile

(32) Benchmark


  1. #!/usr/bin/perl

  2. use Benchmark;

  3. timethese(100,
  4.         {
  5.                 'local'=>;q
  6.                                 {
  7.                                         for(1..10000)
  8.                                         {
  9.                                                 local $a=$_;
  10.                                                 $a *= 2;
  11.                                         }
  12.                                 },

  13.                 'my'=>;q
  14.                         {
  15.                                 for(1..10000)
  16.                                 {
  17.                                         my $a=$_;
  18.                                         $a *= 2;
  19.                                 }
  20.                         }
  21.         });
复制代码



可以拿來算某個algorithm耗費多少時間..
timethese(做幾次iteration,{
'Algorithm名稱'=>;q{ 要計算時間的algorithm },
'Algorithm名稱'=>;q{ 要計算時間的algorithm }
});

论坛徽章:
0
35 [报告]
发表于 2003-08-15 19:42 |只看该作者

Perl模块使用 => 简短例子代码集合!

(33) HTTP:aemon, accept(), get_request(), send_file_response()

一个简单的,只能处理单一请求的Web服务器模型。
send_file_response()方法能把Client请求的文件传送过去。


  1. #!/usr/bin/perl

  2. use HTTP::Daemon;

  3. $| = 1;
  4. my $wwwroot = "/home/doc/";
  5. my $d = HTTP::Daemon->;new || die;
  6. print "Perl Web-Server is running at: ", $d->;url, " ...\n";

  7. while (my $c = $d->;accept)
  8. {       
  9.         print $c "Welcome to Perl Web-Server<br>;";

  10.     if(my $r = $c->;get_request)
  11.         {               
  12.                 print "Received : ", $r->;url->;path, "\n";
  13.                 $c->;send_file_response($wwwroot.$r->;url->;path);
  14.     }

  15.     $c->;close;
  16. }
复制代码

论坛徽章:
0
36 [报告]
发表于 2003-08-21 15:45 |只看该作者

Perl模块使用 => 简短例子代码集合!

(34) Array::Compare, compare(), full_compare()

用于数组比较。
本例实现类似shell command - diff的功能。
如果我们要比较的不是文件,而是比如系统信息,远程文件列表,数据库内容变化等,这个模块会给我们提供方便灵活的操作。



  1. #!/usr/bin/perl

  2. use Array::Compare;

  3. $comp = Array::Compare->;new(WhiteSpace =>; 1);
  4. $cmd = "top -n1 | head -4";
  5. @a1 = `$cmd`;
  6. @a2 = `$cmd`;

  7. @result = $comp->;full_compare(\@a1, \@a2);

  8. foreach(@result)
  9. {
  10.         print $_ + 1, "th line:\n";
  11.         print ">; $a1[$_]>; $a2[$_]";
  12.         print "-----\n";
  13. }
  14. exit 0;
复制代码

论坛徽章:
0
37 [报告]
发表于 2003-08-25 17:21 |只看该作者

Perl模块使用 => 简短例子代码集合!

(35) Algorithm:iff, diff()

用于文件比较。
实现类似unix command diff的功能。


  1. #!/usr/bin/perl

  2. use Algorithm::Diff qw(diff);

  3. die("Usage: $0 file1 file2\n") if @ARGV != 2;

  4. my ($file1, $file2) = @ARGV;
  5. -T $file1 or die("$file1: binary\n");
  6. -T $file2 or die("$file2: binary\n");

  7. @f1 = `cat $file1 `;
  8. @f2 = `cat $file2 `;

  9. $diffs = diff(\@f1, \@f2);

  10. foreach $chunk (@$diffs)
  11. {
  12.         foreach $line (@$chunk)
  13.         {
  14.                 my ($sign, $lineno, $text) = @$line;
  15.             printf "$sign%d %s", $lineno+1, $text;
  16.         }

  17.         print "--------\n";
  18. }

复制代码

论坛徽章:
0
38 [报告]
发表于 2003-09-01 14:35 |只看该作者

Perl模块使用 => 简短例子代码集合!

(36) List::Util, max(), min(), sum(), maxstr(), minstr()...

列表实用工具集。


  1. #!/usr/bin/perl

  2. use List::Util qw/max min sum maxstr minstr shuffle/;

  3. @s = ('hello', 'ok', 'china', 'unix');

  4. print max 1..10;                #10
  5. print min 1..10;                #1
  6. print sum 1..10;                #55
  7. print maxstr @s;                #unix
  8. print minstr @s;                #china
  9. print shuffle 1..10;        #radom order


复制代码

论坛徽章:
0
39 [报告]
发表于 2003-09-02 16:46 |只看该作者

Perl模块使用 => 简短例子代码集合!

(37) HTML:arser

解析HTML。本例为找出一个html文本中的所有图片的地址。(即IMG标签中的src)

子程序start中的“$tag =~ /^img$/”为过滤出img标签。
如果换为“$tag =~ /^a$/”,即是找出所有的链接地址。

详细的方法介绍,请见`perldoc HTML:arser`



  1. #!/usr/bin/perl

  2. use LWP::Simple;
  3. use HTML::Parser;

  4. my $url = shift || "http://www.chinaunix.net";
  5. my $content = LWP::Simple::get($url) or die("unknown url\n");

  6. my $parser = HTML::Parser->;new(
  7.                         start_h =>; [\&start, "tagname, attr"],
  8.                         );

  9. $parser->;parse($content);
  10. exit 0;

  11. sub start
  12. {
  13.         my ($tag, $attr, $dtext, $origtext) = @_;       
  14.         if($tag =~ /^img$/)
  15.         {       
  16.                 if (defined $attr->;{'src'} )
  17.                 {
  18.                         print "$attr->;{'src'}\n";       
  19.                 }
  20.         }
  21. }

复制代码

论坛徽章:
0
40 [报告]
发表于 2003-09-04 14:46 |只看该作者

Perl模块使用 => 简短例子代码集合!

(38) Mail::Sender

(1)发送附件



  1. #!/usr/bin/perl

  2. use Mail::Sender;

  3. $sender = new Mail::Sender{
  4.                                                         smtp =>; 'localhost',
  5.                                                         from =>; 'xxx@localhost'
  6.                                                         };
  7. $sender->;MailFile({
  8.                                         to =>; 'xxx@xxx.com',
  9.                                         subject =>; 'hello',
  10.                                         file =>; 'Attach.txt'
  11.                                         });
  12. $sender->;Close();

  13. print $Mail::Sender::Error eq "" ? "send ok!\n" : $Mail::Sender::Error;


复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP