Why can’t I match my string from standard input in Perl?

The statement $name = <STDIN>; reads from standard input and includes the terminating newline character “\n“. Remove this character using the chomp function:

print "What is your name?\n";
$name = <STDIN>;
chomp($name);
if ($name eq "Jon") {
  print "We have met before!\n";
} else {
  print "We have not met before.\n";
}

Leave a Comment