Perl腳本學習經驗(二)------常用命令舉例


一.chomp
   作      用:去除讀入字符串的換行符,去除\n或者\r\n:
   簡單舉例:
    while(<FDATA>)
    {
        my $tmp_record = $_;
        chomp ($tmp_record);
    }
   
    簡單說明:此時$tmp_record變量的結束處已經不包含換行符;
   
 二.split
   作      用:根據某個模式分割字符串;
   簡單舉例:
   my @record= split(/\|/,$tmp_record);
   
   簡單說明:定義一個record數組,根據“|”分割變量$tmp_record內容;  
   
 三.while
   作      用:循環遍歷
   簡單舉例:
    open(FDATA, "<test.txt")||die "Failed to open test.txt: $!";
    while(<FDATA>)
    {
        my $tmp_record = $_;
        chomp ($tmp_record);
        my @record= split(/\|/,$tmp_record);
    }
    close(FDATA);
   
   簡單說明:讀取test.txt文件,循環遍歷文件中的內容;
  
 四.foreach
   作      用:循環遍歷
   簡單舉例:
    foreach $ckey(sort keys %g_bll)
    {
        my $tmp_record = $g_bll{$ckey};
        chomp ($tmp_record);
        my @record= split(/\|/,$tmp_record);
    }
   
   簡單說明:循環遍歷%g_bll_charge_map容器中的內容;
  
 五.substr
   作      用:截取字符串
   簡單舉例:
   例一:my $FirstFlag = substr(“abcdefg”,0,1);
   例二:my $FirstFlag = substr(“abcdefg”,-1,1);
   
   簡單說明:有三個參數,分別是截取的父串,從0開始的初始位置,字串的長度;
                 例一中截取abcdefg字符串中從0開始長度為1的字符串,結果返回a;
               例二中截取abcdefg字符串中最后一個字符串,結果返回g;
            
 六.sort
   作      用:簡單排序
   簡單舉例:
    foreach $ckey(sort keys %g_bll)
    {
        my $tmp_record = $g_bll{$ckey};
        chomp ($tmp_record);
        my @record= split(/\|/,$tmp_record);
    }
   
   簡單說明:循環遍歷容器中的內容,按照key值排序;exists
  
 七.exists
   作      用:查看hash中是否存在某個值
   簡單舉例:
   if(exists $g_bll{date})
   {
   }
   
   簡單說明:查看容器$g_bll中是否存在鍵值為date的數據,存在返回true,不存在返回false;
  
 八.讀取文件
   作      用:打開文件句柄,讀取文件內容
   簡單舉例:
   open(FDATA, "<<test.txt")||die "Failed to open test.txt: $!";
   foreach $temp_acctsub(sort keys %g_bll_map)
   {
       my $result = $g_bll_map{$temp_acctsub};
       print FDATA $result ;
   }
   close(FDATA);
   
   簡單說明:讀取文件test.txt中的內容,如果不存在該文件,用die命令輸出錯誤信息;
  
 九.讀取文件夾
   作      用:讀取文件夾下的內容
   簡單舉例:
   opendir DH,$g_work_path or die "Cannot open the DIR!";
   foreach $dirname(readdir DH)
   {
       if($dirname ne "." and $dirname ne "..")
       {
           my $filename = $g_work_path .$dirname;
           $file_name_map{$filename} = $dirname;
       }
   }
   closedir(DH);
   
   簡單說明:和讀取文件不同的是使用opendir和closedir;
  
 十.向文件中寫內容
   作      用:讀取文件夾下的內容
   簡單舉例:
   open(FDATA, ">>test.txt")||die "Failed to open test.txt: $!";
   foreach $key(sort keys %g_fee_info_map)
   {
     ........    
   }
   print FDATA $temp_info . "\r\n";
   close(FDATA);
   
   簡單說明:打開文件句柄,>>向文件中追加;
  
 十一.map容器的使用
   作      用:key值匹配value值
   簡單舉例:
   $g_bill_map{$id} = $info;
   
   簡單說明:以id為鍵值,存儲不同用戶的信息;如果key值相同,value值會被覆蓋;
  
 十二.map容器的嵌套使用
   作      用:value值仍舊是一個map,雙層map的使用
   簡單舉例:
   $g_info_map{$filekey}{$acctsub} =  $temp_subinfo_fee;     -----存儲用戶信息
  
    my %g_subinfo_map = %{$g_info_map{$filekey}};            -----提取用戶信息
   
   簡單說明:存儲用戶信息----使用$filekey和$acctsub兩個鍵值存儲$temp_subinfo_fee,實際上這邊有兩個map:
                             首先通過$filekey可以查處一個用用戶信息的map,用戶信息的map是以$acctsub為鍵值,$temp_subinfo_fee為value;
             提取用戶信息----定義了一個%g_subinfo_map的map來存儲通過$filekey查詢出的用戶信息; 
            
 十三.perl腳本中調用lunix命令
   作      用:
   簡單舉例:
   `mkdir -p $g_out_put_path`;
   
   簡單說明:創建文件夾,用``;
  
 十四.perl腳本中和數據庫交互
   作      用:通過腳本訪問數據庫
   簡單舉例:
   sub get_bll_charge_from_database()
   {
       my $g_sql =  "select b.acct_id || '|' || b.sub_id || '|' || b.item_id || '|' || b.cos || '|' || b.charge || '|'  || b.bill_id || '|' || b.number  \n"
                 . "  from bll_info; \n";
     
       &create_unload_sql_file($g_bll_sqlfile,$g_bll_file,$g_sql);
  
       `sqlplus $dbstr \@$g_bll_sqlfile`;
  
       my(@szContent) = `cat $g_bll_file|grep ORA-`;
  
       foreach (@szContent){
           my $tmpContent = $_;
           if($tmpContent =~ /ORA-(\d)+/)   #這里出錯了
           {
               print $tmpContent;
               return;
           }
       }
   }
  
   sub create_unload_sql_file()
   {
       #$_[0] -> sqlfile;$_[1] -> datafile;$_[2] -> sql;
       open(FSQLFILE,">$_[0]") || die "Failed to open $_[0]:$!";
  
       printf FSQLFILE "set serveroutput on \n";
       printf FSQLFILE "set time on \n";
       printf FSQLFILE "set timing on \n";
       printf FSQLFILE "set linesize 1000  \n";
       printf FSQLFILE "set echo off \n";
       printf FSQLFILE "set term off \n";
       printf FSQLFILE "set heading off \n";
       printf FSQLFILE "set pagesize 0 \n";
       printf FSQLFILE "set trimspool on \n";
       printf FSQLFILE "set feedback off \n";
       printf FSQLFILE "set time off \n";
       printf FSQLFILE "set timing off \n";
       printf FSQLFILE "spool $_[1] \n";
       printf FSQLFILE "$_[2] \n";
       printf FSQLFILE "spool off \n";
       printf FSQLFILE "exit; \n";
       close(FSQLFILE);
   }
   
   簡單說明:1. $dbstr數據庫連接串;
                 2. `sqlplus $dbstr \@$g_bll_sqlfile`; 不能直接跟SQL,將SQL寫到一個文件中,執行文件;
                 3.create_unload_sql_file方法對數據庫進行設置,並訪問數據庫執行SQL;
                 4.將結果寫入到$g_bll_file文件中;

 

以上是我在自己寫perl腳本中常用的一些命令,希望可以給新手一點幫助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM