Jim Gibson wrote:
On 7/10/09 Fri Jul 10, 2009 2:25 PM, "Shawn H. Corey"
<shawnhco...@gmail.com> scribbled:
On Fri, 2009-07-10 at 15:19 -0600, Scott wrote:
Hello,
Here is my code on the perl side:
my @wall_data = ();
while(my $wallref = $wallpostquery->fetchrow_hashref())
{
my %walldata;
$walldata{WALL_SUBJECT} = $wallref{'SUBJECT'};
$walldata{WALL_DATE} = $wallref{'DATE'};
$walldata{WALL_POSTID} = $wallref{'POSTID'};
$walldata{WALL_MESSAGE} = $wallref{'MESSAGE'};
push (@wall_data, \%walldata);
# The above pushes the address of hash on the array, over and over
again.
Which works because %walldata is localized to the while loop and is
re-allocated each time through the loop. Each loop iteration creates a new
hash variable, and the array @wall_data contains a list of distinct hash
references.
push @wall_data, { %walldata };
# This code pushes an anonymous hash containing the data on to the
array.
Which also works, because a copy of the hash is saved, but it is not as
efficient.
In other words, the problem lies elsewhere. As far as I can tell the code is
fine as shown, but I don't have any experience with HTML::Template.
I found the error, seems to not be putting the stuff into the hash right
or something. I found a fix so thought i should put it on the list.
Here is an example i found after digging around on google:
<!-- TMPL_LOOP NAME=ROWS -->
<tr>
<td><!-- TMPL_VAR NAME=TITLE --></td>
<td><!-- TMPL_VAR NAME=ARTIST --></td>
<td><!-- TMPL_VAR NAME=ALBUM --></td>
<td><!-- TMPL_VAR NAME=YEAR --></td>
</tr>
<!-- /TMPL_LOOP -->
</table>
</body>
</html>
# songs.cgi
use DBI;
use CGI;
use HTML::Template;
use strict;
my $DBH = DBI->connect(
qw(DBI:vendor:database:host user pass),
{ RaiseError => 1}
);
my $CGI = CGI->new();
# grab the stuff from the database
my $sth = $DBH->prepare('
select title, artist, album, year
from songs
');
$sth->execute();
# prepare a data structure for HTML::Template
my $rows;
push @{$rows}, $_ while $_ = $sth->fetchrow_hashref();
# instantiate substitute the values
$template->param(ROWS => $rows);
print $CGI->header();
print $template->output();
$DBH->disconnect();
Thanks again for everyone helping me on this.
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/