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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.