Friday, February 20, 2009

Template Toolkit virtual methods with less mess

Template Toolkit has a very cool feature called Virtual Methods. It provide methods on native Perl5 data types.

An example is this (In TT syntax):
[% listy = [ 47, -47, 42, -55, 4, -24 ] %]
[% listy.size %]
[% listy.join(', ') %]
Often its called autoboxing. Well TT allows you to define your own. The details are available in the TT VMethod Manual. In that page there is a TODO about a method called define_vmethod.

Since I like to use TT from CGI::Application and I don't like to actually see its guts. I choose to find a way to add a vmethod without the unpretty $Template::Stash::LIST_OPS->{} syntax. I think that is quite unfriendly to TT noobs like most of my coworkers.

I found that define_vmethod is available off the TT Context object. This is available with $tt->context or in my case off plugin TT object in CGI App $self->tt_obj->context.

The parameters are context->define_vmethod( $type, $name, \&implementation )
  • $type - data type, 'scalar', 'list', 'array' or 'hash'
  • $name - the name for you method 'abs', 'smash', 'love'
  • \&implementation - sub reference that does the method
Here is my case, where I really wanted to do:
[% IF indexes.$rate.value.abs > 10 %]

Here is the implementation:
## inside my CGI::App run mode
sub qa {
my $self = shift; ## grab cgi::app object

$self->tt_obj->context->define_vmethod( 'scalar', 'abs',
sub { return abs( shift ) } );
The parameters passed to the sub reference depend upon the datatype. But after the data type value then additional parameters are passed, so I could have added parameters to the abs call. E.g.
[% indexes.$name.value.abs(10) %]
In that case the parameters would have been: my @_ = ( $list_value, 10 );

First parameter for each type:
  • scalar - value
  • list||array - reference to list
  • hash - reference to hash
In another post, I'll write an example of either or both of those.


  1. Thank you for sharing this information.
    We are facing constant growing of data amount in both the Web and personal computers. And the more information we need to proceed the more time it takes. That is why I think that it's worth it to know virtual data room pricing and make a decision.

  2. I'm agreed with John, we need to understand how it works, its very important in our time.
    security online