我有一个我想测试的值($field).阅读perl doc(
http://perldoc.perl.org/Switch.html#Allowing-fall-through),并认为我有这个钉.似乎没有,因为如果我通过’Exposure Bias’,就没有输出,尽管’Exposure Bias Value’可以正常工作.它没有抛出任何错误,所以我不知道.
use Switch; use strict; use warnings; my $field = 'Exposure Bias'; switch($field){ case 'Exposure Bias' {next;} case 'Exposure Bias Value' {print "Exp: $field\n";} }
更新
我似乎假装错了.如果匹配任何一种情况,我想用这个开关做的是打印运行.我认为接下来会将控制权传递给下一个案例的代码,但这是我的错误.
我如何对此进行编码,以便在第一种情况匹配时第二种情况下的代码运行?
工作方案
given($field){ when(['Exposure Bias','Exposure Bias Value']){print "Exp: $field\n";} }
解决方法
DVK关于为什么你的开关没有按预期工作的评论是正确的,但是他忽略了提及一种更好,更安全的方式来实现你的开关.
Switch使用source filters和has been deprecated构建,最好避免使用.如果您正在使用Perl 5.10或更高版本,请使用given和when来构建switch语句:
use strict; use warnings; use feature qw(switch); my $field = 'Exposure Bias'; given($field) { when ([ 'Exposure Bias','Exposure Bias Value',]) { print 'Exp: ' . $field . "\n"; } }
有关详细信息,请参阅perlsyn.