还有另一种方法可以测试第四个“何时”的哈希值是否真实?
#!/usr/local/bin/perl
use warnings;
use 5.012;
my %hash;
$hash{one} = 0;
$hash{two} = 2;
$hash{three} = 0;
print ": ";
my $aw = <>;
chomp $aw;
given ( $aw ) {
when ( 'cat' ) { say '$aw eq cat' }
when ( 'mouse' ) { say '$aw eq mouse' }
when ( 'sheep' ) { say '$aw eq sheep' }
when ( !( !$hash{$_} ) ) { say '$hash{$_} is true' } ###
default { say 'something else' }
}
答案 0 :(得分:2)
好吧,我会用更短的:
when ( !!$hash{ $_ } ) { ... }
但是,那就是它。
答案 1 :(得分:0)
when (%hash) { ... };
毕竟智能匹配!