문제

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?

도움이 되었습니까?

해결책

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...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top