|
NAMETemplate::Alloy::VMethod - VMethod role.DESCRIPTIONThe Template::Alloy::VMethod role provides all of the extra vmethods, filters, and virtual objects that add to the base feature set of Template::Alloy. Most of the vmethods listed here are similar to those provided by Template::Toolkit. We will try to keep Template::Alloy's in sync. Template::Alloy also provides several extra methods that are needed for HTML::Template::Expr support.ROLE METHODS
VIRTUAL METHOD LISTThe following is the list of builtin virtual methods and filters that can be called on each type of data.In Template::Alloy, the "|" operator can be used to call virtual methods just the same way that the "." operator can. The main difference between the two is that on access to hashrefs or objects, the "|" means to always call the virtual method or filter rather than looking in the hashref for a key by that name, or trying to call that method on the object. This is similar to how TT3 will function. Virtual methods are also made available via Virtual Objects which are discussed in a later section. SCALAR VIRTUAL METHODS AND FILTERSThe following is the list of builtin virtual methods and filters that can be called on scalar data types. In Alloy and TT3, filters and virtual methods are more closely related than in TT2. In general anywhere a virtual method can be used a filter can be used also - and likewise all scalar virtual methods can be used as filters.In addition to the filters listed below, Alloy will automatically load Template::Filters and use them if Template::Toolkit is installed. In addition to the scalar virtual methods, any scalar will be automatically converted to a single item list if a list virtual method is called on it. Scalar virtual methods are also available through the "Text" virtual object (except for true filters such as eval and redirect). All scalar virtual methods are available as top level functions as well. This is not true of TT2. In Template::Alloy the following are equivalent: [% "abc".length %] [% length("abc") %] You may set VMETHOD_FUNCTIONS to 0 to disable this behavior.
LIST VIRTUAL METHODSThe following methods can be called on an arrayref type data structures (scalar types will automatically promote to a single element list and call these methods if needed):Additionally, list virtual methods can be accessed via the List Virtual Object.
HASH VIRTUAL METHODSThe following methods can be called on hash type data structures:Additionally, list virtual methods can be accessed via the Hash Virtual Object.
VIRTUAL OBJECTSTT3 has a concept of Text, List, and Hash virtual objects which provide direct access to the scalar, list, and hash virtual methods. In the TT3 engine this will allow for more concise generated code. Because Alloy does not generated perl code to be executed later, Alloy provides for these virtual objects but does so as more of a namespace (using the methods does not provide a speed optimization in your template - just may help clarify things).[% a = "foo"; a.length %] => 3 [% a = "foo"; Text.length(a) %] => 3 [% a = Text.new("foo"); a.length %] => 3 [% a = [1 .. 30]; a.size %] => 30 [% a = [1 .. 30]; List.size(a) %] => 30 [% a = List.new(1 .. 30); a.size %] => 30 [% a = {a => 1, b => 2}; a.size %] => 2 [% a = {a => 1, b => 2}; Hash.size(a) %] => 2 [% a = Hash.new({a => 1, b => 2}); a.size %] => 2 [% a = Hash.new(a => 1, b => 2); a.size %] => 2 [% a = Hash.new(a = 1, b = 2); a.size %] => 2 [% a = Hash.new('a', 1, 'b', 2); a.size %] => 2 One limitation is that if you pass a key named "Text", "List", or "Hash" in your variable stash - the corresponding virtual object will be hidden. Additionally, you can use all of the Virtual object methods with the pipe operator. [% {a => 1, b => 2} | Hash.keys | List.join(", ") %] => a, b Again, there aren't any speed optimizations to using the virtual objects in Alloy, but it can help clarify the intent in some cases. Note: these aren't really objects. All of the "virtual objects" are references to the $SCALAR_OPS, $LIST_OPS, and $HASH_OPS hashes found in the $VOBJS hash of Template::Alloy. AUTHORPaul Seamons <paul@seamons.com>LICENSEThis module may be distributed under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |