I am working on a Perl script, but I am having an issue I can’t really overcome. Here is my code:
my @rowses = ();
while ( @list = $sth->fetchrow_array())
{
%row = ();
if($list[30] == 1)
%row = (
cod_cliente => $list[1],
rag_soc => $list[2],
p_iva => $list[11],
IDanagrafica => $list[0],
tabella => $tab,
IDanagraficaE => $list[0],
tabellaE => $tab,
checkbox => "checked",
);
$LOL = \%row;
print $cgi->p($LOL);
}
else
{
%row = (
cod_cliente => $list[1],
rag_soc => $list[2],
p_iva => $list[11],
IDanagrafica => $list[0],
tabella => $tab,
IDanagraficaE => $list[0],
tabellaE => $tab,
checkbox => "",
);
$LOL = \%row;
print $cgi->p($LOL);
}
push (@rowses, \%row);
}
$template->param(table => \@rowses);
$template->param(tab => $tab);
When I try to print, for debugging, the reference to a row ($LOL), it prints nothing, and when I print the reference at @rowses, it is an array full of all the same hash, the last one the fetched by from the statement.
The weird is, if I print a hash row per time, without referencing it, it prints them well, and all of them.
I am doing that for passing the array reference, containing all the hashes, to a TMPL_LOOP, and print them; but it print a long list of only the last row fetched.
Thanks in advance to everyone who will help me.
Your
%rowis the same variable for each iteration of the while loop. You store just the reference to it in@rowses, which means if you change%row, all the references point to the changed hash. You should define a new%rowfor each iteration of the loop, e.g. by usingindead of
Why $LOL is not printed: If the first argument to
pis a hash reference, it is interpreted as the attributes of the<p>.