【发布时间】:2011-12-01 19:51:26
【问题描述】:
尝试使用 map 和 grep 来解决这个问题,知道有什么问题吗?我不断得到一个 Can't use string ("10") as a HASH ref while "strict refs" 当我尝试打印新哈希的值时出错
sub scrub_hash{
my($self,$hash_ref) = @_;
my $scrubbed_hash = map { defined $hash_ref->{$_} ? ($_ => $hash_ref->{$_}) : () } keys %{$hash_ref};
print STDERR "[scrub]". $_."\n" for values %{$scrubbed_hash};
}
在这里使用 ...
my $params_hash = $cgi->Vars();
my $scrubbed = $self->scrub_empty_params($params_hash) if $self->is_hash($params_hash);
在这种情况下,当通过 post 提交表单时未定义的参数仍显示为 key1=&key2= 所以scrub 将它们关闭
【问题讨论】:
-
还有:
delete @hash{ grep !defined($hash{$_}), keys %hash };