[英]Very Strange Bug When Printing in Perl
所以这是正在运行的代码片段:
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;
}
我们在for循环中,其中$count
是我们要迭代的变量。 这是此代码的特定贯穿过程的输出结果。 为了保护隐私,我将其中的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#.###
如您所见,这有几处错误。
有人知道这里发生了什么吗?
编辑语法。
输出中是否有回车符( \\r
)? 在Unix上,使用perl script.pl | od -c
perl script.pl | od -c
检查输出的每个字符。
Windows中生成的文件通常以\\r\\n
结尾。 在Unix环境中读取这些文件时, chomp
只会删除\\n
。 当我不得不担心可移植性时,我通常会跳过chomp
,只对输入内容说s/\\s+$//
(如果不想剥离所有结尾的内容,则说s/[\\r\\n]+$//
空格)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.