如何在 perl 中向标准输出添加新行

How to add new lines to stdout in perl

我在 .cgi 文件的末尾有这段代码:

if ($cmd eq 'set'){
         my @args = ('ranking', 'set', $bug_id, $rank);
         system(@args) == 0
           or die "system @args failed: $?";
 }

您可以输入数据,它被解释为系统命令。 HTML 页面上的示例输出如下:

Current ranking is 1, I will decrement all bugs with higher ranking by one Not shifting any bug, since there is another one with ranking Bug 111 removed from ranking Bug 111 inserted into ranking at position 1

(全部在一行中)

但我需要像这样格式化输出:

Current ranking is 1,
I will decrement all bugs with higher ranking by one
Not shifting any bug, since there is another one with ranking
Bug 111 removed from ranking
Bug 111 inserted into ranking at position 1

如何将这些新行添加到 HTML 页面?

用 Perl 中的反引号运算符替换 system 调用以将输出捕获到变量中,然后在打印前修改输出:

if ($cmd eq 'set'){
         $_ = `ranking set $bug_id $rank`;
         $? == 0
           or die "command 'ranking set $bug_id $rank' failed: $?";
         s/$/<br>/mg;
         print;
 }

需要 s/ 上的 /m,以便将文本视为 m 多行($ 匹配 \n)。 /g 表示 "do all occurrences"(所有行)。

也许这对你有用。 (注意:未经测试。)