Callum B.
Callum B.

Reputation: 13

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?

Upvotes: 1

Views: 121

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263627

if [ $status = "y" ]: then

That's (almost) Bourne (or bash) shell syntax (except that the : should be ;). The equivalent Perl code is:

if ($status eq "y") {
    # ...
}

eq is equality comparison for strings; == compares numbers.

(The other thing you're doing wrong is not including the error message in your question.)

For example:

$status = <STDIN>;
chomp $status;
if ($status eq "y") {
    print "YAY! I knew you would like it!\n";
}

There are some other things you can do to improve your Perl code. For example, you should always have:

use strict;
use warnings;

near the top of the source file (which will require declaring your variables, probably with my). I suggest getting this program working first before worrying about that, but it's definitely something you'll want to do in the long term.

Upvotes: 2

David W.
David W.

Reputation: 107090

First, always, always put use strict; and use warnings; at the top of your programs. This will catch all sorts of errors such as using = in if statements. = sets a variable's value. == tests numeric equality and eq tests string equality.

Here's your program rewritten. The first line with #! searches your PATH for an executable Perl. This way, you don't have to worry whether Perl is in /usr/bin/perl or /bin/perl or /usr/local/bin/perl.

#! /usr/bin/env  perl
use strict;
use warnings;
use feature qw(say);   # Allows the use of the "say" command

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

print "Do you think this is a fun script (Y/n) \n";
my $status = <STDIN>;
chomp $status;
if ( $status eq "y" ) {
    say "Yay! I knew you would like it!";
}
elsif ( $status eq "n" ) {
    say "You suck, not me!";
}

A better way may be to check whether the input started with a y or not:

if ( $status =~ /^y/i ) {   # Did it start with a 'y' or 'Y'?
    say "Yay! I knew you would like it!";
else {
    say "You suck, not me!";
}

Note the use of my to declare variables. This is something use strict; requires and will catch a lot of programming mistakes. Note that say is like print, but I don't have to keep putting \n on the end.

Upvotes: 1

choroba
choroba

Reputation: 242343

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

Upvotes: 4

Related Questions