punkish has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple config file that I read in with Config::Simple. Now my config file wants to become less simple. Here is an example

# in my.conf file FOO 'this' # in my script use strict; use Config::Simple; use vars qw($FOO); Config::Simple->import_from("my.conf"); # the following works fine print "$FOO\n";

Now I want to add multiple values to FOO, so

# in my.conf file FOO 'this', 'that' # in my script use strict; use Config::Simple; use vars qw(@FOO); Config::Simple->import_from("my.conf"); # the following prints nothing at all for (@FOO) { print "$_\n"; } # however use vars qw($FOO); Config::Simple->import_from("my.conf"); # the following works fine for (@$FOO) { print "$_\n"; } # however, if I provide only one value for FOO in my.conf # in my.conf file FOO 'this' # the following croaks for (@$FOO) { print "$_\n"; } Can't use string ("this") as an ARRAY ref while "strict refs" in use at ./test.pl line 41, <FH> line 1.

Well, I don't want it to croak just in case the user provided only a single value for FOO, however, I have no way of knowing before-hand if one or multiple values for FOO will be provided.

Now, more complications -- FOO is the generic container to hold values with which I have to do something. Except, FOO should be able to hold different kinds of values. For example, FOO holds values that should be inserted in a db. Except, some values in FOO should be inserted in col A while others should be inserted in col B. Yes, instead of FOO, I could have TO_INSERT_IN_A and TO_INSERT_IN_B, however, that wouldn't be extensible to arbitrary columns.I really want to be able to specify perhaps an AoA or HoA in my config file. How?

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re: Simpler Config-Simple needed
by Roy Johnson (Monsignor) on Oct 25, 2005 at 18:16 UTC
    for (ref $FOO ? @$FOO : $FOO) {
    oughta work for the array vs scalar problem. For making Config understand compound structures, I don't know. How about letting the file contain arbitrary different names and then accessing them via the hash returned by $cfg->vars();?

    Caution: Contents may have been coded under pressure.
Re: Simpler Config-Simple needed
by xdg (Monsignor) on Oct 25, 2005 at 18:21 UTC

    You may want to use YAML for your config if what you want is arbitrary perl data structures. However, before going that route, you might want to check out Config::Std and see if it gets you close. It supports multi-valued config options out of the box.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      I've heard too many complaints about YAML breaking to touch it at this point. It seems to be a finicky beast. I'd probably recommend plain old Perl data structures or even XML before YAML.
Re: Simpler Config-Simple needed
by jbware (Chaplain) on Oct 25, 2005 at 18:20 UTC
    This might be what you're looking for:
    for (split ',', $FOO) { print "$_\n"; }
    And I'm not super versed in the config files, but maybe you'll need to do:
    FOO "this, that"

    -jbWare