Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

HTML::Template Accessing Params and Reassigning

by Rodster001 (Pilgrim)
on Aug 17, 2009 at 22:20 UTC ( #789293=perlquestion: print w/ replies, xml ) Need Help??
Rodster001 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings!

I am assigning an array using an HTML::Template parameter (which I had set) like this:

my @array = $tmpl->param('SOME_LOOP');

Which works just fine. If I use Data::Dumper, the contents of the array looks just as I expect (an array of hashes).

But, if I try to reassign that variable (in the same way I originally had, even without doing anything to the array) like this:

$tmpl->param(SOME_LOOP => \@array);

I get this error:

HTML::Template->output() : fatal error in loop output : Not a HASH ref +erence at /usr/lib/perl5/site_perl/5.10.0/HTML/Template.pm line 3059.

Any ideas?

Comment on HTML::Template Accessing Params and Reassigning
Select or Download Code
Replies are listed 'Best First'.
Re: HTML::Template Accessing Params and Reassigning
by snoopy (Deacon) on Aug 18, 2009 at 00:13 UTC
    A concise working code example is always best!

    My guess is that you are seeing something like:

    #! /usr/bin/perl use warnings; use strict; use HTML::Template; use Data::Dumper; my $text = "<TMPL_LOOP foo>bar=<TMPL_VAR bar>\n</TMPL_LOOP>"; my $template = HTML::Template->new(scalarref => \$text, loop_context_vars => 1); my @a = ({bar => 10}, {bar => 20}); $template->param(foo => \@a); my @array = $template->param('foo'); print Dumper(@array); $template->param(foo => \@array); $template->output;
    $VAR1 = [ { 'bar' => 10 }, { 'bar' => 20 } ]; HTML::Template->output() : fatal error in loop output : Not a HASH ref +erence at /usr/share/perl5/HTML/Template.pm line 3059.
    HTML::Template's param method is returning a scalar reference, so by assigning the return value to an array, you end up with a single element array. That element being the array reference.

    The \@array expression then adds another level of indirection. You end up with an array reference to your one element array; the element being an array reference.

    You need to work with a returned array reference. Example follows:

    #! /usr/bin/perl use warnings; use strict; use HTML::Template; use Data::Dumper; my $text = "<TMPL_LOOP foo>bar=<TMPL_VAR bar>\n</TMPL_LOOP>"; my $template = HTML::Template->new(scalarref => \$text, loop_context_vars => 1); $template->param(foo => [{bar => 10}, {bar => 20}]); my $array_ref = $template->param('foo'); if ($array_ref) { print Dumper($array_ref); print Dumper($array_ref->[1]); # # add a new element # push (@$array_ref, {bar => 30}); $template->param(foo => $array_ref); } print $template->output;
    Also be aware of a potential trap with the one argument usage of param method. In the above example, if foo is deleted from the template, this method will returned undef rather than the array. I tend to avoid using it for this reason.

    Update: Added loop_context_vars => 1 to HTML::Template->new(...). Now getting exactly the same error message as the OP.

      HTML::Template's param method is returning a scalar reference, so by assigning the return value to an array, you end up with a single element array. That element being the array reference.

      Thanks Snoop! That was it... for some reason I was thinking that by assigning it to an array it would dereference it. But, looking at it again this morning I am not sure where my mind was :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://789293]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-07-29 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls