1

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?

1
  • Hmmm... after digging into it a bit more it seems the answer is "you can't do that with Config::General" Commented Sep 1, 2009 at 6:21

2 Answers 2

4

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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);

2 Comments

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.)
<rant>Raw perl is friendlier than YAML; at least whitespace isn't significant</rant>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.