我正在使用一个配置文件(YAML格式)来定义类型,这些类型稍后用于验证应用程序所需的其他配置值:
---
action: >
use List::MoreUtils;
my $value = $_;
any { $value eq $_ } qw(fatal keep merge non-fatal replace);
dir : return defined $_ ? -d $_ : -1;
file : return defined $_ ? -f $_ : -1;
string: 1;
---
config-element:
value: foo
type : file
etc ...
这个想法是对每个类型的定义进行eval
,将它们扔进哈希表中,然后调用以验证配置数据(以下是为了易于理解而简化的示意图):
#throw sub refs into hash
my %type_sub;
foreach my $key (keys %$type_def_ref) {
my $sub_str = "sub {$type_def_ref->{$key}}";
$type_sub{$key} = eval $sub_str;
}
#validate (myfile is a real file in the cwd)
print $type_sub{file}->('myfile'),"\n";
print $type_sub{action}->('fatal'), "\n";
问题在于%type_sub中的子程序似乎不接受参数。在上述情况下,第一条打印语句输出
-1
,而第二条则输出:Use of uninitialized value $value in string eq at (eval 15) line 1.
Use of uninitialized value $_ in string eq at (eval 15) line 1.
Can't call method "any" without a package or object reference at
(eval 15) line 1.
这并不是我预期的结果,但是子程序正在被调用。
我做错了什么?
编辑:我太草率了,现在一切都正常了。谢谢Friedo。