[htmltmpl] (newbie) How to populate an array of hash references one by one?
Brought to you by:
samtregar
From: Ken C. <ke...@ke...> - 2003-11-24 19:47:38
|
Hi All, I'm playing with HTML::Template, and I've run into something that should be easy, but I don't see how it can work. The docs say to pass many elements for a loop, you do this: $template->param( loopvar => [ { name=>"foo",value=>"baloney" }, { name=>"bar",value=>"tiple" }, { name=>"blah",value=>"mulroney" }, ], ); So, I'm using anonymous array and hash operators to make an anonymous array of hash references. (page 246, "The anonymous array composer", camel book). That's great, but what if you don't know your content at programming time? {Just for context, I'm using perl 5.6.1, HTML::Template 2.6, with "use strict"}. Ideally, I want to add a line at a time to that array, and then pass that whole array to the param. So, I tried various things: # attempt 1 my @test = (); push @test { name=>"foo",value=>"blitz" }; # Gets "Global symbol "%test" requires explicit package name at ...[push line]" !?!?! there is no %test. # attempt 2 my @test = (); push @test \{ name=>"foo",value=>"blitz" }; # Gets "Backslash found where operator expected at ...[push line]" # attempt 3 my @test = (); push @test [ { name=>"foo",value=>"blitz" } ]; # Gets "Type of arg 1 to push must be array (not array slice) at ...[push line]" [more stuff along the same lines that didn't work...] *grrr* Okay, says I, I'll work on something else and wait for the muse to strike. I decide I'll figure out how to pass an array into a param... # attempt 4 my @test = [ { a=>"b",c=>"d" }, { e=>"f",g=>"h" }, ]; $template->param(itemloop => \@test); # Gets "HTML::Template->output() : fatal error in loop output : HTML::Template->param() : Single reference arg to param() must be a has-ref! You gave me a ARRAY. at ..." # attempt 5 my %test; $test{"a"}="b"; $test{"c"}="d"; $test{"e"}="f"; $template->param(itemloop => \%test); # Gets "HTML::Template::param() : attempt to set parameter 'itemloop' with a scalar - parameter is not a TMPL_VAR! at ..." # attempt 6 my %test; $test{"a"}="b"; $test{"c"}="d"; $test{"e"}="f"; $template->param(itemloop => %test); # Gets "You gave me an odd number of parameters to param()! at ..." Ach. So, no better luck. Clearly there's some simple skullduggery I'm missing here. Can anyone enlighten me? Pointers to documentation would be fine, though the HTML::Template page and the perldoc for HTML::Template didn't help too much already. -Ken |