[英]Very Strange Bug When Printing in Perl
So here's the snippet of code that's running: 所以这是正在运行的代码片段:
if($verbose){
my $toPrint = "Added ";
if($types[$count]){
$toPrint .= "$types[$count] ";
print "Type: $types[$count]\n"; #Manual debugging
print "\$toPrint: $toPrint\n"; #Manual debugging
}
if($addressTypes[$count]){
$toPrint .= "$addressTypes[$count] ";
print "Address Type: $addressTypes[$count]\n"; #Manual debugging
print "\$toPrint: $toPrint\n"; #Manual debugging
}
if($addresses[$count]){
$toPrint .= "$addresses[$count] ";
print "Address: $addresses[$count]\n"; #Manual Debugging
print "\$toPrint: $toPrint\n"; #Manual debugging
}
$toPrint .= "to the address entries\n";
print "Final value of \$toPrint before printing: $toPrint\n"; #Manual debugging
print $toPrint;
}
We're inside a for loop, with $count
being the variable we're iterating on. 我们在for循环中,其中
$count
是我们要迭代的变量。 Here's what the output of a particular runthrough of this code looks like. 这是此代码的特定贯穿过程的输出结果。 For the sake of privacy, I've replaced the IP in it with ###.###.###.###.
为了保护隐私,我将其中的IP替换为###。###。###。###。
Type: zix01
$toPrint: Added zix01
Address Type: A
$toPrint: Added zix01 A
Address: ###.###.###.###
toPrint: Added zix01 A ###.###.###.###
to the address entries before printing: Added zix01 A ###.###.###.###
to the address entries#.###
As you can see, there are several things wrong with this. 如您所见,这有几处错误。
Does anyone have any idea what's going on here? 有人知道这里发生了什么吗?
edited for grammar. 编辑语法。
Are there carriage return characters ( \\r
) somewhere in your output? 输出中是否有回车符(
\\r
)? On Unix use perl script.pl | od -c
在Unix上,使用
perl script.pl | od -c
perl script.pl | od -c
to examine each character of output. perl script.pl | od -c
检查输出的每个字符。
Files generated in Windows often have \\r\\n
line endings. Windows中生成的文件通常以
\\r\\n
结尾。 When those files are read in a Unix environment, chomp
will only remove the \\n
. 在Unix环境中读取这些文件时,
chomp
只会删除\\n
。 When I have to worry about portability, I usually skip chomp
and just say s/\\s+$//
on the input (or s/[\\r\\n]+$//
if you don't want to strip all the trailing whitespace). 当我不得不担心可移植性时,我通常会跳过
chomp
,只对输入内容说s/\\s+$//
(如果不想剥离所有结尾的内容,则说s/[\\r\\n]+$//
空格)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.