perl 读写配置文件

 

1.配置文件格式形式如下
 
[NEWBIN]
path = /data1/vshare/newbin
uid = root
gid = root
read only = no 
[col]
path = /data1/col
uid = root
gid = root
read only = no
2.读配置函数
read_conf()
读出为整个hash
$hash{"NEWBIN"}{"path "}= “/data1/vshare/newbin”
 3.读大分类函数
read_cat($cat)
读出为
$hash{"NEWBIN"}{"path "}= “/data1/vshare/newbin”
4.读key函数
read_cat_key($cat,$key)
读出为值
5.写指定数据函数
write_cat_key($cat,$key,$value)
(1)读出整个配置,替换此value,整个重写回去

 

#! /usr/bin/perl
  
use RW_Conf;
 
 
$cup = new RW_Conf(); 
$cup->setfilename("my.conf");
 
 
# my %ret = $cup->read_conf();
# foreach my $cat(  keys %ret){   
#   foreach my $key(  keys %{$ret{$cat}}){   	my $value=$ret{$cat}{$key};
#  print "[".$cat."]".$key."=".$value,"\n";
# 	}    
# }  

#my $cat="lady_high";
#my %ret = $cup->read_cat($cat);
#  foreach my $key(  keys %ret ){   
#	  my $value=$ret{$key};
#   print "[".$cat."]".$key."=".$value,"\n";
#  }
  
#my $cat="lady_high";
#my $key="vcodec";
#my $value = $cup->read_cat_key($cat,$key);
#print "[".$cat."]".$key."=".$value,"\n";

my $cat="lady_high";
my $key="xxxxx";
$cup->write_cat_key($cat,$key,"yyyy");


my $cat="lady_high";
my $key="vfopts2";
$cup->write_cat_key($cat,$key,"yyyy");

 


 

package RW_Conf;
 
use strict;
 
sub new {
  my $this = {};  
  bless $this;   
  return $this; 
}

sub setfilename()
{
	 my $this = shift;
	 $this->{"filename"}=shift;
}

sub getfilename()
{
	 my $this = shift;
	return   $this->{"filename"};
}

#读配置函数
#read_conf 返回 %hash
 sub read_conf()
{
	 my $this = shift;
        my %ret=();
        open FH, $this->{"filename"} or die("can't not open ".$this->{"filename"}."\n"); 
        my $cat="";
		my $key="";
        while (my $myline = <FH>) {  
        chomp $myline ;
			next if $myline eq "";
		    if( $myline =~ /^\s*\[\s*([^=]+)\s*\]/ ) {
				$cat=$1;
				#print $cat,"\n";
			}
			elsif ($myline =~ /=/) 
			{ 
				 my ($key, $value) = $myline =~ /^([^=]+)=(.*)$/; $key =~ s/\s//g;
				 $ret{$cat}{$key}=$value;
				  #print "[".$cat."]|".$key."|".$value,"\n";
			}
                
        }
        close FH; 
        return %ret;
}
#读大分类函数
#read_cat($cat)
#读出为%hash

 sub read_cat()
{
	 my $this = shift; 
	 my $find_cat = shift;
	 
        my %ret=();
        open FH, $this->{"filename"} or die("can't not open ".$this->{"filename"}."\n"); 
        my $cat="";
		my $key="";
        while (my $myline = <FH>) {  
        chomp $myline ;
			
		    if( $myline =~ /^\s*\[\s*([^=]+)\s*\]/ ) {
				$cat=$1;
				#print $cat,"\n";
			}
			elsif ($myline =~ /=/) 
			{ 
				 my ($key, $value) = $myline =~ /^([^=]+)=(.*)$/; $key =~ s/\s//g;
				 $ret{$cat}{$key}=$value;
				  #print "[".$cat."]|".$key."|".$value,"\n";
			}
                
        }
        close FH; 
        return %{$ret{$find_cat}};
}
#读key函数
#read_cat_key($cat,$key)
 sub read_cat_key()
{
	 my $this = shift; 
	 my $find_cat = shift;
	 my $find_key = shift;
	 
        my %ret=();
        open FH, $this->{"filename"} or die("can't not open ".$this->{"filename"}."\n"); 
        my $cat="";
		my $key="";
        while (my $myline = <FH>) {  
        chomp $myline ;
			
		    if( $myline =~ /^\s*\[\s*([^=]+)\s*\]/ ) {
				$cat=$1;
				#print $cat,"\n";
			}
			elsif ($myline =~ /=/) 
			{ 
				 my ($key, $value) = $myline =~ /^([^=]+)=(.*)$/; $key =~ s/\s//g;
				 $ret{$cat}{$key}=$value;
				  #print "[".$cat."]|".$key."|".$value,"\n";
			}
                
        }
        close FH; 
        return  $ret{$find_cat}{$find_key};
}
#写指定数据函数
#write_cat_key($cat,$key)
#读出整个配置,替换此value,整个重写回去
 sub write_cat_key()
{
	 my $this = shift; 
	 my $find_cat = shift;
	 my $find_key = shift;
	 my $write_value=shift;
	 
        my %ret=();
        open FH, $this->{"filename"} or die("can't not open ".$this->{"filename"}."\n"); 
        my $cat="";
		my $key="";
        while (my $myline = <FH>) {  
        chomp $myline ;
			
		    if( $myline =~ /^\s*\[\s*([^=]+)\s*\]/ ) {
				$cat=$1;
				#print $cat,"\n";
			}
			elsif ($myline =~ /=/) 
			{ 
				 my ($key, $value) = $myline =~ /^([^=]+)=(.*)$/; $key =~ s/\s//g;
				 $ret{$cat}{$key}=$value;
				  #print "[".$cat."]|".$key."|".$value,"\n";
			}
                
        }
        close FH; 
        $ret{$find_cat}{$find_key}=$write_value;
		 
		#重写配置文件
		open DET,">", $this->{"filename"}  || die ("can't not open ".$this->{"filename"}."\n"); 
		 foreach my $cat(  keys %ret){   
			 print DET  "[".$cat."]" ,"\n";
 
		  foreach my $key(  keys %{$ret{$cat}}){  
			  my $value=$ret{$cat}{$key}; 
			   print DET  $key."=".$value,"\n";
			}    
		 }  
		 close DET; 


}
 
1;



 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值