【问题标题】:Perl Config::Simple: Adding Extra control characters (\\n) in the filePerl Config::Simple:在文件中添加额外的控制字符 (\\n)
【发布时间】:2019-04-25 06:56:27
【问题描述】:

我有一个程序必须从 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) 属性文件中添加默认标头

【问题讨论】:

  • chomp $rev 删除换行符。
  • 为什么需要去掉header?
  • 首先,我要感谢你的方法chomp。我想删除,因为我在 ant 中调用属性文件。这可能吗?
  • 那为什么不用Config::Properties呢?
  • 哈希字符应该被蚂蚁忽略。

标签: perl configuration


【解决方案1】:

感谢@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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2013-08-30
    • 1970-01-01
    相关资源
    最近更新 更多