in reply to Re^2: Config files
in thread Config files

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

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

Replies are listed 'Best First'.
Re^4: Config files
by sparkel (Acolyte) on Dec 10, 2004 at 22:27 UTC
    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

        Hi Jenda

        Thank you for the reply. I understood:

        @files = split /\s*,\s*/, $config->{path}{files};

        If the arrays can occur at any place in the config file along with scalar values, I did not understand how the following code takes care of that

        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}]; } }

        Actually, I did not understand the following line:

        foreach my $value (qw(files colors and other options that should be +arrays)) {

        Thanks.