Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How to format cgi output?

by iwanthome (Beadle)
on Apr 06, 2004 at 02:22 UTC ( [id://342810]=perlquestion: print w/replies, xml ) Need Help??

iwanthome has asked for the wisdom of the Perl Monks concerning the following question:

My code is like this:

print "Content-type: text/html\n\n"; print("-----router ip--------sip----------dip-----------sif------dif-- +-----proto-----sport------dport----pkts--size<br>"); foreach( @sort_result ) { #print; @tmpdata = split(/\|/,$_); my $tmp_string = sprintf("%15s%15s%15s%4s%4s%2s%6d%6d%10d%10d",$tmpdata[0],$tmpdata[1 +],$tmpdata[2],$tmpdata[3],$tmpdata[4], $tmpdata[5],$tmpdata[ +6],$tmpdata[7], $flows{$_}{pkts},$flo +ws{$_}{size}); print $tmp_string,"<br>"; }

After cgi executed,result is

-----router ip--------sip----------dip-----------proto-----sport------ +dport----pkts--size 10.1.64.233 10.1.64.36 99.1.64.255 1 017 137 137 1865 145956

but I hope it display like this:

-----router ip--------sip----------dip-----------proto-----sport------ +dport----pkts----size 10.1.64.233 10.1.64.36 99.1.64.255 017 137 +137 1865 145956

So how can I do this?

thanks!

Replies are listed 'Best First'.
Re: How to format cgi output?
by tachyon (Chancellor) on Apr 06, 2004 at 02:30 UTC

    HTML rendering ignores multiple whitespace and newlines and renders as a single space. You can either specify a Content-Type of text/plain or you can wrap your stuff in <PRE> tags which specify preformatted text ie fixed width, render the spaces as expected.

    Given that you have the power of HTML to hand you could just do a nice table.

    #!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; my @rows = join '', map{"<td>$_</td>"}('router ip', 'sip','dip','sif', +'dif','proto','sport','dport','pkts','size'); for (@sort_result) { my @tmpdata = split /\|/; push @rows, join '', map{"<td>$_</td>"} @tmpdata[0..7], $flows{$_} +{pkts}, $flows{$_}{size}; } @rows = map{ "<tr>$_</tr>\n" } @rows; print "<table border=1>\n" , @rows, "</table>";

    cheers

    tachyon

Re: How to format cgi output?
by blue_cowdawg (Monsignor) on Apr 06, 2004 at 02:48 UTC

    If you are outputing to a browser then as tachyon pointed out the multiple white space is going to get eaten during rendering. You would be better off using the intrinsic HTML methods of CGI.

    One such implementation looks something like this:

    use CGI qw(:all); print header; print start_html; print "<table>\n"; # yuck... I hate this... foreach my $row(@sorted_data){ # Don't know where @sorted data came fr +om... print tr(map { td($_) } split(/|/,$row) ); } print "</table>"; # end yuck print end_html;

    DISCLAIMER: this code is very untested and the author was very tired when he wrote it.
    Hopefully there is enough there to at least give you an idea of how to proceed. You can sort out the rest I'm sure.

Re: How to format cgi output?
by jdporter (Paladin) on Apr 06, 2004 at 03:35 UTC

    As the previous respondents have said, a html table may be a good solution.
    However, depending on your desired effect, you might be happy with simply wrapping the text in a <pre> tag pair.

    Without <pre> tags:

    10.1.64.233 10.1.64.36 99.1.64.255 1 15137 137 1865 145956 0

    With <pre> tags:

        10.1.64.233     10.1.64.36    99.1.64.255   1  15137   137  1865    145956         0
    

    (I've only shown the numbers, because your header line has some strange inconsistencies which I think you still need to work out.)

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

Re: How to format cgi output?
by nmcfarl (Pilgrim) on Apr 06, 2004 at 03:50 UTC
    Another option if you don't care about it being HTML is to change the first line to:
    print "Content-Type: text/plain\n\n";
    Then you'll end up with a text document, which the various browser will either download or display.
Re: How to format cgi output?
by davido (Cardinal) on Apr 06, 2004 at 04:18 UTC
    Just remember that your CGI script (usually) just outputs HTML. HTML stands for Hyper Text Markup Language. Everything you see being done with HTML (tables, frames, pre-formatted text, colors, font sizes, centered text, justified text, etc.) can all be done by having your CGI script output the HTML necessary.

    It's easy to get all caught up in the fact that your CGI script has to print the content, forgetting the fact that your content can still be "marked up" with HTML tags.

    So the fact is that your question is a HTML question. Because all that Perl has to do with it is that you're using Perl to output your HTML, and you just use simple print statements to do that. If you want Perl to output a centered header, you just say, "print "<center/><h2/>Header text!</h2></center>";"... pretty simple. The next trick is just learning HTML. I think that's a different website. ;)


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://342810]
Approved by AidanLee
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-18 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found