我找不到实现配置继承的 Perl 模块。
但是编写一个简单的过滤器来解决继承并不是那么复杂。给定一个 HoH,如果我们将配置中的每个条目都视为一个增量,我们可以像下面这样解析继承:
use Algorithm::C3; # must be installed from CPAN
sub resolve_inheritance {
my $input = shift;
my %output;
# hash of arrays where the arrays contain all parents
my %child_parent_relations =
map { my ($c, @p) = split /\s*:\s*/; $c => [\@p, $input->{$_}] } keys %$input;
my $get_parents = sub { @{ $child_parent_relations{shift()}[0] } };
my $get_data = sub { $child_parent_relations{shift()}[1] };
# prepare stuff for C3 resolution
my $resolution_cache = {};
my $resolve = sub {
my $child = shift;
Algorithm::C3::merge($child, $get_parents, $resolution_cache);
};
# now we go through all childs, and build temporary hashes from the C3 linearization.
for my $child (keys %child_parent_relations) {
my @linearization = $resolve->($child);
my %temp;
for my $delta_name (reverse @linearization) {
my $delta = $get_data->($delta_name);
@temp{keys %$delta} = values %$delta;
}
# save the data in the output:
$output{$child} = \%temp;
}
return \%output;
}
测试:
my $input = {
's1' => { A => 1 },
's2:s1' => { A => 2, B => 2 },
's3:s1' => { B => 3, C => 3 },
's4:s3:s2' => { }
};
my $expected = {
's1' => { A => 1 },
's2' => { A => 2, B => 2 },
's3' => { A => 1, B => 3, C => 3 },
's4' => { A => 2, B => 3, C => 3 },
};
use Test::More tests => 1;
is_deeply resolve_inheritance($input), $expected, 'C3 resolution';
如您所见,这赋予了:
-operator 右关联性(从右到左组合)。
如果您想要深度优先分辨率,即:
my $expected = {
's1' => { A => 1 },
's2' => { A => 2, B => 2 },
's3' => { A => 1, B => 3, C => 3 },
's4' => { A => 1, B => 3, C => 3 }, # s3 completely overwrites s2
};
然后你会得到不同的结果。这可以通过首先继承每个父级,然后仅合并直接父级而不是整个层次结构来完成。在单继承的情况下,深度优先线性化和 C3 分辨率的结果是等效的。
为了支持深度优先分辨率,我们将上面代码中的函数换掉$resolve
,并将其更改为
my $resolve; $resolve = sub {
my $child = shift;
return $child, map { $resolve->($_) } $get_parents->($child);
};
这是最小的更改,但当然可以通过仅保留每个父级的最左侧出现来提高效率:
use List::MoreUtils 'uniq';
my $resolve; $resolve = sub {
my $child = shift;
return uniq $child, map { $resolve->($_) } $get_parents->($child);
};
如果用于输入的数据结构可以记住部分的顺序,那么这种深度优先的解决方案将变得更加容易。但是,哈希是无序的。