in reply to how to pass hash to perl template toolkit

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>

Replies are listed 'Best First'.
Re^2: how to pass hash to perl template toolkit
by nikmit (Sexton) on Nov 28, 2015 at 09:12 UTC
    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?