srins has asked for the wisdom of the Perl Monks concerning the following question:

hi thanks for your suggestion i tried to pass the hash of hashes i constructed
as $template->process($file,%add) || die $template->error(); for my hash my %add=( hsh1=>{ name=>"name1 ", type =>"text", default=>" ", value=>[" "], entries=>"M" }, hsh2=>{ name=>"name2 ", type=>"dropdown", req=>"prc", default=>" ", value=>["value1","value2","value3"], entries=>" " } ); for your code [% FOREACH element = add.keys %] [% IF add.$element.type == "text" %] <input type="text" name="[% add.$element.name %]" value="[% add.$e +lement.default %]"> [% ELSIF add.$element.type == "dropdown" %] <select name="[% add.$element.name %]"> [% FOREACH option = add.$element.value %] <option value="[% option %]"> [% option %] </option> [% END %] </select> [% END %] <br> [% # put a line break between the form elements %] [% END %]

but i get following error, Content-type: text/html Can't use string ("hsh2") as a HASH ref while "strict refs" in use at /usr/cisco/packages/perl/perl-5.8.6/lib/site_perl/5.8.6/sun4-solaris/Template/Service.pm line 79.
i also tried to pass the reference as $template->process($file,\%add) || die $template->error();
but nothing is substituted in the html file.can u help me how to pass my hash to the html file or i need to change the above code.please help me in this regard.
Thanks, srins.

Replies are listed 'Best First'.
Re: how to pass hash to perl template toolkit
by johnnywang (Priest) on Nov 05, 2005 at 20:48 UTC
    you're missing one more indirection, your template will see the name "add" if you pass it in as {'add'=>\%foo}. The following runs fine and does the right thing:
    use strict; use Template; my %add=( hsh1=>{ name=>"name1 ", type =>"text", default=>" ", value=>[" "], entries=>"M" }, hsh2=>{ name=>"name2 ", type=>"dropdown", req=>"prc", default=>" ", value=>["value1","value2","value3"], entries=>" " } ); my $template = Template->new(); $template->process(\*DATA,{'add'=>\%add},\*STDOUT) || die $template->error(); __DATA__ [% FOREACH element = add.keys %] [% IF add.$element.type == "text" %] <input type="text" name="[% add.$element.name %]" value="[% add.$e +lement.default %]"> [% ELSIF add.$element.type == "dropdown" %] <select name="[% add.$element.name %]"> [% FOREACH option = add.$element.value %] <option value="[% option %]"> [% option %] </option> [% END %] </select> [% END %] <br> [% # put a line break between the form elements %] [% END %]
    __OUTPUT__ <select name="name2 "> <option value="value1"> value1 </option> <option value="value2"> value2 </option> <option value="value3"> value3 </option> </select> <br> <input type="text" name="name1 " value=" "> <br>
      Ten years after the reply by johnnywang, I read it and found it rather useful... Prepending the variable name in add.$element.type with the dollar sign makes it work - in this piece of code and in mine. Yet according to the documentation it is ignored unless INTERPOLATE=1 is set, and the colour coding from vim-perl suggests the same.

      I doubt this is that badly documented, what am I missing? Why is the '$' sign vital in there when it (seemingly) shouldn't be?