-1

I'm creating a little program that chooses between 3 different languages and outputs either, Ruby, Python, or a random element from an array.

However my if statement apparently has a syntax error in it because no matter what I try, I keep getting this:

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.

Here's the code I have as of right now:

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();

I've also tried this:

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];
}
}

I also tried using strict; and warnings

I have also tried with STDIN

All of these output the same error. What is causing this error?

13aal
  • 1,634
  • 1
  • 21
  • 47

2 Answers2

5

You're missing a semi-colon after the following:

chomp $choice

Keep in mind that the following is a valid statement:

chomp $choice if ($choice = "Ruby")

By the way,

$choice = "Ruby"

should be

$choice eq "Ruby"

= is the scalar assignment or list assignment operator.
== is the numerical comparison operator.
eq is the string comparison operator.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • 1
    Nope he's not kidding. [Perl Comparison Operators](https://www.cs.cf.ac.uk/Dave/PERL/node37.html). Note there is a difference comparing strings and numbers. If you think something said about Perl is a joke, it's probably true :D – vogomatix Nov 09 '15 at 15:14
  • Well that actually explains a whole hell of a lot thank you both! – 13aal Nov 09 '15 at 15:19
  • 2
    For future occasions just look at someone's reputation score. If they've got more than 1000, they're likely to be trying to tell the truth. If they've got more than 100,000 consider it The Word Of God :D – vogomatix Nov 09 '15 at 15:24
  • 2
    @vogomatix, Even "Gods" make mistakes, but it's probably better to assume you're missing something and ask for more information. // In this case, I think the question was rhetorical, an exclamation. I think the answer consisted solely of "You're missing a semi-colon after the following: `chomp $choice`" at that point, so I responded by providing additional information as to why the error appeared where it did (along with the off-topic comment about the wrong operator). – ikegami Nov 09 '15 at 15:44
  • @ikegami Thank you for the help, really appreciated – 13aal Nov 09 '15 at 16:46
0

You are missing a ; after the line chomp $choice

Always include use strict; and use warnings; in your scripts.

dgw
  • 13,418
  • 11
  • 56
  • 54