http://qs321.pair.com?node_id=1215300


in reply to INSERT file contents inside a table

I always recommend not trying to print out html in the middle of perl code. It looks messy is a nightmare to maintain. Much better to break the html out into a template. If you want to keep it as a single script you can have the template in the cgi it still provides a level of separation. In my opinion something like this is much easier to read and maintain.

#!/usr/bin/perl use CGI ':standard'; use strict; use warnings; use CGI::Carp 'fatalsToBrowser'; # use only for testing use Template; my $cgi = new CGI; $cgi->header( -charset => 'utf-8' ); $ENV{"PATH"} = "/usr/sbin:/usr/bin:/sbin:/bin"; my %data; while(<DATA>) { chomp; my @columns = split; push @{$data{rows}}, \@columns } close FILE; my $tt = new Template; my $template = q( <!DOCTYPE html> <html> <head> <title>PM 1215287</title> </head> <body onload="parent.adjustMyFrameHeight();"> <div id='wrapper'> <table> <thead> <tr> <th>Time</th> <th>Category</th> <th>Sender</th> <th>Recipients</th> <th>Quarantined-ID</th> </tr> </thead> <tbody> [% FOR row=rows %] <tr> <td>[% row.0 %] [% row.1 %] [% row.2 %]</t +d> <td>[% row.3 %]</td> <td>[% row.4 %]</td> <td>[% row.5 %]</td> <td>[% row.6 %]</td> </tr> [% END %] </tbody> </table> </div> <style> body { background-color : #95B8DB; } #wrapper { min-height:500px; display:block; } table, tr, th, td { border : 1px solid grey; } </style> </body> </html> ); $tt->process( \$template, \%data ) or die $tt->error; __DATA__ May 3 15:58:17 SPAM sender@domain.com peter.w@recipient.com spam-Lub +_LynRmcvj.gz May 3 15:58:24 SPAM sender@domain1.com coev074@recipient1.com spam-g +SCAdAuMpiD0.gz May 3 15:59:27 BANNED sender@domain2.com nadia.meli@grecipient2.com +banned-BpF8aM7FM48B May 3 16:00:29 SPAM sender@domain.com sascha_ste@recipient.com spam- +6fFrrzUjgvug.gz May 3 16:01:48 INFECTED sender@domain3.com philipp_me@recipient.com +virus-SZLvi-VO4IIv May 4 16:02:28 SPAM sender@domain.com caro.grill@recipient3.com spam +-saYzvikxOfVL.gz May 4 16:02:45 SPAM sender@domain.com pellitter@recipient.com spam-8 +K649OHNlRQ2.gz May 4 16:03:14 SPAM sender@domain1.com anton.d@recipient.com spam-3r +pUGn3IcDnG.gz