mike wrote:
 > however, it appears that TT3 has not been touched in over two
 > years.

Hi Mike,

Actually, it's more the case the the TT3 web site hasn't been touched
in over two years.  I _have_ been touching TT3 in that time (honest!)
although not as much as I would have liked due to commitments to various
projects for other people.

However, my work schedule is now relatively clear so I'm doing a fair
bit of TT3 touching right now.  I'm hoping this will be the last burst
to get it out the door in the next few months.

Opening up the new TT3 subversion repository and updating the web site
are high on my TODO list, just as soon as I've got them in a sensible
state.

 > rather than wait for TT3, i am proffering the attached
 > unintrusive patch to satiate those (such as myself) whom are suffering
 > from moderate to severe insanity arising from the use of template
 > toolkit and code that uses wantarray.

Count me among those people.  It's probably of little consolation, but
I kick myself hard every time I get bitten by it.  Consider me suitably
punished.

 > *please* apply this to TT2.

Well seeing as you asked so nicely...

 > it's a relatively simple bolt-on enhancement.
 > it passes all the tests.  it doesn't severely alter the
 > existing grammar.  it doesn't cause people muscle fatigue,
 > gastroesophageal reflux disease, or lymphoma.  it allows people out
 > there working with template toolkit and DBIx::Class (in addition to
 > other wantarray-loving code) to severely reduce their stress levels and
 > become more productive members of their development teams.

You present a compelling case.  :-)

The patch itself is fine and I think it addresses a problem that definitely
needs to be solved.  As you mention, I've been promising better support for
this in TT3, although until today I hadn't quite figured out how that was
going to manifest itself (apart from making scalar context the default, but
then we've just inverted the problem).

The main issue that I can see with with the SCALAR directive is that it only
addresses part of the problem.  For example, you still can't do things like
this:

   [% FOREACH track IN album.tracks %]

where you want the tracks() method called in scalar context against the
album object.

So I sent a few hours today thinking hard about this and came up with a
simple and elegant solution (if I may say so myself) that appears to
implement everything we need from a plugin.

Like this:

   [% USE scalar %]
   [% object.scalar.method %]     # calls object.method in scalar context

The scalar plugin defines a .scalar vmethod for hashes and lists (so
that blessed list objects can use it too).  The .scalar dotop returns an
object (a "monad" in functional programming lingo, if my understanding of
the terminology is correct) which "wraps" the original object on its left.
The monad object has an AUTOLOAD method which simply delegates the .method
call to the original object, calling it in a scalar context, of course.

The nice thing about implementing it as a virtual method is that it works
everywhere:

   [% tracks = album.scalar.tracks %]          # assignments
   [% FOREACH track IN album.scalar.tracks %]  # loops
   [% IF album.scalar.tracks.size %]           # conditionals
   ...etc...

It also Just Works[tm] with variables bound to subroutines, like this:

   [% USE scalar %]
   [% scalar.foo %]               # foo() in scalar context

...although this is implemented slightly differently to the vmethods.
In this case, the scalar plugin (in the 'scalar' variable) has an AUTOLOAD
method which received the .foo call, fetches the foo subroutine from the
stash, then calls it in scalar context.

I've checked the plugin into the repository:

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

There some basic tests here:

   http://tt2.org/svnweb/Template2/view/trunk/t/scalar.t

Have a play and see what you think.  If we're happy that this adequately
solves the problem then I'll tidy it up, add some documentation and include
it in the next release.

Thank you for the patch all the same.  I appreciate the time and effort you
spent on it, even if we don't actually end up using it.  If nothing else,
it forced me to address the underlying problem and (hopefully) fix it properly.

Cheers
A

_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to