首页 > 解决方案 > Perl Config::Simple:在文件中添加额外的控制字符 (\\n)

问题描述

我有一个程序必须从 perl 脚本读取和写入配置(属性)文件。我正在使用 Config::Simple 进行读写操作。

我的问题是,我可以在属性文件中读写。但是,当我尝试更新属性值中的一个配置值时,它会添加控制字符 (\n) 并且还想删除由“cfg-write()”方法创建的默认标头。我试过搜索论坛并提到 API,而不是信息。有人可以发光吗?

当我尝试调用日期/时间函数时,它不会添加任何额外的字符。但是,当我尝试执行 svn 命令时,它会添加额外的字符。我已经尝试在控制台上使用打印命令,它可以完美地工作而无需控制字符

my $buildDate = strftime('%d/%m/%y %H:%M:%S',localtime);
my $rev = `svn info --show-item revision`;
my $cfg = Config::Simple->import_from('temp.properties', \my %Config);
$cfg->param("builddate", $buildDate);
$cfg->param("revision", $rev);
$cfg->write() or die $cfg->error();

属性文件:

; Config::Simple 4.58
; Thu Apr 25 10:59:10 2019

version=Build Automation
revision=14794\\n
builddate=25/04/19 10:59:10

预期结果:1) 需要在修订配置参数中删除控制字符 (\n) 2) cfg-write 或 cfg-autosave() 将默认标头写入文件,我想删除/忽略

实际结果:1)添加了额外的控制字符(\n) 2)将默认标题添加到属性文件中

标签: perlconfiguration

解决方案


感谢@choroba。优秀的投入

我尝试使用Config::Properties::Commons

 my $cpc = Config::Properties::Commons->new();
     $cpc->load('temp.properties');
     # Access
     $cpc->set_property( builddate => 'Updated' );
     $cpc->save('temp.properties');

Code for **Config::Simple**

    my $rev = `svn info --show-item revision /home/sudhir/Work/Workspaces/Eclipse/trunk`;

        chomp($ver);
        my $cfg = Config::Simple->import_from('easycare.properties', \my %Config) or 
     die print "ERROR: Not able to open easycare.properties file";


    $cfg->param("builddate", $buildDate);
    $cfg->param("revision", $rev);
    $cfg->param("version", $ver);
    $cfg->write() or die $cfg->error();

推荐阅读