簡體   English   中英

表示一個復雜的Perl數據結構,該結構在Config :: General中包含數組引用

[英]Representing a complex Perl data structure containing array references in Config::General

我在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中完成嗎?

我認為Config :: General不可能實現。 例如:

use Config::General qw(SaveConfigString);

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

print SaveConfigString($config);

產生

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

如果無法保存,則可能無法加載。

這就是我要做的:

  1. 弄清楚我希望我的配置文件是什么樣子。
  2. 找到一個能夠加載這樣的配置文件的模塊。 (如果事實證明很難加載,則可能需要對格式進行一些更改。)
  3. 如果步驟2的結果不適合程序的其余部分直接使用,請編寫一些代碼以將配置讀取器提供給我的內容轉換為程序想要的內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM