2

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?

cjm
  • 61,471
  • 9
  • 126
  • 175
Christian
  • 107
  • 1
  • 1
  • 5
  • 8
    When debugging this, print the values...`print "That ($syl) is not my name; ($name) is my name!\n";`. And when you see the unexpected line break in the output, you get a good idea what is wrong. This is a basic technique in debugging in any language; when something goes wrong, print the erroneous values (and the expected values). – Jonathan Leffler Jun 11 '11 at 04:23

1 Answers1

8

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...
DavidO
  • 13,812
  • 3
  • 38
  • 66