some perl module introduction

Spreadsheet::ParseExcel
perl解析Excel文件的例子。

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::FmtUnicode; #gb support

my $oExcel = new Spreadsheet::ParseExcel;

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

print "AUTHOR:", $oBook->{Author} , "/n"
if defined $oBook->{Author};

for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++)
{
$oWkS = $oBook->{Worksheet}[$iSheet];
print "--------- SHEET:", $oWkS->{Name}, "/n";
for(my $iR = $oWkS->{MinRow} ;
     defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ;
     $iR++)
{
  for(my $iC = $oWkS->{MinCol} ;
      defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ;
      $iC++)
  {
   $oWkC = $oWkS->{Cells}[$iR][$iC];
   print "( $iR , $iC ) =>", $oWkC->Value, "/n" if($oWkC);
  }
}

 


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

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

use Array::Compare;

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

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

foreach(@result)
{
   print $_ + 1, "th line:/n";
   print "> $a1[$_]> $a2[$_]";
   print "-----/n";
}
exit 0; 


Algorithm::Diff, diff()

用于文件比较。
实现类似unix command diff的功能。
#!/usr/bin/perl

use Algorithm::Diff qw(diff);

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

my ($file1, $file2) = @ARGV;
-T $file1 or die("$file1: binary/n");
-T $file2 or die("$file2: binary/n");

@f1 = `cat $file1 `;
@f2 = `cat $file2 `;

$diffs = diff(@f1, @f2);

foreach $chunk (@$diffs)
{
   foreach $line (@$chunk)
   {
      my ($sign, $lineno, $text) = @$line;
       printf "$sign%d %s", $lineno+1, $text;
   }

   print "--------/n";


HTML::Parser

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

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

详细的方法介绍,请见`perldoc HTML::Parser`
#!/usr/bin/perl

use LWP::Simple;
use HTML::Parser;

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

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

$parser->parse($content);
exit 0;

sub start
{
   my ($tag, $attr, $dtext, $origtext) = @_;   
   if($tag =~ /^img$/)
   {   
      if (defined $attr->{'src'} )
      {
         print "$attr->{'src'}/n";   
      }
   }


Data 查找替换

         use Data::SearchReplace ('sr');
         sr({ SEARCH => 'searching', REPLACE => 'replacing'}, /$complex_var);
                                                                                                
         # or OO
                                                                                                
         use Data::SearchReplace;
         $sr = Data::SearchReplace->new({ SEARCH => 'search for this',
                                          REPLACE => 'replace with this' });
                                                                                                
         $sr->sr(/$complex_var);
         $sr->sr(/$new_complex_var);
                                                                                                
         # if you want more control over your search/replace pattern you
         #  can pass an entire regex instead complete with attributes
                                                                                                
         sr({ REGEX => 's/nice/great/gi' }, /$complex_var);
                                                                                                
         # you can even use a subroutine if you'd like
         #  the input variable is the value and the return sets the new
         #  value.
                                                                                                
         sr({ CODE => sub { uc($_[0]) } }, /$complex_var);

        use Data::SearchReplace qw(sr);
        sr({SEARCH => 'find', REPLACE => 'replace'}, /@data);
        sr({REGEX  => 's/find/replace/g'}, /%data);
        sr({CODE   => sub {uc($_[0])} }, /@data);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值