我正在使用配置文件(在 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.
这根本不是我所期望的,但是正在调用子例程。
我究竟做错了什么?
编辑:我很马虎,现在一切正常。感谢弗里多。