perl 一些有用的 util

比如 日志,和shell运行
# !/bin/perl -w
use   Time :: Local ;

my   $log_input_file = " ~/log " ;
sub  set_log_input_file{
  
$log_input_file = $_ [ 0 ];
}


# &perl_log("tt","/home/xj_liukaiyi/log");
sub  perl_log{
  
my   $log_msg = shift   @_ ;
  
print   $log_msg , " \n " ;
  
system   ' ttmp1=$(date +%Y-%k-%d);ttmp2=$(date +%M:%S);echo "$ttmp1 $ttmp2: ' . " $log_msg " . ' " >> ' . $log_input_file ;
}

# &system_util("xls -l","~/log");
sub  system_util{
  
my   $comm = shift   @_ ;
  
my   @return = system   $comm ;
  
if  (  $return [( @return - 1 )]  ==   0  ) { 
     
& perl_log( " [success] " . $comm , $log_input_file );
  }
else {
     
& perl_log( " [error] " . $comm , $log_input_file );
  }
  
return   $return [( @return - 1 )];
}

1 ;
excel 读取util 
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl"); &parse_excel("2w.xls","歌曲列表","&read_wlh);
# !/bin/perl -w
use  Spreadsheet :: ParseExcel;
use  Spreadsheet :: ParseExcel :: FmtUnicode; 
use  Encode;
use  Unicode :: Map ();
my   $Map   =  new Unicode :: Map ( " GB2312 " );


my   $excelCODE = ' gb2312 ' ;
my   $coslCODE = ' utf8 ' ;

my   %parseCache  ;
my   %writeCache  ;

my   $oFmtJ   =  Spreadsheet :: ParseExcel :: FmtUnicode -> new(Unicode_Map  => $excelCODE ); 
my   $oExcel   =  new Spreadsheet :: ParseExcel;



#
#&parse_excel("all4.xls","乐扑","&read_lp);  
# sub read_lp{  $_[1 .. 2.3  ] } 
sub  parse_excel{
 
my   $sExcelName = shift ;
 
my   $sSheetName = shift ;
 
my   $action_fun = shift ;
 
 
my ( $iR ,   $iC ,   $oWkS , $oWkC );
 
# 开启 excel
  my   $oBook   =   $oExcel -> Parse( $sExcelName , $oFmtJ );
 
# 根据 sheet 名称匹配 
  for ( my   $iSheet = 0 $iSheet   <   $oBook -> {SheetCount} ;  $iSheet ++ ){
    
my   $t_oWkS   =   $oBook -> {Worksheet}[ $iSheet ];
    
my   $sWkSName = encode( $coslCODE , decode( $excelCODE , $t_oWkS -> {Name} ));
    
if ( $sWkSName =~/ $sSheetName / ){
      
$oWkS = $t_oWkS ;
      
last ;
   }
 }


 
for ( my   $iR   =   $oWkS -> {MinRow} ;
     
defined   $oWkS -> {MaxRow}  &&   $iR   <=   $oWkS -> {MaxRow} ;
     
$iR ++ ){
     
my   @arr_row ;
     
for ( my   $iC   =   $oWkS -> {MinCol} ;
         
defined   $oWkS -> {MaxCol}  &&   $iC   <=   $oWkS -> {MaxCol} ;
         
$iC ++ ){
       
$oWkC = $oWkS -> {Cells}[ $iR ][ $iC ];
       
my   $col_value = "" ;
           
$col_value = encode( $coslCODE , decode( $excelCODE , $oWkC -> Value) )  if  ( $oWkC ) ;
           
push ( @arr_row , $col_value );
         }
      
push ( @arr_row , $iR );
     
$action_fun -> ( @arr_row );
    }
}

# 使用 demo
find  .   - maxdepth  1   - name  " *xls "   - exec  perl  - '
require("/home/xj_liukaiyi/src/perl/excel/excelUtil.pl");
my $file=$ARGV[0];
sub myRead{
  my $str="";
  foreach$tmp(@_){
    $str+"$tmp\t";
  }
  system "echo $str >> $file.data";
}
&parse_excel("$file",0,\&myRead);
print "$file\n";
'  {}  \ ;
本文转自博客园刘凯毅的博客,原文链接:perl 一些有用的 util,如需转载请自行联系原博主。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值