[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Manual' page = 'VMethods' %] [% WRAPPER toc; PROCESS tocitem title ="DESCRIPTION" subs = [ "Scalar Virtual Methods", "Hash Virtual Methods", "List Virtual Methods", "Automagic Promotion of Scalar to List for Virtual Methods", "Defining Custom Virtual Methods" ]; PROCESS tocitem title ="AUTHOR" subs = []; PROCESS tocitem title ="VERSION" subs = []; PROCESS tocitem title ="COPYRIGHT" subs = []; END %] [% WRAPPER section title="DESCRIPTION" -%]

The Template Toolkit provides virtual methods for manipulating variable values. Most of them are analogous to regular Perl functions of the same names. This section describes the different virtual methods that can be applied to scalar, list and hash values.

[% WRAPPER subsection title = "Scalar Virtual Methods" -%] [%- END %] [% WRAPPER subsection title = "Hash Virtual Methods" -%] [%- END %] [% WRAPPER subsection title = "List Virtual Methods" -%] [%- END %] [% WRAPPER subsection title = "Automagic Promotion of Scalar to List for Virtual Methods" -%]

In addition to the scalar virtual methods listed in the previous section, you can also call any list virtual method against a scalar. The item will be automagically promoted to a single element list and the appropriate list virtual method will be called.

One particular benefit of this comes when calling subroutines or object methods that return a list of items, rather than the preferred reference to a list of items. In this case, the Template Toolkit automatically folds the items returned into a list.

The upshot is that you can continue to use existing Perl modules or code that returns lists of items, without having to refactor it just to keep the Template Toolkit happy (by returning references to list). Class::DBI module is just one example of a particularly useful module which returns values this way.

If only a single item is returned from a subroutine then the Template Toolkit assumes it meant to return a single item (rather than a list of 1 item) and leaves it well alone, returning the single value as it is. If you're executing a database query, for example, you might get 1 item returned, or perhaps many items which are then folded into a list.

The FOREACH directive will happily accept either a list or a single item which it will treat as a list. So it's safe to write directives like this, where we assume that 'something' is bound to a subroutine which might return 1 or more items:

    [% tt_start_tag %] FOREACH item = something [% tt_end_tag %]
       ...
    [% tt_start_tag %] END [% tt_end_tag %]

The automagic promotion of scalars to single item lists means that you can also use list virtual methods safely, even if you only get one item returned. For example:

    [% tt_start_tag %] something.first   [% tt_end_tag %]
    [% tt_start_tag %] something.join    [% tt_end_tag %]
    [% tt_start_tag %] something.reverse.join(', ') [% tt_end_tag %]

Note that this is very much a last-ditch behaviour. If the single item return is an object with a 'first' method, for example, then that will be called, as expected, in preference to the list virtual method.

[%- END %] [% WRAPPER subsection title = "Defining Custom Virtual Methods" -%]

You can define your own virtual methods for scalars, lists and hash arrays. The Template::Stash package variables $SCALAR_OPS, $LIST_OPS and $HASH_OPS are references to hash arrays that define these virtual methods. HASH_OPS and LIST_OPS methods are subroutines that accept a hash/list reference as the first item. SCALAR_OPS are subroutines that accept a scalar value as the first item. Any other arguments specified when the method is called will be passed to the subroutine.

    # load Template::Stash to make method tables visible
    use Template::Stash;
    # define list method to return new list of odd numbers only
    $Template::Stash::LIST_OPS->{ odd } = sub {
	my $list = shift;
	return [ grep { $_ % 2 } @$list ];
    };

template:

    [% tt_start_tag %] primes = [ 2, 3, 5, 7, 9 ] [% tt_end_tag %]
    [% tt_start_tag %] primes.odd.join(', ') [% tt_end_tag %]		# 3, 5, 7, 9
[%- END %] [%- END %] [% WRAPPER section title="AUTHOR" -%]

Andy Wardley <abw@andywardley.com>

[% ttlink('http://www.andywardley.com/', 'http://www.andywardley.com/') -%]

[%- END %] [% WRAPPER section title="VERSION" -%]

Template Toolkit version 2.14, released on 04 October 2004.

[%- END %] [% WRAPPER section title="COPYRIGHT" -%]
  Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

[%- END %]