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

Hi Jenda,

Thanks for the suggestion. I think other than option 1, as you have suggested, other options would complicate a lot of things...and hence I'll specify the options that allow multiple values.

If the config file contains:

[path] root = C:\test code = %root%\code1 [array] files = hello.txt,hi.txt,howdy.txt dirs = %code%, %root%, %code%

Then, for the "dirs" array, how can I change the following regular expression to find and store all values between %%..since there can be more than one (as seen in the dirs array)...

$value =~ s{%(?:\[([^\]%]+)\])?([^%]*)%}{ if ($2 eq '') { '%' } elsif ($1 eq '') { $INIhashref->{$sectionname}{$2} } else { $INIhashref->{$1}{$2} } }ge;

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

    It already does, thanks to the /g option.

    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,

      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.

        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