perlif-statementstatements

Having Trouble with Perl if then Statements


I'm having trouble making yes/no questions with Perl, and I just couldn't figure it out. I'm kinda a noob at this.

#! usr/bin/perl

print "Hello there!\n";
print "What is your favorite game?\n";
$name = <STDIN>;
chomp $name;
print "That's awesome! $name is my favorite game too!\n";

print "Do you think this is a fun script (Y/n) \n";
$status = <STDIN>;
if [ $status = "y" ]: then

print "YAY! I knew you would like it!\n";

if [ $status = "n" ]: then

print "You suck, not me!\n";

What am I doing wrong?


Solution

  • if [ is a shell syntax. In Perl, you should use

    if (...) {
    

    Also, = is the assignment operator. For string equality, use eq:

    if ($status eq 'y') {
        print "YAY\n";
    

    Before comparing, you should chomp $status the same way you're already chomping $name.

    Also, note that Y and y are not equal.

    Also, your first ("shebang") line misses the starting slash:

    #! /usr/bin/perl