Re: html template and tables



On 8/30/07, Pat Rice <patrick.j.rice@xxxxxxxxx> wrote:
snip
#!/usr/bin/perl

use strict;
use warnings;

Good form, keep doing this.


my $data_file = 'myfile/var/log/my.log';
print "$data_file \n";

# Open the file for reading.
open (DATA, "$data_file") or die "can't open $data_file $!";

use the three argument version of open with a lexical (instead of
global) file handle, and don't put scalars in quotes by themselves (it
is generally useless and confusing to people reading your code).

open my $fh, '<', $data_file
or die "could not open $data_file:$!";

my @array_of_data = <DATA>;

You almost never want to do this. Read the file handle line by line
with a while loop.


print "this is the file: \n";
#print @array_of_data;
print "\n";


#Variable dlecrations
my $line;
my $testIf;
my @date;
my @time;
my @IVM;
my @ID;

Don't do this. Variables in Perl should be declared in the tightest
possible scope and at their first usage.

foreach my $line (@array_of_data)
{

This should be

while (my $line = <$fh>) {

or better yet

while (<$fh>) {

since your first action is to split the line and assign the parts to variables.

# Start an if statement, the condition of which is
# "If this particular line contains the word dangerous."

######## chomping
#print "\n in foreach loop \n";
chomp($line);

Please indent consistently, you will write fewer bugs and make it
easier for other people to read your code.

if ( ($line =~ /TestString/i) && ($line =~ /TestString2/i) )
{

######Changing this to take this as an array
(@date, @time, @IVM, @ID) = split (' ', $line);

All of the scalars from the split are going to go into @data. There
can only be one array on the right hand side of an assignment, at it
must be the last variable (this is not strictly true, but the other
variables will get no data). Also, this is not the data structure
HTML::Template is looking for.


} # End the if condition here.

} # End the foreach loop here.

close (DATA);
###########################close the file



############################ template section
use HTML::Template;

While it is not wrong to use a module at this point, it is standard
practice to put all of the use statements at the top of the program
(they all execute at compile time anyway, so putting them deep in the
code doesn't save you any time).

# set up my table, and print it out
my $template = q{
<TABLE border=1><TMPL_LOOP name="table_data">
<TR><TMPL_LOOP name="date">
<TD><TMPL_VAR name="time"></TD>
</TMPL_LOOP></TR>
</TMPL_LOOP></TABLE>
};
#take it from the table and put pump it out on to the web
$template = HTML::Template->new(scalarref => \$template, option =>'value');
$template->param(table_data => \@table_data);

Where did @table_data come from? This would certainly trigger an
error under the strict pragma.


##print out html
print "Content-Type: text/html\n\n", $template->output;


Here is how your code should look:

#!/usr/bin/perl

use strict;
use warnings;

use HTML::Template;

my $data_file = shift;

# Open the file for reading.
open my $fh, '<', $data_file
or die "can't open $data_file:$!\n";

my @fields = qw<date time IVM ID>;
my @data;
while (<$fh>) {
chomp;
if (/TestString/i && /TestString2/i) {
my %row;
@row{@fields} = (split)[0 .. 3];
push @data, \%row;
}
}

# set up my table, and print it out
my $template = q{
<TABLE border=1>
<TR><TH>Date</TH><TH>time</TH><TH>IVM</TH><TH>ID</TH></TR>
<TMPL_LOOP name="table_data">
<TR>
<TD><TMPL_VAR name="date"></TD>
<TD><TMPL_VAR name="time"></TD>
<TD><TMPL_VAR name="IVM"></TD>
<TD><TMPL_VAR name="ID"></TD>
</TR>
</TMPL_LOOP>
</TABLE>
};
#take it from the table and put pump it out on to the web
$template = HTML::Template->new(scalarref => \$template);
$template->param(table_data => \@data);

##print out html
print "Content-Type: text/html\n\n", $template->output;


Given the data

2007-01-01 00:00 IVM1 foo TestString TestString2
2007-01-01 01:00 IVM2 foo TestString TestString2
2007-01-01 01:00 IVM2 bar TestString TestString2
2007-01-01 03:00 IVM1 foo This row won't print

it prints

Content-Type: text/html


<TABLE border=1>
<TR><TH>Date</TH><TH>time</TH><TH>IVM</TH><TH>ID</TH></TR>

<TR>
<TD>2007-01-01</TD>
<TD>00:00</TD>
<TD>IVM1</TD>
<TD>foo</TD>
</TR>

<TR>
<TD>2007-01-01</TD>
<TD>01:00</TD>
<TD>IVM2</TD>
<TD>foo</TD>
</TR>

<TR>
<TD>2007-01-01</TD>
<TD>01:00</TD>
<TD>IVM2</TD>
<TD>bar</TD>
</TR>

</TABLE>
.



Relevant Pages