如果语句输出错误

13a*_*aal -1 perl

我正在创建一个小程序,可以选择3种不同的语言,并输出Ruby,Python或数组中的随机元素.

但是我的if语句显然有一个语法错误,因为无论我尝试什么,我都会得到这个:

syntax error at test.pl line 15, near ") {"
syntax error at test.pl line 17, near "} elsif"
Execution of test.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)

这是我现在的代码:

sub welcome {
    my @choices = qw( Perl Python Ruby );
    my $lang = 3;
    print("Welcome, to the test script, this will test what language you would like to learn.. In order to find out these choices, write this same definition in all three different languages\n");
    print("There are",  $lang,  "languages to chose from please pick one:\n");
    print "@choices";
    my $choice = <STDIN>;
    chomp $choice
    if ($choice = "Ruby") {
        print("You have chosen Ruby!\n");
    } elsif ($choice = "Python") {
        print("You have chosen Python!\n");
    } else {
        print("You're already writing in Perl!! Let me choose for you:");
        my $rand_elm = @choices[rand @choices];
    }
}
welcome();
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

my $choice = <STDIN>;
chomp $choice
if ($choice = "Ruby") 
{
    print("You have chosen Ruby!\n");
} 
elsif ($choice = "Python")
 {
    print("You have chosen Python!\n");
} 
else
 {
    print("You're already writing in Perl!! Let me choose for you:");
    my $rand_elm = @choices[rand @choices];
}
}
Run Code Online (Sandbox Code Playgroud)

我也试过用strict;warnings

我也尝试过 STDIN

所有这些都输出相同的错误.导致此错误的原因是什么?

ike*_*ami 5

您在以下情况后错过了分号:

chomp $choice
Run Code Online (Sandbox Code Playgroud)

请记住以下是有效的声明:

chomp $choice if ($choice = "Ruby")
Run Code Online (Sandbox Code Playgroud)

顺便说说,

$choice = "Ruby"
Run Code Online (Sandbox Code Playgroud)

应该

$choice eq "Ruby"
Run Code Online (Sandbox Code Playgroud)

=是标量赋值或列表赋值运算符.
==是数值比较运算符.
eq是字符串比较运算符.

  • 对于未来的场合,只需看看某人的声誉得分.如果他们有超过1000,他们可能会试图说实话.如果他们有超过10万,请考虑上帝的话语:D (2认同)
  • @vogomatix,即使是"众神"也会犯错误,但最好还是假设你错过了某些东西并要求提供更多信息.//在这种情况下,我认为问题是修辞,一个感叹.我认为答案完全是由于"你在以下后面错过了一个分号:'chomp $ choice`",所以我回答提供了关于错误出现的原因的附加信息(以及关于错误运营商的偏离主题评论). (2认同)