http://www.perlmonks.org?node_id=779430

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

I'm trying to read some third party INI files using Config::IniFiles. I'm using the latest Config::IniFiles v2.52 with perl v5.8.8. I've never used Config::IniFiles before.

Given a simple example third party INI file, fred.ini, as follows:

[Trace] TRACE_MODULE=mod1 TRACE_MODULE=mod2
running this program:
use strict; use warnings; use Data::Dumper; use Config::IniFiles; my $cfg = Config::IniFiles->new( -file => 'fred.ini' ) or die "error: IniFiles->new: @Config::IniFiles::errors"; my @values = $cfg->val( 'Trace', 'TRACE_MODULE' ); print Dumper( \@values ); $cfg->WriteConfig( 'fred2.ini' ) or die "error: WriteConfig: $!\n";
emits:
$VAR1 = [ 'mod1', 'mod2' ];
and produces a new ini file, fred2.ini, as follows:
[Trace] TRACE_MODULE= <<EOT mod1 mod2 EOT
That is, Config::IniFiles seems to read the INI file ok, but writes it out in a different "<<EOT here document" format.

I need the new INI file to be written in its original format. Otherwise, the third party application is unable to read the updated INI files that I produce.

From reading the Config::IniFiles docs, it's unclear to me whether the third party multi-valued parameter INI file format, namely:

TRACE_MODULE=mod1 TRACE_MODULE=mod2
is officially supported. Though Config::IniFiles seems to read this format happily enough, it's not mentioned in its docs and I can see no way to make WriteConfig() emit a multi-valued parameter in this format.

There are many ways I could solve this problem. I could raise a Config::IniFiles ticket/patch enhancing this module to handle my third party multi-value parameter format. Or look for a different INI file module, write my own, ... Suggestions on how to solve my problem are welcome.

Replies are listed 'Best First'.
Re: Problem with Config::IniFiles multi-valued parameters and WriteConfig
by pKai (Priest) on Jul 13, 2009 at 09:26 UTC

    I have used Config:IniFiles in the past and always liked the TIEHASH interface.

    The mismatch in reading multi entries vs. writing that values struck me as odd, too. Though I never had the necessity to keep the syntax, as you have.

    Patching: On the plus side it seems it would be enough to enhance sub OutputConfig (maybe also enhancing the constructor's config to make the behaviour adjustable).
    Would have been nice if the part where the ARRAY is written, were a sub, which would offer the possibility to inhert from Config::IniFiles and override that behaviour without patching.

Re: Problem with Config::IniFiles multi-valued parameters and WriteConfig
by Anonymous Monk on Jul 13, 2009 at 09:48 UTC
    I don't think it is supported. I would submit a patch for Config::IniFiles, and subclass Config::IniFiles for personal use.