Question

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?

Was it helpful?

Solution

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...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top