views:

460

answers:

2

I have the following data structure in Perl code:

my $config = {
    'View::Mason' => {
        comp_root     => [
            [ 'teamsite'   => 'root/teamsite' ],
            [ 'components' => 'root/components' ],
        ],
    },
};

I'm trying to represent this structure in a Config::General configuration file.

So far I have:

<View::Mason>
    <comp_root>
        teamsite        root/teamsite
    </comp_root>
    <comp_root>
        components      root/components
    </comp_root>
</View::Mason>

Which at least makes the "comp_root" element an array reference, but I can't get it to point to another array reference.

Can this be done in Config::General?

+4  A: 

I don't believe it's possible with Config::General. For example:

use Config::General qw(SaveConfigString);

my $config = {
    'View::Mason' => {
        comp_root     => [
            [ 'teamsite'   => 'root/teamsite' ],
            [ 'components' => 'root/components' ],
        ],
    },
};

print SaveConfigString($config);

produces

<View::Mason>
    comp_root   ARRAY(0x94ea168)
    comp_root   ARRAY(0x94fbc98)
</View::Mason>

If it can't save it, odds are it can't load it.

Here's what I would do:

  1. Figure out what I want my config file to look like.
  2. Find a module capable of loading a config file like that. (Possibly making some changes to the format, if it proves too difficult to load.)
  3. If the result of step 2 is not suitable for direct use by the rest of my program, write some code to convert what the config reader gives me into what my program wants.
cjm
I'm kind of locked in to Config::General at the moment but sounds like a good approach for the next project. What I might have to do here is some post-processing to merge in multiple config settings into the final data structure the module requires.Thanks.
Hissohathair
+1  A: 

YAML might be an option for you:

use strict;
use warnings;
use Data::Dumper qw(Dumper);
use YAML::XS qw(Load);

my $config_text = '
View::Mason:
  comp_root:
    -
      - teamsite
      - root/teamsite
    -
      - components
      - root/components
';

my $config = Load($yaml_text);
print Dumper($config);
FM
While YAML can represent any arbitrary Perl data structure, it's not necessarily the most user-friendly config file format. (Although I have used it for config files that needed to contain complex data structures.)
cjm
<rant>Raw perl is friendlier than YAML; at least whitespace isn't significant</rant>
derobert