http://www.perlmonks.org?node_id=160122

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

I am using HTML::Template with great success, but want to iterate through a huge number of template values as part of a form submission checker. Assigning the template variable values is lengthy and repetitive using the only method I know how. I have the template variables names and their required values in a hash, so wouldn't it be nice if I could just iterate through that and churn out the template variables?

So, is there a way to use a variable for all or part of the left hand side of the value pair assigmnent? Or another way to do the same thing?
# how I do it now $tmpl->param(flag_location => "foo $var bar"); # I have not used hashes here to make it easier # to see the two things I am trying to do. # how I want to do it my $param = 'flag_location'; $tmpl->param($param => "foo $var bar"); # or better still my $param = 'location'; $tmpl->param("flag_$param" => "foo $var bar");
Also, what is the correct name for the values either side of the '=>' so I can refer to these things correctly in the future?

Evidently I'm a little confused as to the exact usage of this type of assignment and it's limitations, and can't find any good explanations of it anywhere thus far.

Replies are listed 'Best First'.
Re: Value Pair Assignment
by Biker (Priest) on Apr 18, 2002 at 07:38 UTC

    This works:

    use strict; my %hash; my $foo='xxx'; my $bar='yyy'; $hash{$foo}='foo-value'; $hash{$bar}='bar-value'; my($k,$v); while(($k,$v)=each %hash) { print("$k => $v\n"); }


    Everything went worng, just as foreseen.

Re: Value Pair Assignment
by Fletch (Bishop) on Apr 18, 2002 at 07:34 UTC

    => isn't an assignment. It's just a fancy comma that happens to quote its left hand side if its a bareword (unquoted string with no spaces).

      Thanks!
      $tmpl->param($param, "foo $var bar");
      Does it nicely.
Re: Value Pair Assignment
by Biker (Priest) on Apr 18, 2002 at 07:29 UTC

    "what is the correct name for the values either side of the '=>' so I can refer to these things correctly in the future?"

    key => value


    Everything went worng, just as foreseen.

Re: Value Pair Assignment
by mce (Curate) on Apr 18, 2002 at 08:01 UTC
    Hi Ryan,

    Just call me stupid, but I don't see what the problem is :-).
    HTML::Template::param is a subroutine which takes two arguments, a key and a value. Of course you can loop over a hash and change the keys or values as arguments of the param sub.

    This has nothing to do with HTML::Template as such, but it works for every sub or command (like print, etc...).

    Perhaps post some actual code so I can understand your problem.


    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

      param does not take two arguments. It takes a list of key/value pairs. Read the documentation next time.

      $tmp->param( foo => 'bar', baz => 'quux' ); # or $tmp->param(%pairs);
      I didn't realise HTML::Template::param took two arguments

      firstly) because I was not aware of the functionality of '=>' and that it was actually passing two parameters and not one. secondly) because every example I have ever seen that taught me to use it was of the form 'variable => value'.

      Simple problem, simple solution - All in a day's learning.