为什么我不能在 Perl 中匹配来自标准输入的字符串? [英] Why can't I match my string from standard input in Perl?
本文介绍了为什么我不能在 Perl 中匹配来自标准输入的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我的脚本不能正常工作?
Why will my script not work correctly?
我关注了一个 YouTube 视频并为他工作.
I follow a YouTube video and worked for the guy.
我在 Windows 上使用 ActiveState ActivePerl 5.12.2.1202 运行 Perl
I am running Perl on Windows using ActiveState ActivePerl 5.12.2.1202
这是我的小代码块.
print "What is your name?
";
$name = <STDIN>;
if ($name eq "Jon") {
print "We have met before!
";
} else {
print "We have not met before.
";
}
代码自动跳转到else语句,甚至不检查if语句.
The code automatically jumps to the else statement and does not even check the if statement.
推荐答案
$name = <STDIN>;
语句从标准输入中读取并包含终止换行符 "
代码>".使用
chomp
函数删除这个字符:
The statement $name = <STDIN>;
reads from standard input and includes the terminating newline character "
". Remove this character using the chomp
function:
print "What is your name?
";
$name = <STDIN>;
chomp($name);
if ($name eq "Jon") {
print "We have met before!
";
} else {
print "We have not met before.
";
}
这篇关于为什么我不能在 Perl 中匹配来自标准输入的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文