in reply to Re: Config files
in thread Config files

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.

Replies are listed 'Best First'.
Re^3: Config files
by Jenda (Abbot) on Dec 10, 2004 at 18:44 UTC

    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

        Just choose a delimiter and split the values after you read them. What delimiter to use depends on the characters allowed in the values, but most of the time comma is good enough. So assuming the INI contains:

        [path] files=foo.txt,bar.txt,baz.csv
        you'd just need to do something like:
        @files = split /\s*,\s*/, $config->{path}{files}; # the \s* removes the optional whitespace around delimiters
        You might do the splitting inside the forValue=> callback, but I think that would just complicate the code. You'd have to specify what values to split, because you'd definitely not want to split everything that contains the delimiter. So I think it's better to split after everything is read.

        If you did want to modify the $config structure to contain certain values as arrays you might do something like this:

        foreach my $Section (values %$config) { # $Section is a hash ref containing the values of the section, not th +e section name! foreach my $value (qw(files colors and other options that should be +arrays)) { $Section->{$value} = [split /\s*,\s*/, $Section->{$value}]; } }
        After this the $config->{anySection}{files} is no longer a string but a reference to an anonymous array containing the individual items.

        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