[Templates] FOREACH problem with wantarray and a single element result

Andy Wardley abw at wardley.org
Sat Jun 21 09:48:44 BST 2008


Michael Lackhoff wrote:
> I have a problem when iterating over the results of an object method 
> that returns its results dependant on wantarray.

Ah.  TT2 is broken in that regard, I'm afraid.  My bad.

There's a workaround for TT2 in the form of the scalar plugin.  That's
new to 2.20 (which isn't released yet), but you can grab it from SVN.

http://tt2.org/svnweb/Template2/view/trunk/lib/Template/Plugin/Scalar.pm

     [% USE Scalar %]

     # TT2 calls object methods in array context by default
     [% object.method %]

     # force it to use scalar context
     [% object.scalar.method %]

     # also works with subroutine references
     [% scalar.my_sub_ref %]

The problem is fixed properly for TT3.  All methods will be called in scalar
context by default.  The '@' sigil will be used to force list context when
you really want it.

     # TT3
     [% object.method %]     # scalar context by default

     [% object. at method %]    # force list context

HTH
A



More information about the templates mailing list