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

1) Does Config::IniFiles provide some means of adding a new:
Parameter=Value

... pair to an *.ini file?

2) If so is there a way to add this new
Parameter=Value

... pair at a specific location within an existing section?

3) Examples of how I'd like to use Config::IniFiles to add NEW "Parameter=Value" pairs to my *.ini file

********* BEFORE ************

My *.ini file at start, a sample Section

SECTION_KEY
PARAMETER_A=<VALUE_A>
PARAMETER_B=<VALUE_B>
PARAMETER_C=<VALUE_C>
PARAMETER_D=<VALUE_D>

********* AFTER ************

Insert one new "PARAMETER", just after PARAMETER_C, plus new parameter's associated value

SECTION_KEY
PARAMETER_A=<VALUE_A>
PARAMETER_B=<VALUE_B>
PARAMETER_C=<VALUE_C>
PARAMETER_NEW=<VALUE_NEW>
PARAMETER_D=<VALUE_D>

********* AFTER ************

Insert two new "PARAMETER"'s, just after PARAMETER_C, plus the two new parameters' associated values

SECTION_KEY
PARAMETER_A=<VALUE_A>
PARAMETER_B=<VALUE_B>
PARAMETER_C=<VALUE_C>
PARAMETER_NEW1=<VALUE_NEW1>
PARAMETER_NEW2=<VALUE_NEW2>
PARAMETER_D=<VALUE_D>

  • Comment on Question concerning Perl module Config::IniFiles

Replies are listed 'Best First'.
Re: Question concerning Perl module Config::IniFiles
by toolic (Bishop) on Dec 08, 2014 at 16:50 UTC
    Yes, you can add new param/value pairs to an .ini file, as a quick read of the Config::IniFiles POD shows:
    use warnings; use strict; use Config::IniFiles; my $cfg = Config::IniFiles->new(-file => 'my.ini'); $cfg->newval('SECTION_KEY', 'PARAMETER_new', '<VALUE_NEW>'); $cfg->RewriteConfig();

    I don't see anything about specifying the order of parameters. Why do you think order is important?

      The nature of my *.ini file is such that ordering matters

        The nature of my *.ini file is such that ordering matters

        And then what happened?

        When you wrote a small program to see what Config::IniFiles does, did it "ordering matters" well enough for you? What did it do? Was it wrong or wright? Did you use sects or mysects?...

Re: Question concerning Perl module Config::IniFiles
by RonW (Parson) on Dec 08, 2014 at 21:04 UTC

    You can use Config::INI::Reader::Ordered to read the input INI file in to an array, so that the order can be preserved. For creating the output file in a specific order, you will need to use the write_string method of Config::INI::Writer.

    Update: Or just print $out "$key = $value\n"; since the format is so simple.