For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > html template and tables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author html template and tables
Pat Rice

2007-08-30, 7:26 pm

Hi all
I'm trying to do the following:
Print out the table from an array using HTML teplates.

I'm not sure of the sintax of how to get this table to display, I dont like
posting code here, but I'm kinda stuck and I dont know if I am going in the
right direction to do what I want to do.

my main problem is with this area of the code, and what to supply to
it................ if anyone has any good links please let me know.

# 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);


Thanks in advance






code is below:


________________________________________
________________________________________
_________
#!/usr/bin/perl

use strict;
use warnings;

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 $!";
my @array_of_data = <DATA>;

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;





foreach my $line (@array_of_data)
{
# 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);



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

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

} # End the if condition here.

} # End the foreach loop here.

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



############################ template section
use HTML::Template;
# 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);

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

Chas Owens

2007-08-31, 7:27 pm

On 8/30/07, Pat Rice <patrick.j.rice@gmail.com> 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>
Uri Guttman

2007-08-31, 7:27 pm

>>>>> "CO" == Chas Owens <chas.owens@gmail.com> writes:
[color=darkred]

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

don't say that unless you back it with reasons. slurping has its uses
and can be much faster with file::slurp than line by line. and there are
processing techniques you can do with slurped data that are much harder
when doing line by line.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com