Perl 为什么交换机没有';不行?
这个程序抛出一个错误。问题是我必须使用Perl 为什么交换机没有';不行?,perl,switch-statement,mod-perl,perl-data-structures,Perl,Switch Statement,Mod Perl,Perl Data Structures,这个程序抛出一个错误。问题是我必须使用开关案例。如何在Perl中实现这一点 #use strict; #use warnings; my $input = print "Enter the number"; $input = <STDIN>; switch($input){ case "1" {print "UPC"} case "2" {print "ES"} case "3" {print "MS"} else {print "Enter the
开关
案例
。如何在Perl中实现这一点
#use strict;
#use warnings;
my $input = print "Enter the number";
$input = <STDIN>;
switch($input){
case "1" {print "UPC"}
case "2" {print "ES"}
case "3" {print "MS"}
else {print "Enter the correct value"}
}
#严格使用;
#使用警告;
my$input=打印“输入号码”;
$input=;
开关($input){
案例“1”{打印“UPC”}
案例“2”{打印“ES”}
案例“3”{打印“MS”}
否则{print“输入正确的值”}
}
有点不同:
use feature "switch";
given ($foo) {
when (/^abc/) { $abc = 1; }
when (/^def/) { $def = 1; }
when (/^xyz/) { $xyz = 1; }
default { $nothing = 1; }
}
您可以使用添加一个更传统的case语句,但正如robarl指出的那样,这是不推荐的
另外,永远不要注释掉使用strict;使用警告代码>作为修复问题的尝试 有点不同:
use feature "switch";
given ($foo) {
when (/^abc/) { $abc = 1; }
when (/^def/) { $def = 1; }
when (/^xyz/) { $xyz = 1; }
default { $nothing = 1; }
}
您可以使用添加一个更传统的case语句,但正如robarl指出的那样,这是不推荐的
另外,永远不要注释掉使用strict;使用警告代码>作为修复问题的尝试 您需要导入交换机才能使用它:
use Switch;
然而,这一转变一直在进行。见这个问题:
此处讨论了一些备选方案(及其试验状态):
总之,如果您使用的是Perl>5.10.1,则可以将以下内容用于非弃用、非实验性的交换机:
use v5.10.1;
for ($var) {
when (/^abc/) { $abc = 1 }
when (/^def/) { $def = 1 }
when (/^xyz/) { $xyz = 1 }
default { $nothing = 1 }
}
您需要导入交换机才能使用它:
use Switch;
然而,这一转变一直在进行。见这个问题:
此处讨论了一些备选方案(及其试验状态):
总之,如果您使用的是Perl>5.10.1,则可以将以下内容用于非弃用、非实验性的交换机:
use v5.10.1;
for ($var) {
when (/^abc/) { $abc = 1 }
when (/^def/) { $def = 1 }
when (/^xyz/) { $xyz = 1 }
default { $nothing = 1 }
}
你的标题是什么意思?对于将来的参考,说明你收到的错误消息通常是有帮助的。你的标题是什么意思?对于将来的参考,说明你收到的错误消息通常是有帮助的。它不是不推荐的,但它是实验性的。它不是不推荐的,但它是实验性的。