in reply to CPAN, why hast thou forsaken me?

What sort of data structure do you want to store the config file in? This (untested) code builds a hash of arrays. The keys to the hash are the section names and the values are an array of the values within that section.

my %config; my $section; open(CONF, 'config.ini') || die "Can't open conf.ini: $!\n"; while (<CONF>) { next unless /\S/; chomp; if (/\[(.+)\])/) { $section = $1; } else { push @{$config{$section}}, $_; } }

Update: Bug corrected. Thanks to jeroenes for pointing it out.

As for fundflow's comment. He has a good point. My solution would probably to read the first line separately and if it wasn't a 'section' line to die with an error.

--
<http://www.dave.org.uk>

"Perl makes the fun jobs fun
and the boring jobs bearable" - me

Replies are listed 'Best First'.
Re: Re: CPAN, why hast thou forsaken me?
by myocom (Deacon) on Dec 21, 2000 at 03:52 UTC

    And, indeed, this is more or less what I ended up using (mine populates a hash of hashes). I haven't discounted tye's idea of "fixing" Config::Ini, though...

    For the record:

    my $inifile='config.ini' my %words; my $section; open (INI,$inifile) || die "Couldn't open '$inifile': $!\n"; my $section='DEFAULT'; # Somewhere to store the garbage while (<INI>) { next unless /\S/; next if /^[;'#]/; # Ignore comments chomp; if (/\[(.+)\]/) { $section = $1; } else { $words{$section}{$_}=1; } } close(INI);
Re: Re: CPAN, why hast thou forsaken me?
by fundflow (Chaplain) on Dec 20, 2000 at 22:38 UTC
    I'm not really familiar with INI files, but it might make sense to initialize $section to something in case the first group is not declared. Something like
    my $section = "MAIN";
Re: Re: CPAN, why hast thou forsaken me?
by jeroenes (Priest) on Dec 20, 2000 at 22:41 UTC
    Davorg, you may have forgotten a $. With the $:
    } else { push @{$config{$section}}, $_; }
    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)