perl读取配置文件

作者在 2017-02-21 13:05:45 发布以下内容

工作要求,要写一个读取配置文件的脚本,从来没接触过,研究了一天才整出来,特此做笔记

代码如下:

#!/usr/bin/perl -w
##===============================

##====     打开配置文件  ======
open(CONF,"<","./file.conf")||die"cannot open the file: $!\n";
my $comm="asdsdssdfefe201010dfs&payUse=2sdf7021sddfs0170218";
##my $comm=$_[0];
my $str ="<?xml version=\"1.0\" encoding=\"GBK\"?>\n";

while (<CONF>)
{
    if(index ($_, "[") > -1)
    {
	     my $position = tell(CONF);
       my $keyword_line = $_;
       my $line = <CONF>;
       ##取得一行,去掉结尾换行符
       $line =~ s/\x0D?\x0A?$//; 

       ##====     对比个性化字段读取相应配置  ======
       if (($comm =~ m/pageNo/) and  (index ($keyword_line, "pageNo") > -1))
       {
           $str =$str .$line;
           last;
       }
       elsif (($comm =~ m/tranFlowNo/) and  (index ($keyword_line, "tranFlowNo") > -1))
       {
           $str =$str .$line;
           last;
       }
       elsif (($comm =~ m/unionBankNo/) and  (index ($keyword_line, "unionBankNo") > -1))
       {
           $str =$str .$line;
           last;
       }
       elsif (($comm =~ m/payUse/) and  (index ($keyword_line, "payUse") > -1))
       {
           $str =$str .$line;
           last;
       }
       elsif (($comm =~ m/beginDate/) and  (index ($keyword_line, "beginDate") > -1))
       {
           $str =$str .$line;
           last;
       }
       elsif(index ($keyword_line, "others") > -1)
       {
           $str =$str .$line;
           last;
       }
       
       seek(CONF, $position, 0);	
    }
}
printf "$str";
##====     关闭配置文件  ======
close (CONF);
#return $str;
exit;
配置文件file.conf内容:

###########################################################
#####   脚本配置文件说明,方括号下直接填入报文格式 #####
###########################################################

[pageNo]
<ebank></cbtranFlowNo></remark></row></list></ebank>

[tranFlowNo]
<ebank>State><errorCode>000000</errorCode><errorMsg>sucess</errorMsg></ebank>

[unionBankNo]
<ebank><<currencyType>0</currencyType>>201701011123111</transferFlowNo></ebank>

[payUse]
<ebank><tranDate>20170101</tranDate>201701011123111</transferFlowNo></ebank>

[beginDate]
<ebank></ebank>

[others]
<ebank>accountType>0</accountType></ebank>

配置文件随便写了点内容,仅做测试使用。

perl | 阅读 2862 次
文章评论,共0条
游客请输入验证码
文章归档
最新评论