我正在运行Windows XP,带有EPIC的Eclipse 3.2和用于我的Perl解释器的Cygwin,我得到了意想不到的结果.
仅供参考...当我在我的Ubuntu发行版(VMware,同一台PC)上运行时,我得到了预期的结果.为什么?
############ CODE: #############
use warnings;
use strict;
my $test = "test";
my $input = <STDIN>;
print length $test, " ", length $input, "\n";
chomp $input;
print "|$test| |$input| \n"; #The bars indicate white space, new line, etc...
print length $test, " ", length $input, "\n";
if ($test eq $input) {
print "TIME TO QUIT";
}
Run Code Online (Sandbox Code Playgroud)
Windows XP上的结果:
test <-- My input
4 6 <-- Lengths printed before chomp
|test| |test <-- Print the variables after chomp
| <-- There is still a new line there
4 5 <-- Lengths after the initial chomp
Run Code Online (Sandbox Code Playgroud)
鉴于Windows XP在问题中的数字,差异必须归因于CRLF(回车,换行)处理.的chomp,它出现排除后,LF但不是CR; 打印将CR转换为CR LF.
chomp的Perl文档说如果你为Windows($/ = "\r\n";)正确设置EOL ,那么chomp应该正确地做它的东西:
$/ = "\r\n";
$test = "test\r\n";
print "<<$test>>\n";
chomp $test;
print "<<$test>>\n";
Run Code Online (Sandbox Code Playgroud)
输出的十六进制转储产生:
0x0000: 3C 3C 74 65 73 74 0D 0A 3E 3E 0A 3C 3C 74 65 73 <<test..>>.<<tes
0x0010: 74 3E 3E 0A t>>.
0x0014:
Run Code Online (Sandbox Code Playgroud)
我不确定为什么$/不自动设置 - 可能是Cygwin混淆了事情(假装太成功它在Unix上运行).