繁体   English   中英

在Perl中打印时非常奇怪的错误

[英]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#.###

如您所见,这有几处错误。

  1. 以“地址”开头的行之后的行以空格开头,而不是预期的$。
  2. 此后的行以“到地址条目”开头,而不是“ $ toPrint的最终值”。
  3. 该行的末尾也没有“至地址条目”。
  4. 最后两行之间有一个额外的换行符。
  5. 最后一行不包含单词“ Added zix01 A”,即使它应该打印的变量也是如此。
  6. 最后一行还不包括应该打印的IP,但最后5个字符除外,它们在字符串的其余部分之后但在换行符之前出现。

有人知道这里发生了什么吗?

编辑语法。

输出中是否有回车符( \\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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM