Search code examples
htmlperlcgi

How to add new lines to stdout in perl


I have this piece of code at the end of a .cgi-file:

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

You can input the data and it's interpreted as a system command. An example output on the HTML-page is the following:

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

(all in one line)

But I need to format the output like this:

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

How can I add those new lines to the HTML-page?


Solution

  • Replace the system call with the backtick operator in Perl to capture your output into a variable, then massage the output before printing:

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

    The /m on the s/ is needed so that the text is treated as multiple lines ($ matches at \n). The /g says "do all occurrences" (all lines).

    Maybe this will work for you. (Caveat: untested.)