Discussion:
[htmltmpl] HTML::Template Loop Issue
Shawn Scott
2010-04-08 21:20:36 UTC
Permalink
Good Afternoon,

I have been using HTML::Template for some time and have recently run into an
starnge issue. I have the following code

my @error = @_;
my @error_loop = ();
my %error_data;
print header;
foreach (@error) {
$error_data{ERROR_DATA} = $_;
push(@error_loop, \%error_data);
}
my $template = HTML::Template->new(filename =>
"$html_root/signup_error.html");
$template->param(ERROR_LOOP => \@error_loop);
print $template->output;

When I run this code it gives me the correct number of elements, based on
the number of errors in the loop @error array. The only issue each one is
shown as the last line pushed to the @error_loop. If I change the code to
this

foreach (@error) {
print p"DEBUG - $_\n";
$error_data{ERROR_DATA} = $_;
push(@error_loop, \%error_data);
}
I get the correct vaules printing out in the debug print line but then the
actual template loop variables are wrong.

Any help would be great.
Lyle
2010-04-08 21:54:57 UTC
Permalink
Problem with your hash scoping:-

my @error = @_; my @error_loop = ();
print header;
foreach (@error) {
my %error_data;
$error_data{ERROR_DATA} = $_;
push(@error_loop, \%error_data);
}
Roger Burton West
2010-04-08 21:55:48 UTC
Permalink
Post by Shawn Scott
When I run this code it gives me the correct number of elements, based on
Well, yes. You're defining a single hash and pushing multiple references
to it. Chech out perldoc perlref...

For this specific problem, cut out the middleman:

my @error = @_;
my @error_loop = ();
print header;
foreach (@error) {
push(@error_loop, {ERROR_DATA => $_});
}
my $template = HTML::Template->new(filename => "$html_root/signup_error.html");
$template->param(ERROR_LOOP => \@error_loop);
print $template->output;

Roger
Alex Teslik
2010-04-08 22:22:30 UTC
Permalink
Post by Shawn Scott
Good Afternoon,
I have been using HTML::Template for some time and have recently run
into an starnge issue. I have the following code
my %error_data;
print header;
$error_data{ERROR_DATA} = $_;
}
my $template = HTML::Template->new(filename =>
"$html_root/signup_error.html");
print $template->output;
Each entry in your @error_loop is looking by reference at the %error_data hash
key ERROR_DATA. So you are not storing the actual data in @error_loop, just a
reference to the ERROR_DATA hash key in %error_data. That hash key takes the
last value of the @error_loop during the foreach - and then all the references
pointing to that key look the same.

You are using a lot of variables, sigils, metacharacters, and capitalization
that you don't need. It makes the code hard to read. It could be cleaner:

my @errors = @_;
my $template = HTML::Template->new(filename => "errors.html");
$template->param (
errorloop => [
map { { error => $_ } } @errors
],
);
print header();
print $template->output;

untested.

HTH,
Alex
Sam Tregar
2010-04-08 21:53:16 UTC
Permalink
Put the 'my %error_data;' inside the loop. You're pushing a reference to
the same hash over and over again.

-sam
Post by Shawn Scott
Good Afternoon,
I have been using HTML::Template for some time and have recently run into
an starnge issue. I have the following code
my %error_data;
print header;
$error_data{ERROR_DATA} = $_;
}
my $template = HTML::Template->new(filename =>
"$html_root/signup_error.html");
print $template->output;
When I run this code it gives me the correct number of elements, based on
this
print p"DEBUG - $_\n";
$error_data{ERROR_DATA} = $_;
}
I get the correct vaules printing out in the debug print line but then the
actual template loop variables are wrong.
Any help would be great.
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Html-template-users mailing list
https://lists.sourceforge.net/lists/listinfo/html-template-users
Shawn Scott
2010-04-09 00:32:20 UTC
Permalink
Wow, embarrassing I missed that? At any rate thanks for all the quick
replies.
Post by Lyle
Problem with your hash scoping:-
print header;
my %error_data;
$error_data{ERROR_DATA} = $_;
}
--
To argue with a person who has renounced the use of reason is like
administering medicine to the dead.

Thomas Paine
Loading...