如何根据用户输入(字符串)在perl中调用子例程?
我是perl编程新手,我正在尝试调用与用户输入有关的子例程:如何根据用户输入(字符串)在perl中调用子例程?,perl,if-statement,user-input,subroutine,Perl,If Statement,User Input,Subroutine,我是perl编程新手,我正在尝试调用与用户输入有关的子例程: print "Would you like to [A]dd a new student or [R]eturn to the previous menu?"; $tempCommand = <>; if($tempCommand eq "A") {addStudent()} elsif($tempCommand eq "R") {mainmenu()} else{mainmenu()}
print "Would you like to [A]dd a new student or [R]eturn to the previous menu?";
$tempCommand = <>;
if($tempCommand eq "A") {addStudent()}
elsif($tempCommand eq "R") {mainmenu()}
else{mainmenu()}
print“您想[A]添加新学员还是[R]返回上一个菜单?”;
$tempCommand=;
如果($tempCommand eq“A”){addStudent()}
elsif($tempCommand eq“R”){main menu()}
else{main menu()}
调用始终以else条件结束,即使
我输入A或R。您的问题是,当您使用
从标准输入数据
读取时,返回并存储在$tempCommand
中的值将附加一个换行符。您需要使用chomp()
函数删除它
chomp($tempCommand = <>);
chomp($tempCommand=);
您的问题是,当您使用
读取标准输入数据时,返回并存储在$tempCommand
中的值将附加一个换行符。您需要使用chomp()
函数删除它
chomp($tempCommand = <>);
chomp($tempCommand=);
您需要从用户输入中选择换行符,它应该可以工作:
use strict;
use warnings;
print "Would you like to [A]dd a new student or [R]eturn to the previous menu? ";
chomp(my $tempCommand = <>);
if ($tempCommand eq "A") {
addStudent()
}
elsif ($tempCommand eq "R") {
mainmenu()
}
else {
mainmenu()
}
sub addStudent {
print "In sub \"Addstudent\"";
}
sub mainmenu {
print "In sub \"Mainmenu\"";
}
使用严格;
使用警告;
打印“您想[A]添加新学员还是[R]返回上一个菜单?”;
chomp(我的$tempCommand=);
if($TEMP命令等式“A”){
addStudent()
}
elsif($tempCommand eq“R”){
主菜单()
}
否则{
主菜单()
}
副学生{
打印“在子项中添加学生”;
}
子主菜单{
打印“在子菜单”主菜单中”;
}
您需要从用户输入中选择换行符,它应该可以工作:
use strict;
use warnings;
print "Would you like to [A]dd a new student or [R]eturn to the previous menu? ";
chomp(my $tempCommand = <>);
if ($tempCommand eq "A") {
addStudent()
}
elsif ($tempCommand eq "R") {
mainmenu()
}
else {
mainmenu()
}
sub addStudent {
print "In sub \"Addstudent\"";
}
sub mainmenu {
print "In sub \"Mainmenu\"";
}
使用严格;
使用警告;
打印“您想[A]添加新学员还是[R]返回上一个菜单?”;
chomp(我的$tempCommand=);
if($TEMP命令等式“A”){
addStudent()
}
elsif($tempCommand eq“R”){
主菜单()
}
否则{
主菜单()
}
副学生{
打印“在子项中添加学生”;
}
子主菜单{
打印“在子菜单”主菜单中”;
}