user466663
user466663

Reputation: 845

How to display contents of a file with formatting preserved on the browser using Perl CGI

I have a text file saved in the server side. I have to read the file and display its content to the browser using perl CGI. The file has several paragraphs, newline and tab characters. I want to preserve those formatting. Currently the contents of the file is displayed, but it is all jumbled up as a single continuous paragraph.

print "<table border='1'>\n";
    print "<tr>\n";
    print "<td><b>Whole Report</b></td>\n";
    print "</tr>\n";
    print "<tr>\n";
    print "<td>$entireReport</td>\n";
    print "</tr>\n";
    print "</table>\n";

In the above code, I am reading the entire content of the file into the variable $entireReport.

Thanks

Upvotes: 0

Views: 1033

Answers (1)

dmg
dmg

Reputation: 4491

wrap the code in a <PRE></PRE> tag. Say you have the contents in $fileContents then use:

print "<td><pre>", CGI::escapeHTML($fileContents), "</pre></td>\n";

This will keep the formatting in the file verbatim.

--dmg

Upvotes: 1

Related Questions