perl:Config ::简单配置文件部分阅读

时间:2015-07-24 08:42:27

标签: perl

我正在尝试使用Config::Simple

执行配置文件

配置文件,即new1.conf

[Section1]
param1=value1
param2=value2

[Section2]
param1=value1
param2=value2

[Section3]
param1=value1
param2=value2

这是我的代码

use Config::Simple;

$cfg = new Config::Simple(syntax => 'ini');

#
# Get Section Names
# 
$cfg = Config::Simple->import_from('new.conf', \%Config)  or die Config::Simple->error();
my @arr = ( keys %Config );
@arr1 = grep s/\..*//, @arr;
my %uniq;
@uniq{@arr} = ();
@sections = keys %uniq;

foreach my $secname (sort @sections) 
{
    print "section : $secname\n";
    foreach (sort keys %Config) 
    {

        print "$_ : $Config{$_}\n";

    }
}

为此我得到像这样的输出

section : Section1
Section1.param1 : value1
Section1.param2 : value2
Section2.param1 : value1
Section2.param2 : value2
Section3.param1 : value1
Section3.param2 : value2
section : Section2
Section1.param1 : value1
Section1.param2 : value2
Section2.param1 : value1
Section2.param2 : value2
Section3.param1 : value1
Section3.param2 : value2
section : Section3
Section1.param1 : value1
Section1.param2 : value2
Section2.param1 : value1
Section2.param2 : value2
Section3.param1 : value1
Section3.param2 : value2

我试图将章节名称与相应章节中的参数进行比较。

因为我正在尝试编写此代码

foreach my $secname (sort @sections) 
{
    print "section : $secname\n";
    foreach (sort keys %Config) 
    {
        $var = grep { !/\b[A-Za-z0-9.].*[.]/ } @arr;
        if($secname == $var)
        {
            print "$secname\n";
        }
        else
        {
            print "false\n";
        }

        #compare 'secname' vs 'dialer onboard'.xxx
        #print "$_ : $Config{$_}\n";

    }
}

这对我不起作用。我只是坚持到这里。为此我得到像这样的输出

section : Section1
false
false
false
false
false
false
section : Section2
false
false
false
false
false
false
section : Section3
false
false
false
false
false
false

我无法比较和显示部分名称以及相应的参数和值。

最后我想要输出如下。

section : Section1
Section1.param1 : value1
Section1.param2 : value2
section : Section2
Section2.param1 : value1
Section2.param2 : value2
section : Section3
Section3.param1 : value1
Section3.param2 : value2

或至少我想显示参数。 我认为我在比较params和section时做错了什么。我无法确定这一点。

请有人建议我哪里出错了 提前谢谢。

1 个答案:

答案 0 :(得分:2)

你似乎把它变得比它需要的复杂得多。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Config::Simple;

my $cfg = Config::Simple->import_from('new1.conf', \my %config)
  or die Config::Simple->error();

my $curr_section = '';
foreach (sort keys %config) {
  my ($section, $param) = split /\./;

  if ($section ne $curr_section) {
    say "section : $section";
    $curr_section = $section;
  }
  say "$_: $config{$_}";
}

如果您使用更适合处理INI文件的模块,那就更容易了。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Config::INI::Reader;

my $cfg = Config::INI::Reader->read_file('new1.conf');

foreach my $section (sort keys %$cfg) {
  say "section: $section";
  foreach my $param (sort keys %{$cfg->{$section}}) {
    say "$section.$param : $cfg->{$section}{$param}"
  }
}

哦,你的问题是由于使用了错误的比较运算符引起的。您想要$secname eq $var,而不是$secname == $var。在您的代码中添加use strictuse warnings 始终是一个好主意。

相关问题