You don't have to hardcode the names if you do not want to, but it's IMHO the best solution. There are basicaly three possible solutions.

  1. you can use the code above and specify what options allow multiple values. This way you know for sure that $config->{one}{root} is a string and $config->{one}{size} is an array. Which means you can safely write $path = $config->{one}{root} . "/processing.log"; and foreach my $size (@{$config->{one}{size}}) { ....
  2. You can split the values that contain the delimiters. This would allow the users to specify multiple values for any option in the INI file, the problem is that your script would have to be able to accept that. Otherwise you end up with $path (from the example in the previous solution) containing something like "ARRAY(0x14234234)/processing.log" which doesn't look too usefull to me. The same problem, just in the other direction would be with the "size" option, if the user did not specify several, the $config->{one}{size} would not be an array ref, but a string, so the foreach from the first solution would error out. I think this solution would complicate your script imensely.
  3. The last solution would be to split everything. Which means that $config->{sectionName}{optionName} would always be an array ref. And then if you had a use for just one value of an option you'd use $config->{sectionName}{optionName}[0] and if you wanted to do something with all of them you'd loop over @{$config->{sectionName}{optionName}}. The catch of this solution is that you might need to allow the delimiter in the value of some options. Or you would want to use a different delimiter for some options. Comma is fine if you are writing a list of color names, but is it so great for prices? What if the user enters prices=1,050? Did he/she mean two values, one and fifty or just one value, one thousand fifty?

You can choose whichever solution you like best, but my choice would be 1) any day.

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


In reply to Re^9: Config files by Jenda
in thread Config files by sparkel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.