tags:

views:

127

answers:

4

I am trying to write a simple Perl CGI script that:

  • runs a CLI script
  • reads the resulting .out file and converts the data in the file to an HTML table.

Here is some sample data from the .out file:

 10.255.202.1   2472327594  1720341
 10.255.202.21   2161941840  1484352
  10.255.200.0   1642646268  1163742
 10.255.200.96   1489876452  1023546
 10.255.200.26   1289738466   927513
 10.255.202.18   1028316222   706959
 10.255.200.36    955477836   703926

Any help would be much appreciated.

A: 

This doesn't directly answer your question, but is it possible to use AWK instead? It shouldn't be too difficult to wrap the whole content, then each column entry with the appropriate html tags to create a basic table.

Ryan P.
Yes, it is possible to use any Turing complete language to accomplish this task. Though IMHO this sort of text processing task is right in Perl's wheelhouse.
mobrule
+1  A: 

The following is untested and probably needs a lot of polishing but it gives a rough idea:

use CGI qw/:standard *table/;

print
  start_html('clicommand results'),
  start_table;

open(my $csvh, 'clicommand |');

while (<$csvh>) {
   print Tr(map { td($_) } split);
}

close($csvh);

print
  end_table,
  end_html;  
reinierpost
As a general rule, you should encode any HTML metacharacters before outputting a string (e.g. `<` -> `<`). Try HTML::Entities::encode_entities.
rjh
Great, thanks, I'll try that out. And no, it's not homework, just a little project I am working on and I really don't know Perl :)
A: 

try to put that text file into a twod array.
and from that twod array you can create an html table.
a simplification can be achieved by breaking the text into its lines.

-put the file into a variable.
-get each line within the text into a single element of the array.*1
-for each of the elements in that array, break the content into the columns.*2
at that you have a twod array representing a table.
then you just go through the array and print what the html at each location.*3

###########################################

*1 my @the_table_lines = map { /\Z/gi } $the_whole_text;  #or something like that.
*2 my @columns_of_current_line = map { /\Z/gi } $url_content; #or something like that.
*3 like when going down a line in the array you must print "" etc.


Hermann Ingjaldsson
A: 

You'll very likely want to make the HTML prettier by using a CSS stylesheet or adding borders to the table, but here's a simple start.

#!/usr/bin/perl

use strict;
use warnings;

my $output = `cat dat`;
my @lines  = split /\n/, $output;

my @data;
foreach my $line (@lines) {
    chomp $line;
    my @d = split /\s+/, $line;
    push @data, \@d;
}

print <<HEADER;
<html>
<table>
HEADER

foreach my $d (@data) {
    print "\t", "<tr>";
    print map { "<td>$_</td>" } @$d;
    print "</tr>", "\n";
}

print <<FOOTER;
</table>
</html>
FOOTER

This makes the following output:

<html>
<table>
        <tr><td>10.255.202.1</td><td>2472327594</td><td>1720341</td></tr>
        <tr><td>10.255.202.21</td><td>2161941840</td><td>1484352</td></tr>
        <tr><td>10.255.200.0</td><td>1642646268</td><td>1163742</td></tr>
        <tr><td>10.255.200.96</td><td>1489876452</td><td>1023546</td></tr>
        <tr><td>10.255.200.26</td><td>1289738466</td><td>927513</td></tr>
        <tr><td>10.255.202.18</td><td>1028316222</td><td>706959</td></tr>
        <tr><td>10.255.200.36</td><td>955477836</td><td>703926</td></tr>
</table>
</html>

To understand how to modify the look of your HTML tables, the w3schools website entry on the table tag is a good start.

James Thompson
I don't really understand the downvote for working code.
James Thompson