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

Hi Jenda,

It only works if the section name is added on. So,

[path] root = C:\test [path2] m = C:\path2 root = C:\code [check] #this will work p = %[path]root%, %[path2]m% [check2] #this will not work...even though m is only entered once q = %[path]root%, %m%

Please tell me how I can change the code to recognize the correct variable when the path is not mentioned...since m is seen only once in the config file...it should be able to recongnize it...but it does not seem to. Thank you.

Replies are listed 'Best First'.
Re^13: Config files
by Jenda (Abbot) on Dec 12, 2004 at 01:55 UTC

    I told you the code looks into the current section if you do not specify the section name. I don't think it's wise to search for the values in other sections, but if you want the code can easily be changed to do that.

    $value =~ s{%(?:\[([^\]%]+)\])?([^%]*)%}{ if ($2 eq '') { '%' } elsif ($1 eq '') { if (exists $INIhashref->{$sectionname}{$2}) { $INIhashref->{$sectionname}{$2} } else { my $found; foreach my $section (values %$INIhashref) { if (exists $section->{$2}) { $found = $section->{$2}; last; } } $found } } else { $INIhashref->{$1}{$2} } }ge;
    With this if the user doesn't specify the section name then the option is first looked up in the current section and then in all already read sections. If the option was specified in several versions then you end up with the value from a "random" one. Hashes do not preserve order so the code cannot tell in what order were the section in the INI file.

    If you did want to take the into consideration you'd have to instruct Config::IniHash to store the order in @{$config->{'__SECTIONS__'}} using the sectionorder parameter. See the docs. You'd then loop through the @$INIhashref{@{$INIhashref->{'__SECTIONS__'}}} instead of <code>values %$INIhashref. (The scary looking code in the previous line is a hash slice, using the array referenced by $config->{'__SECTIONS__'} as the list of keys.

    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,

      Thanks for the reply. The config::IniHash seems to convert all the keys and values from the config file to lowercase. Is there a way I can preserve the case changes that are seen in the config file after reading them in the hashes?
      Thank you

        Maybe it's time to read the module docs. There is an option that controls the case changes.

        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