in reply to Config files

This is Perl, you can do anything. So desing whatever syntax will be most comfortable to your users. I usualy use %variablename% in config files and templates and so far it worked fine with my users. (As you can guess by now they are all Windows users.)

If you happen to use Config::IniHash you can specify a function to be called for each option read and perform whatever transformations you like. Eg. if you wanted the users to use %foo% if they want to include the "foo" option in the current section and %[sec]foo% to include the "foo" option in the "sec" section. you could do it like this:

use Config::IniHash qw(ReadINI); $config = ReadINI('c:\temp\zkINI.ini', systemvars => 0, forValue => sub { my ($name, $value, $sectionname, $INIhashref) = @_; $value =~ s{%(?:\[([^\]%]+)\])?([^%]*)%}{ if ($2 eq '') { '%' } elsif ($1 eq '') { $INIhashref->{$sectionname}{$2} } else { $INIhashref->{$1}{$2} } }ge; return $value; } ); use Data::Dumper; print Dumper($config);
This of course assumes that you do not try to reference an option that was not yet specified :-)

Jenda
We'd like to help you learn to help yourself
Look around you, all you see are sympathetic eyes
Stroll around the grounds until you feel at home
   -- P. Simon in Mrs. Robinson

Replies are listed 'Best First'.
Re^2: Config files
by sparkel (Acolyte) on Dec 10, 2004 at 17:56 UTC
    Hi Jenda,

    Thanks for the response. That is exactly what I was looking for.

    I have been trying to go over your code (excellent code by the way) and the hash part is hard for me to understand.

    Could you please explain me how I can obtain the different values from the hash?

    Thanks.

      The thing returned by ReadINI() is a reference to a hash of hashes. This means that:

      • to access a specific value in a specific section: $config->{sectionName}{valueName}
      • to list the sections: foreach my $sectionName (keys %$config) {...
      • to list the values in a section:
        foreach my $name (%{$config->{sectionName}}) { print "$name = $config->{sectionName}{$name}\n"; }
        or
        for my $Section ($config->{sectionName) { foreach my $name (%$Section) { print "$name = $Section->{$name}\n"; }} or ...
      I think you should read the perlreftut and perldsc manpages.

      Jenda
      We'd like to help you learn to help yourself
      Look around you, all you see are sympathetic eyes
      Stroll around the grounds until you feel at home
         -- P. Simon in Mrs. Robinson

        Thanks Jenda, That made a hashes a lot clearer...One other question I had with Config::IniHash...What is the best way to store arrays in the config file?

        So if I had an array:

        @colors = qw ( red blue green )

        and I wanted to put it in the config file so anybody could change it, but also could be read by the Config::IniHash module, what is the best way to write it in the config file?

        I am following your example and have changed other variables in the config file to look something like:

        [path] root = C:\test code = %root%\code1