简体   繁体   English

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

[英]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. 如您所见,这有几处错误。

  1. The line after the line starting with "Address" starts with a space instead of the intended $. 以“地址”开头的行之后的行以空格开头,而不是预期的$。
  2. The line after that starts with " to the address entries" instead of "Final value of $toPrint". 此后的行以“到地址条目”开头,而不是“ $ toPrint的最终值”。
  3. That line also does not have " to the address entries" at the end of it. 该行的末尾也没有“至地址条目”。
  4. There is an extra line break between the last two lines. 最后两行之间有一个额外的换行符。
  5. The last line does not contain the word "Added zix01 A" even though the variable it's supposed to be printing does. 最后一行不包含单词“ Added zix01 A”,即使它应该打印的变量也是如此。
  6. The last line also does not include the IP it is supposed to be printing, except for the last 5 characters of it, which are coming out after the rest of the string but before the line break. 最后一行还不包括应该打印的IP,但最后5个字符除外,它们在字符串的其余部分之后但在换行符之前出现。

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.

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