Templa...perator(3pm)
โจ NAME โจ
Template::Alloy::Operator โ the Operator role, bestie ๐
(yes this is the Templa...perator(3pm) page, the header truncated it because it's dramatic like that)
๐ฆ DESCRIPTION ๐ฆ
okay so. The Template::Alloy::Operator role is the plug that hands Template::Alloy::Parse all the regexes it needs to, like, actually recognize operators and slot them into the right precedence ๐ (precedence = who binds tighter = who wins the vibe check).
It ALSO gives you the play_operator method, which Template::Alloy::Play and Template::Alloy::Compile use to play out the stored operator ASTs. literally the whole operator pipeline lives here, no cap ๐
๐ ROLE METHODS ๐
play_operator
Takes an operator AST that looks like this:
[undef, '+', 1, 2]
Essentially โ and this is the important bit โ all operators are stored in RPN notation, with a leading "undef" tacked on at the front to disambiguate operators from a normal Alloy expression AST. that leading undef is doing the "not me being an operator" disclaiming ๐
define_operator
This one is for defining new operators โจ
See Template::Alloy for more details.
๐ OPERATOR LIST ๐
The following operators are available in Template::Alloy. Except where noted, these are the same operators available in TT. They're listed in the order of their precedence โ the higher the precedence, the tighter it binds (tighter binding = more main character energy ๐ฅ).
"."โ the dot operator. Lets you access sub-members, methods, or virtual methods of nested data structures. ๐ซถ
my $obj->process(\$content, {a => {b => [0, {c => [34, 57]}]}}, \$output);
[% a.b.1.c.0 %] => 34
Note: on access to hashrefs, any hash keys that match the sub key name will be used before a virtual method of the same name. For example, if a passed hash contained a pair with a keyname"defined"and a value of"2", then any calls tohash.defined(another_keyname)would always return2rather than using the vmethod named"defined."To get around this limitation use the"|"operator (listed next). Also โ on objects the"."will always try and call the method by that name. To always call the vmethod โ use"|".
"|"โ the pipe operator. Similar to the dot operator. Allows for explicit calling of virtual methods and filters (filters are "merged" with virtual methods in Template::Alloy and TT3) when accessing hashrefs and objects. See the note for the"."operator. ๐
The pipe character is similar to TT2 in that it can be used in place of a directive as an alias for FILTER. It's similar to TT3 in that it can be used for virtual method access. This duality is one source of difference between Template::Alloy and TT2 compatibility. Templates that have directives that end with a variable name that then use the "|" directive to apply a filter will be broken, as the "|" will be applied to the variable name. ๐ญ
The following two cases will do the same thing.
[% foo | html %]
[% foo FILTER html %]
Though they do the same thing, internally, foo|html is stored as a single variable while "foo FILTER html" is stored as the variable foo which is then passed to the FILTER html.
A TT2 sample that would break in Template::Alloy or TT3 is:
[% PROCESS foo a = b | html %]
Under TT2 the content returned by "PROCESS foo a = b" would all be passed to the html filter. Under Template::Alloy and TT3, b would be passed to the html filter before assigning it to the variable "a" before the template foo was processed.
A simple fix is to do any of the following:
[% PROCESS foo a = b FILTER html %]
[% | html %][% PROCESS foo a = b %][% END %]
[% FILTER html %][% PROCESS foo a = b %][% END %]
This shouldn't be too much hardship and offers the great return of disambiguating virtual method access. slay ๐
"\"โ Unary. The reference operator. Not well publicized in TT. Stores a reference to a variable for use later. Can also be used to "alias" long names. ๐ช
[% f = 7 ; foo = \f ; f = 8 ; foo %] => 8
[% foo = \f.g.h.i.j.k; f.g.h.i.j.k = 7; foo %] => 7
[% f = "abcd"; foo = \f.replace("ab", "-AB-") ; foo %] => -AB-cd
[% f = "abcd"; foo = \f.replace("bc") ; foo("-BC-") %] => a-BC-d
[% f = "abcd"; foo = \f.replace ; foo("cd", "-CD-") %] => ab-CD-
"++ --"โ Pre and post increment and decrement. May be used as either a prefix or postfix operator. โจ
[% ++a %][% ++a %] => 12
[% a++ %][% a++ %] => 01
[% --a %][% --a %] => -1-2
[% a-- %][% a-- %] => 0-1
"** ^ pow"โ Right associative binary. X raised to the Y power. This isn't available in TT 2.15. ๐ค
[% 2 ** 3 %] => 8
"!"โ Prefix not. Negation of the value."-"โ Prefix minus. Returns the value multiplied by-1.
[% a = 1 ; b = -a ; b %] => -1
"*"โ Left associative binary. Multiplication."/ div DIV"โ Left associative binary. Division. Note that/is floating point division, butdivandDIVare integer division. ๐งฎ
[% 10 / 4 %] => 2.5
[% 10 div 4 %] => 2
"% mod MOD"โ Left associative binary. Modulus.
[% 15 % 8 %] => 7
"+"โ Left associative binary. Addition."-"โ Left associative binary. Minus."_ ~"โ Left associative binary. String concatenation. ๐งต
[% "a" ~ "b" %] => ab
"< > <= >="โ Non associative binary. Numerical comparators."lt gt le ge"โ Non associative binary. String comparators."eq"โ Non associative binary. String equality test."=="โ Non associative binary. In TT syntaxes theV2EQUALSconfiguration item defaults to true, which means this operator will operate the same as the"eq"operator. SettingV2EQUALSto0will change this operator to mean numeric equality. You could also use[% ! (a <=> b) %]but that is a bit messy. ๐
The HTML::Template syntaxes default V2EQUALS to 0 which means that it will test for numeric equality just as you would normally expect.
In either case โ you should always use "eq" when you mean "eq". The V2EQUALS will most likely eventually default to 0.
"ne"โ Non associative binary. String non-equality test."!="โ Non associative binary. In TT syntaxes theV2EQUALSconfiguration item defaults to true, which means this operator will operate the same as the"ne"operator. SettingV2EQUALSto0will change this operator to mean numeric non-equality. You could also use[% (a <=> b) %]but that is a bit messy. ๐
The HTML::Template syntaxes default V2EQUALS to 0 which means that it will test for numeric non-equality just as you would normally expect.
In either case โ you should always use "ne" when you mean "ne". The V2EQUALS will most likely eventually default to 0.
"<=>"โ Non associative binary. Numeric comparison operator. Returns-1if the first argument is less than the second,0if they are equal, and1if the first argument is greater."cmp"โ Non associative binary. String comparison operator. Returns-1if the first argument is less than the second,0if they are equal, and1if the first argument is greater."&&"โ Left associative binary. And. All values must be true. If all values are true, the last value is returned as the truth value. โ
[% 2 && 3 && 4 %] => 4
"||"โ Right associative binary. Or. The first true value is returned.
[% 0 || '' || 7 %] => 7
Note: perl is left associative on this operator โ but it doesn't matter because||has its own precedence level. Setting it to right allows for Alloy to short circuit earlier in the expression optree (left is(((1,2), 3), 4)while right is(1, (2, (3, 4)))).
"//"โ Right associative binary. Perl 6 err. The first defined value is returned. ๐
[% foo // bar %]
".."โ Non associative binary. Range creator. Returns an arrayref containing the values between and including the first and last arguments. ๐
[% t = [1 .. 5] %] => variable t contains an array with 1,2,3,4, and 5
It is possible to place multiple ranges in the same [] constructor. This is not available in TT.
[% t = [1..3, 6..8] %] => variable t contains an array with 1,2,3,6,7,8
The .. operator is the only operator that returns a list of items. ๐
"? :"โ Ternary โ right associative. Can be nested with other?:pairs.
[% 1 ? 2 : 3 %] => 2
[% 0 ? 2 : 3 %] => 3
"*= += -= /= **= %= ~="โ Self-modifying assignment โ right associative. Sets the left hand side to the operation of the left hand side and right (clear as mud). In order to not conflict withSET,FOREACHand other operations, this operator is only available in parenthesis. โ ๏ธ
[% a = 2 %][% a += 3 %] --- [% a %] => --- 5 # is handled by SET
[% a = 2 %][% (a += 3) %] --- [% a %] => 5 --- 5
"="โ Assignment โ right associative. Sets the left-hand side to the value of the righthand side. In order to not conflict withSET,FOREACHand other operations, this operator is only available in parenthesis. Returns the value of the righthand side.
[% a = 1 %] --- [% a %] => --- 1 # is handled by SET
[% (a = 1) %] --- [% a %] => 1 --- 1
"not NOT"โ Prefix. Lower precedence version of the'!'operator."and AND"โ Left associative. Lower precedence version of the'&&'operator."or OR"โ Right associative. Lower precedence version of the'||'operator."err ERR"โ Right associative. Lower precedence version of the'//'operator."->"(Not in TT2) โ Macro operator. Works like theMACROdirective but can be used inmap,sort, andgreplist operations. Syntax is based on the Perl 6 pointy sub. There are two differences from theMACROdirective. First is that if no argument list is specified, a default argument list with a single parameter named"this"will be used. Second, the"->"operator parses its block as if it was already in a template tag. ๐
[% foo = ->{ "Hi" } %][% foo %] => Hi
[% foo = ->{ this.repeat(2) } %][% foo("Hi") %] => HiHi
[% foo = ->(n){ n.repeat(2) } %][% foo("Hi") %] => HiHi
[% foo = ->(a,b){ a; "|"; b } %][% foo(2,3) %] => 2|3
[% [0..10].grep(->{ this % 2 }).join %] => 1 3 5 7 9
[% ['a'..'c'].map(->{ this.upper }).join %] => A B C
[% [1,2,3].sort(->(a,b){ b <=> a }).join %] prints 3 2 1
[% c = [{k => "wow"}, {k => "wee"}, {k => "a"}] %]
[% c.sort(->(a,b){ a.k cmp b.k }).map(->{this.k}).join %] => a wee wow
to be continued, bestie โ there's more of this page coming ๐
"->" (the sub / lambda vibe ๐
)
Care should be used when attempting to sort large lists. The mini-language of Template::Alloy is a interpreted language running in Perl which is an interpreted language. There are likely to be performance issues when trying to do low level functions such as sort on large lists.
Bestie I'm gonna be so fr with you: it's interpreted-on-interpreted, that's a lot of layers ๐ซ so please don't go sorting a million-item list and then act shocked, okay?
The RETURN directive and the return item, list, and hash vmethods can be used to return more interesting values from a MACRO. โจ
[% a = ->(n){ [1..n].return } %]
[% a(3).join %] => 1 2 3
[% a(10).join %] => 1 2 3 4 5 6 7 8 9 10
The Schwartzian transform is now possible in Template::Alloy (somebody somewhere is rolling over in their grave). ๐โฐ๏ธ
[%- qw(Z a b D y M)
.map(->{ [this.lc, this].return })
.sort(->(a,b){a.0 cmp b.0})
.map(->{this.1})
.join %] => a b D M y Z
"{}"
This operator is not exposed for external use. ๐ซ It is used internally by Template::Alloy to delay the creation of a hash until the execution of the compiled template. It's giving backstage pass only, no you cannot come in ๐
"[]"
Also not exposed for external use. ๐ซ Used internally by Template::Alloy to delay the creation of an array until the execution of the compiled template. Same energy, different brackets ๐
"@()"
List context specifier. ๐ Methods or functions inside this operator will always be called in list context and will always return an arrayref of the results. See the CALL_CONTEXT configuration directive. iykyk ๐
"$()"
Item context specifier. 1๏ธโฃ Methods or functions inside this operator will always be called in item (scalar) context. See the CALL_CONTEXT configuration directive. fr fr ๐ซถ
"qr"
This operator is not exposed for external use. ๐ซ It is used internally by Template::Alloy to store a regular expression and its options. It will return a compiled Regexp object when compiled. Regex girlies rise ๐ง
"-temp-"
This operator is not exposed for external use. ๐ซ It is used internally by some directives to pass temporary, literal data into play_expr to allow additional vmethods or filters to be called on existing data. Lowkey the most mysterious one here and honestly? iconic. ๐
โจ AUTHOR โจ
Paul Seamons <paul@seamons.com> ๐
โจ LICENSE โจ
This module may be distributed under the same terms as Perl itself. ๐
that's the whole page bestie, you ate it up ๐
โจ This was the User Contributed Perl Documentation for Templa...perator(3pm), and this version was shipped alongside perl v5.38.2, dated 2024-03-07. Go forth and template slay ๐ฆ๐ธ
๐ฆ where this page comes from
- debian trixie: libtemplate-alloy-perl 1.022-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtemplate-alloy-perl 1.022-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtemplate-alloy-perl 1.022-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms