[Templates] Flattening a hash

Jason Gottshall jgottshall@capwiz.com
Fri, 17 Feb 2006 12:46:09 -0500


Bill, I responded to a similar post of yours back in November:

http://template-toolkit.org/pipermail/templates/2005-November/008042.htm
l

Does this address the same issue, or am I misunderstanding your current
issue?

Jason=20

Bill Moseley wrote:
> On Fri, Feb 17, 2006 at 08:23:00AM +0000, Andy Wardley wrote:
> > I suspect your options method is returning a list of=20
> hashes, rather than
> > a reference to a list of hashes.
> >=20
> >     return \@hashes;     # good
> >     return  @hashes;     # bad
> >=20
> > In the first case you should always get what you expect. =20
> In the second
> > case, TT will notice if you return multiple values (e.g.=20
> @hashes > 1)
> > and fold it into a list reference.  But if there's only one=20
> item then
> > TT will assume you meant to return that and won't fold it=20
> into a list.
>=20
> Ah!  It's not TT flattening, it's TT folding.
>=20
> I forgot that I'm using Rose::Object, and
> Rose::Object::MakeMethods::Generic says for its array methods it does:
>=20
>     Interfaces
>        "get_set"
>            If called with no arguments, returns the array contents in
>            list context or a reference to the actual array=20
> stored by the
>            object in scalar context.
>=20
> So I need to to force reading the object in scalar context.
>=20
> Oh, this is ugly:
>=20
>         x =3D field.options.size > 2 ? field.options : [=20
> field.options ];
>=20
>         FOR i =3D x;
>             "<option value=3D'$i.value'>";
>             i.label | html;
>             "</option>\n";
>         END;
>=20
> I suspect there's something better.