abhi1390 has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl programming. And i am trying to do the configuration file using Config::Simple The configuration file i.e. new1.conf

[Section1] param1=value1 param2=value2 [Section2] param1=value1 param2=value2 [Section3] param1=value1 param2=value2

here is my code

use Config::Simple; $cfg = new Config::Simple(syntax => 'ini'); # # Get Section Names # $cfg = Config::Simple->import_from('new.conf', \%Config) or die Confi +g::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"; } }

For this i am getting the output like this

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

I am trying to compare the section names with paramaters in that respective section. for that i am trying to write this code

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"; } }

This is not working for me. I stuck up here only I cant be able to compare and display the section name with the respective params and values.

And finally i want output like this below.

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

or at least i want to display the params. I think i am doing something wrong in comparing params and section. I am not able to identify that. Please somebody suggest me where i am going wrong Thanks in advance.

Replies are listed 'Best First'.
Re: perl: Config::Simple config file reading
by poj (Abbot) on Jul 24, 2015 at 10:48 UTC
    Try
    #!perl; use strict; use Config::Simple; my %Config=(); Config::Simple->import_from('new.conf', \%Config) or die Config::Simple->error(); my %hash=(); for my $key (keys %Config){ my ($sect,$para) = split /\./,$key; $hash{$sect}{$para} = $Config{$key}; } for my $sect (sort keys %hash){ print "section : $sect\n"; for my $para (sort keys %{$hash{$sect}}){ print "$sect.$para : ".$hash{$sect}{$para}."\n"; } }
    poj
Re: perl: Config::Simple config file reading
by 1nickt (Canon) on Jul 24, 2015 at 23:23 UTC

    UPDATE: You also cross-posted this question on Stackoverflow. It's good form to mention that and provide a link.

    There are problems with your code. You should always use strict; at the top of all your code and let Perl find some of your errors for free.

    But besides that you are using a module that doesn't return the data in the way you want. You should use Config::Tiny which is only for ini-style files, and returns a reference to a hash structure as you want:

    #! perl -w use strict; use feature qw/ say /; use Config::Tiny; my $cfg = Config::Tiny->new; $cfg = Config::Tiny->read('new.conf') or die $!; foreach my $section (sort keys %{ $cfg }) { say "section: $section"; foreach my $param (sort keys %{ $cfg->{ $section } }) { say "$section.$param : $cfg->{ $section }->{ $param }"; } } __END__
    The way forward always starts with a minimal test.