表示 Config::General 中包含数组引用的复杂 Perl 数据结构

发布于 2024-08-03 23:05:02 字数 737 浏览 5 评论 0原文

我在 Perl 代码中有以下数据结构:

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

我试图在 Config::General 配置文件。

到目前为止,我已经:

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

这至少使“comp_root”元素成为数组引用,但我无法让它指向另一个数组引用。

这可以在 Config::General 中完成吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-08-10 23:05:02

我不相信 Config::General 可以做到这一点。例如:

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>

如果无法保存它,则很可能无法加载它。

这就是我要做的:

  1. 找出我想要的配置文件的样子。
  2. 找到一个能够加载这样的配置文件的模块。 (如果事实证明加载起来太困难,可能会对格式进行一些更改。)
  3. 如果步骤 2 的结果不适合我的程序的其余部分直接使用,请编写一些代码将配置读取器给我的内容转换为什么我的程序想要。

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.
心凉怎暖 2024-08-10 23:05:02

YAML 可能是您的一个选择:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文