Thread: [htmltmpl] HTML::Template Loop Issue
Brought to you by:
samtregar
From: Shawn S. <sha...@gm...> - 2010-04-08 21:20:43
|
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. |
From: Lyle <web...@co...> - 2010-04-08 21:55:32
|
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); } |
From: Roger B. W. <ro...@fi...> - 2010-04-08 22:13:30
|
On Thu, Apr 08, 2010 at 05:20:36PM -0400, Shawn Scott wrote: >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. 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 |
From: Alex T. <al...@ac...> - 2010-04-08 22:44:50
|
On Thu, 8 Apr 2010 17:20:36 -0400, Shawn Scott wrote > 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; 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 |
From: Sam T. <sa...@tr...> - 2010-04-08 23:41:13
|
Put the 'my %error_data;' inside the loop. You're pushing a reference to the same hash over and over again. -sam On Thu, Apr 8, 2010 at 5:20 PM, Shawn Scott <sha...@gm...> wrote: > 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. > > > > > > > > ------------------------------------------------------------------------------ > 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 > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > > |
From: Shawn S. <sha...@gm...> - 2010-04-09 00:32:32
|
Wow, embarrassing I missed that? At any rate thanks for all the quick replies. On Thu, Apr 8, 2010 at 5:30 PM, Lyle <web...@co...> wrote: > 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); > } > > > -- To argue with a person who has renounced the use of reason is like administering medicine to the dead. Thomas Paine |