我正在使用配置文件(在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 ...
我们的想法是评估每个类型定义,将它们抛入哈希,然后调用以验证配置数据(以下是为了便于理解而示意图):
#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中的子程序似乎不接受参数.在上面的例子中,第一个print语句输出-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.
这根本不是我所期望的,但子程序正在被调用.
我究竟做错了什么?
编辑:
我很草率,现在一切正常.感谢弗里多.