yazdım ve bu üç Perl altprogram anahtarları kullanmak ve onları çok yararlı buluyorum.
sub switchOne($){ # standard switch
my($prefix,$testVal,@caseVals)[email protected]_;
$s=0;
while($s<scalar(@caseVals)){
if($testVal eq $caseVals->[$s]){
return $prefix."_".$testVal;
}
$s++;
}
return $prefix."Not";
}
sub switchTwo($){ # test for 2 conditions switch = mapping x & Y
my($prefix,$testVal1,$testVal2,@caseVals1,@caseVals2)[email protected]_;
$s=0;
while($s<scalar(@caseVals)){
if($testVal1 eq $caseVals1->[$s] && $testVal2 eq $caseVals2->[$s]){
return $prefix."_".$testVal1;
}
$s++;
}
return $prefix."Not";
}
sub switchRange($){ # test for range switch
my($prefix,$testVal1,@caseVals1,@caseVals2)[email protected]_;
$s=0;
while($s<scalar(@caseVals)){
if($testVal > $caseVals->[$s]&&$testVal < $caseVals2->[$s]){
return $prefix."_".($s+1);
}
$s++;
}
return $prefix."Not";
}
############# here is the calling code
$prefix="case";
@cases=(1,12,3,45,5,61,7,8,9,10); # cases to test against/quote strings
$case=&switchOne($prefix,$testvariable,\@cases);
# prefix must be different for each switch call for different labels
#duplicate labels can cause problems
while($case ne ""){
# initialization common code block
goto $case;
case_1: # cases in array
#code
last;
case_12:
# code
last;
case_61:
last;
case_7:
last;
case_8:
last;
case_9:
last;
case_10:
last;
caseNot:
# no match comes here
#code
last;
}
# here is a dbl variable matching call example
# prefix can be in the call quoted
# both cases must be true to get a match
$case=&switchTwo("intrsctn",$test1,$test2,\@cases1,\@cases2);
while($case ne ""){
# initial code as desired
goto $case;
intrsctn_1:
# code
last;
# as many labels as cases
intrsctnNot:
last;
}
# here is a switch example to test for a variable in a range (between)
$case=&switchRange("thscase",$testvariable,\@cases1,\@cases2);
while($case ne ""){
goto $case;
thscase_1: # this returns the array index +1 on the prefix
# code
last;
# as many labels as cases
thscaseNot:
# N must be uppercase
last;
}
anahtarı özelliğini
Tüm belgeler: a "kullanmak" ifadesi istediğiniz sözdizimi dayalı sürüm numarasını içerecek şekilde http://perldoc.perl.org/perlsyn.html#Switch-statements – RickF
İhtiyaç olarak iyi. Kullanım dışı bırakma/kaldırma ipucu için – SMT