我期待着知道一些最好的方法来管理属性文件。
我们有一组设备(比方说N)。这些设备中的每一个都有特定的属性。例如,设备A具有属性
A.a11=valuea11
A.a12=valuea12
。
设备B具有属性
B.b11=valueb11
B.b12=valueb12。
除此之外,它们还有一些适用于所有设备的公共属性。
X.x11=valuex11
X.x12=valuex12
我正在写一个在这些设备上运行一些测试套件的自动化程序。一次,测试脚本在单个设备上运行。设备名称将作为参数传递。基于设备名称,代码将获取相应的属性和公共属性,并使用这些属性更新设备。例如,对于设备A,代码将获取A.a11,A.a12 (设备A特定的)和X.x11,X.x12 (公共)属性,并在运行测试脚本之前将其上载到设备。
因此,在代码中,我需要管理这些属性,以便只将设备特定的和通用的属性上传到设备,而忽略其余的属性。我是这样管理它的
if ($device eq 'A') then
upload A's properties
elsif ($device eq 'B') then
upload B's properties
endif
upload Common (X) properties.随着设备数量的不断增加,以这种方式管理设备变得有点困难。
因此,我期待着其他一些最好的方法来管理这些属性。
发布于 2011-08-16 16:37:48
这是一个很好的例子,v (在广义的OOP文献中也称为特征)将是有用的。
与传统的对象不同,是一个类,对象*扮演*角色。
有关更多信息,请查看相应的驼鹿文档。
示例:
package Device::ActLikeA;
use Moose::Role;
has 'attribute' => (
isa => string,
is => 'rw',
default => 'Apple',
);
sub an_a_like_method {
my $self = shift;
# foo
}
1;现在我有了一个名为Device::ActLikeA的角色,我该如何处理它呢?
好的,我可以将角色应用于类,在ActLikeA中定义的代码和属性将在类中可用:
package Device::USBButterChurn;
use Moose;
does 'Device::ActLikeA';
# now has an attribute 'attribute' and a method 'an_a_like_method'
1;您还可以将角色应用于类的各个实例。
package Device;
use Moose;
has 'part_no' => (
isa => 'Str',
is => 'ro',
required => 1,
);
has 'serial' => {
isa => 'Str',
is => 'ro',
lazy => 1,
build => '_build_serial',
);
1;然后是查看部件并应用适当角色的主代码:
my @PART_MATCH = (
[ qr/Foo/, 'Device::MetaSyntacticVariable' ],
[ qr/^...-[^_]*[A][^-], 'Device::ActLikeA; ],
[ qr/^...-[^_]*[B][^-], 'Device::ActLikeB; ],
# etc
);
my $parts = load_parts($config_file);
for my $part ( @$parts ) {
my $part_no = $part->part_number();
for my $_ (@PART_MATCH) {
my ($match, $role) = @$_;
$part->apply_role($role)
if $part_no =~ /$match/;
}
}发布于 2011-08-16 19:52:08
这里有一个非常直接的方法。
首先,您需要一种方法来指示A“是-a”X和B“是-a”X,即X是A和B的父级。
然后,您的upload_device例程将如下所示:
sub upload_properties {
my $device = shift;
... upload the "specific" properties of $device ...
for my $parent (parent's of $device) {
upload_properties($parent);
}
}一种实现方式:
用配置文件中的一行表示"is-a“关系,如下所示:
A.isa = X(您可以随意使用其他语法-您使用的语法将取决于您希望如何解析文件。)
从配置文件中,创建所有设备的散列,如下所示:
$all_devices = {
A => { a11 => valuea11, a12 => valuea12, isa => [ 'X' ]},
B => { b11 => valueb11, b12 => valueb12, isa => [ 'X' ] },
X => { x11 => valuex11, x12 => valuex12, isa => [] },
}upload_properties例程:
sub upload_properties {
my ($device) = @_;
for my $key (keys %$device) {
next if $key eq "isa";
... upload property $key => $device->{$key} ...
}
my $isa = $device->{isa}; # this should be an array ref
for my $parent_name (@$isa) {
my $parent = $all_devices->{$parent_name};
upload_properties($parent);
}
}
# e.g. to upload device 'A':
upload_properties( $all_devices->{'A'} );发布于 2011-08-16 19:53:55
您可以通过将设备属性存储在散列中来消除大的if-else链。然后,您只需确保特定的$device出现在该散列中。
#!/usr/bin/perl
use warnings;
use strict;
my %vals = (
A => {
a11 => 'valuea11',
a12 => 'valuea12',
},
B => {
b11 => 'valueb11',
b12 => 'valueb12',
},
);
foreach my $device qw(A B C) {
if (exists $vals{$device}) {
upload_properties($vals{$device});
}
else {
warn "'$device' is not a valid device\n";
}
}
sub upload_properties {
my($h) = @_;
print "setting $_=$h->{$_}\n" for sort keys %$h; # simulate upload
print "\n";
}https://stackoverflow.com/questions/7074924
复制相似问题