Christian
Christian

Reputation: 107

Perl Conditional not substituting in scalar value

For some reason, my if statements aren't working the way I want them to.

use strict;
use warnings;

my $syl;
my $name = "Chris";
print "Enter my name\n";
$syl = <>;
if ($syl eq $name)
{
  print "You entered my name!\n";
}
else
{
  print "That's not my name!\n";
}

It looks like it should work from all of the tutorials I've read, but when I type in "Chris" whether capitalized, lowercase, with or without quotation marks, it always evaluates to false. Use Strict and Use Warnings don't tell me I'm doing anything wrong so what, if anything, can I do?

Upvotes: 2

Views: 128

Answers (1)

DavidO
DavidO

Reputation: 13942

You need to use chomp. That strips the newline from the end of the input string that got put there when the user typed 'enter.'

$syl = <>;
chomp $syl;
#.... etc...

Upvotes: 8

Related Questions