WHEW!  I just bonked my head a few times over this, and got chunks of help
here and there from the #perl IRC channel.

Say you have a hash you're going to send to the award-winning "Template"
Perl module.  Say this hash is based on "token=value" pairs coming in
from STDIN.

Finally, say your template uses some of Template's "FOREACH" directive
so that it can act upon arrays?

Well!  Here's how you fill in a particular chunk of your hash with a
little hash in a piece of your array:

    ${$hash{$array}}[$index] { $label } = $value;


$hash is the thing being sent to Template's "process" method.
(Most of the items in this hash are simply things like {"Name" => "Bill"} )

$array is the label of a thing to iterate on within your template.
(It's the thing you mention in your "FOREACH" directive)

$index is a number saying _where_ in the array something is to go.
(That way you can have a number of array items and keep their hashes
stuck together)

$label is the name part of the hash item in the array $array.

And finally, $value is the value to store.


WHEW!!!  Complicated, huh?


But, it lets me do stuff like:


item1.code=00001
item1.name=Hot Cocoa Mix
item1.price=$0.59
item1.qty=1

item2.code=00345
item2.name=Milk
item2.price=$1.00
item2.qty=1


And then in my template do something like:

[% FOREACH i = item %]
Code = [% i.code %]
Name = [% i.name %]
Price = [% i.price %]
Qty = [% i.qty %]
[% END %]

:)

-bill!
overly proud, and starving for lunch

Reply via email to