1Template::Alloy::OperatUosre(r3)Contributed Perl DocumenTteamtpiloante::Alloy::Operator(3)
2
3
4
6 Template::Alloy::Operator - Operator role.
7
9 The Template::Alloy::Operator role provides the regexes necessary for
10 Template::Alloy::Parse to parse operators and place them in their
11 appropriate precedence. It also provides the play_operator method
12 which is used by Template::Alloy::Play and Template::Alloy::Compile for
13 playing out the stored operator ASTs.
14
16 play_operator
17 Takes an operator AST in the form of
18
19 [undef, '+', 1, 2]
20
21 Essentially, all operators are stored in RPN notation with a
22 leading "undef" to disambiguate operators in a normal Alloy
23 expression AST.
24
25 define_operator
26 Used for defining new operators.
27
28 See Template::Alloy for more details.
29
31 The following operators are available in Template::Alloy. Except where
32 noted these are the same operators available in TT. They are listed in
33 the order of their precedence (the higher the precedence the tighter it
34 binds).
35
36 "." The dot operator. Allows for accessing sub-members, methods, or
37 virtual methods of nested data structures.
38
39 my $obj->process(\$content, {a => {b => [0, {c => [34, 57]}]}}, \$output);
40
41 [% a.b.1.c.0 %] => 34
42
43 Note: on access to hashrefs, any hash keys that match the sub key
44 name will be used before a virtual method of the same name. For
45 example if a passed hash contained pair with a keyname "defined"
46 and a value of "2", then any calls to hash.defined(another_keyname)
47 would always return 2 rather than using the vmethod named
48 "defined." To get around this limitation use the "|" operator
49 (listed next). Also - on objects the "." will always try and call
50 the method by that name. To always call the vmethod - use "|".
51
52 "|" The pipe operator. Similar to the dot operator. Allows for
53 explicit calling of virtual methods and filters (filters are
54 "merged" with virtual methods in Template::Alloy and TT3) when
55 accessing hashrefs and objects. See the note for the "." operator.
56
57 The pipe character is similar to TT2 in that it can be used in
58 place of a directive as an alias for FILTER. It similar to TT3 in
59 that it can be used for virtual method access. This duality is one
60 source of difference between Template::Alloy and TT2 compatibility.
61 Templates that have directives that end with a variable name that
62 then use the "|" directive to apply a filter will be broken as the
63 "|" will be applied to the variable name.
64
65 The following two cases will do the same thing.
66
67 [% foo | html %]
68
69 [% foo FILTER html %]
70
71 Though they do the same thing, internally, foo|html is stored as a
72 single variable while "foo FILTER html" is stored as the variable
73 foo which is then passed to the FILTER html.
74
75 A TT2 sample that would break in Template::Alloy or TT3 is:
76
77 [% PROCESS foo a = b | html %]
78
79 Under TT2 the content returned by "PROCESS foo a = b" would all be
80 passed to the html filter. Under Template::Alloy and TT3, b would
81 be passed to the html filter before assigning it to the variable
82 "a" before the template foo was processed.
83
84 A simple fix is to do any of the following:
85
86 [% PROCESS foo a = b FILTER html %]
87
88 [% | html %][% PROCESS foo a = b %][% END %]
89
90 [% FILTER html %][% PROCESS foo a = b %][% END %]
91
92 This shouldn't be too much hardship and offers the great return of
93 disambiguating virtual method access.
94
95 "\" Unary. The reference operator. Not well publicized in TT. Stores
96 a reference to a variable for use later. Can also be used to
97 "alias" long names.
98
99 [% f = 7 ; foo = \f ; f = 8 ; foo %] => 8
100
101 [% foo = \f.g.h.i.j.k; f.g.h.i.j.k = 7; foo %] => 7
102
103 [% f = "abcd"; foo = \f.replace("ab", "-AB-") ; foo %] => -AB-cd
104
105 [% f = "abcd"; foo = \f.replace("bc") ; foo("-BC-") %] => a-BC-d
106
107 [% f = "abcd"; foo = \f.replace ; foo("cd", "-CD-") %] => ab-CD-
108
109 "++ --"
110 Pre and post increment and decrement. My be used as either a
111 prefix or postfix operator.
112
113 [% ++a %][% ++a %] => 12
114
115 [% a++ %][% a++ %] => 01
116
117 [% --a %][% --a %] => -1-2
118
119 [% a-- %][% a-- %] => 0-1
120
121 "** ^ pow"
122 Right associative binary. X raised to the Y power. This isn't
123 available in TT 2.15.
124
125 [% 2 ** 3 %] => 8
126
127 "!" Prefix not. Negation of the value.
128
129 "-" Prefix minus. Returns the value multiplied by -1.
130
131 [% a = 1 ; b = -a ; b %] => -1
132
133 "*" Left associative binary. Multiplication.
134
135 "/ div DIV"
136 Left associative binary. Division. Note that / is floating point
137 division, but div and DIV are integer division.
138
139 [% 10 / 4 %] => 2.5
140 [% 10 div 4 %] => 2
141
142 "% mod MOD"
143 Left associative binary. Modulus.
144
145 [% 15 % 8 %] => 7
146
147 "+" Left associative binary. Addition.
148
149 "-" Left associative binary. Minus.
150
151 "_ ~"
152 Left associative binary. String concatenation.
153
154 [% "a" ~ "b" %] => ab
155
156 "< > <= >="
157 Non associative binary. Numerical comparators.
158
159 "lt gt le ge"
160 Non associative binary. String comparators.
161
162 "eq"
163 Non associative binary. String equality test.
164
165 "=="
166 Non associative binary. In TT syntaxes the V2EQUALS configuration
167 item defaults to true which means this operator will operate the
168 same as the "eq" operator. Setting V2EQUALS to 0 will change this
169 operator to mean numeric equality. You could also use [% ! (a <=>
170 b) %] but that is a bit messy.
171
172 The HTML::Template syntaxes default V2EQUALS to 0 which means that
173 it will test for numeric equality just as you would normally
174 expect.
175
176 In either case - you should always use "eq" when you mean "eq".
177 The V2EQUALS will most likely eventually default to 0.
178
179 "ne"
180 Non associative binary. String non-equality test.
181
182 "!="
183 Non associative binary. In TT syntaxes the V2EQUALS configuration
184 item defaults to true which means this operator will operate the
185 same as the "ne" operator. Setting V2EQUALS to 0 will change this
186 operator to mean numeric non-equality. You could also use [% (a
187 <=> b) %] but that is a bit messy.
188
189 The HTML::Template syntaxes default V2EQUALS to 0 which means that
190 it will test for numeric non-equality just as you would normally
191 expect.
192
193 In either case - you should always use "ne" when you mean "ne".
194 The V2EQUALS will most likely eventually default to 0.
195
196 "<=>"
197 Non associative binary. Numeric comparison operator. Returns -1
198 if the first argument is less than the second, 0 if they are equal,
199 and 1 if the first argument is greater.
200
201 "cmp"
202 Non associative binary. String comparison operator. Returns -1 if
203 the first argument is less than the second, 0 if they are equal,
204 and 1 if the first argument is greater.
205
206 "&&"
207 Left associative binary. And. All values must be true. If all
208 values are true, the last value is returned as the truth value.
209
210 [% 2 && 3 && 4 %] => 4
211
212 "||"
213 Right associative binary. Or. The first true value is returned.
214
215 [% 0 || '' || 7 %] => 7
216
217 Note: perl is left associative on this operator - but it doesn't
218 matter because || has its own precedence level. Setting it to
219 right allows for Alloy to short circuit earlier in the expression
220 optree (left is (((1,2), 3), 4) while right is (1, (2, (3, 4))).
221
222 "//"
223 Right associative binary. Perl 6 err. The first defined value is
224 returned.
225
226 [% foo // bar %]
227
228 ".."
229 Non associative binary. Range creator. Returns an arrayref
230 containing the values between and including the first and last
231 arguments.
232
233 [% t = [1 .. 5] %] => variable t contains an array with 1,2,3,4, and 5
234
235 It is possible to place multiple ranges in the same [] constructor.
236 This is not available in TT.
237
238 [% t = [1..3, 6..8] %] => variable t contains an array with 1,2,3,6,7,8
239
240 The .. operator is the only operator that returns a list of items.
241
242 "? :"
243 Ternary - right associative. Can be nested with other ?: pairs.
244
245 [% 1 ? 2 : 3 %] => 2
246 [% 0 ? 2 : 3 %] => 3
247
248 "*= += -= /= **= %= ~="
249 Self-modifying assignment - right associative. Sets the left hand
250 side to the operation of the left hand side and right (clear as
251 mud). In order to not conflict with SET, FOREACH and other
252 operations, this operator is only available in parenthesis.
253
254 [% a = 2 %][% a += 3 %] --- [% a %] => --- 5 # is handled by SET
255 [% a = 2 %][% (a += 3) %] --- [% a %] => 5 --- 5
256
257 "=" Assignment - right associative. Sets the left-hand side to the
258 value of the righthand side. In order to not conflict with SET,
259 FOREACH and other operations, this operator is only available in
260 parenthesis. Returns the value of the righthand side.
261
262 [% a = 1 %] --- [% a %] => --- 1 # is handled by SET
263 [% (a = 1) %] --- [% a %] => 1 --- 1
264
265 "not NOT"
266 Prefix. Lower precedence version of the '!' operator.
267
268 "and AND"
269 Left associative. Lower precedence version of the '&&' operator.
270
271 "or OR"
272 Right associative. Lower precedence version of the '||' operator.
273
274 "err ERR"
275 Right associative. Lower precedence version of the '//' operator.
276
277 "->" (Not in TT2)
278 Macro operator. Works like the MACRO directive but can be used in
279 map, sort, and grep list operations. Syntax is based on the Perl 6
280 pointy sub. There are two differences from the MACRO directive.
281 First is that if no argument list is specified, a default argument
282 list with a single parameter named "this" will be used. Second,
283 the "->" operator parses its block as if it was already in a
284 template tag.
285
286 [% foo = ->{ "Hi" } %][% foo %] => Hi
287 [% foo = ->{ this.repeat(2) } %][% foo("Hi") %] => HiHi
288 [% foo = ->(n){ n.repeat(2) } %][% foo("Hi") %] => HiHi
289 [% foo = ->(a,b){ a; "|"; b } %][% foo(2,3) %] => 2|3
290
291 [% [0..10].grep(->{ this % 2 }).join %] => 1 3 5 7 9
292 [% ['a'..'c'].map(->{ this.upper }).join %] => A B C
293
294 [% [1,2,3].sort(->(a,b){ b <=> a }).join %] prints 3 2 1
295
296 [% c = [{k => "wow"}, {k => "wee"}, {k => "a"}] %]
297 [% c.sort(->(a,b){ a.k cmp b.k }).map(->{this.k}).join %] => a wee wow
298
299 Note: Care should be used when attempting to sort large lists. The
300 mini-language of Template::Alloy is a interpreted language running
301 in Perl which is an interpreted language. There are likely to be
302 performance issues when trying to do low level functions such as
303 sort on large lists.
304
305 The RETURN directive and return item, list, and hash vmethods can
306 be used to return more interesting values from a MACRO.
307
308 [% a = ->(n){ [1..n].return } %]
309 [% a(3).join %] => 1 2 3
310 [% a(10).join %] => 1 2 3 4 5 6 7 8 9 10
311
312 The Schwartzian transform is now possible in Template::Alloy
313 (somebody somewhere is rolling over in their grave).
314
315 [%- qw(Z a b D y M)
316 .map(->{ [this.lc, this].return })
317 .sort(->(a,b){a.0 cmp b.0})
318 .map(->{this.1})
319 .join %] => a b D M y Z
320
321 "{}"
322 This operator is not exposed for external use. It is used
323 internally by Template::Alloy to delay the creation of a hash until
324 the execution of the compiled template.
325
326 "[]"
327 This operator is not exposed for external use. It is used
328 internally by Template::Alloy to delay the creation of an array
329 until the execution of the compiled template.
330
331 "@()"
332 List context specifier. Methods or functions inside this operator
333 will always be called in list context and will always return an
334 arrayref of the results. See the CALL_CONTEXT configuration
335 directive.
336
337 "$()"
338 Item context specifier. Methods or functions inside this operator
339 will always be called in item (scalar) context. See the
340 CALL_CONTEXT configuration directive.
341
342 "qr"
343 This operator is not exposed for external use. It is used
344 internally by Template::Alloy to store a regular expression and its
345 options. It will return a compiled Regexp object when compiled.
346
347 "-temp-"
348 This operator is not exposed for external use. It is used
349 internally by some directives to pass temporary, literal data into
350 play_expr to allow additional vmethods or filters to be called on
351 existing data.
352
354 Paul Seamons <paul@seamons.com>
355
357 This module may be distributed under the same terms as Perl itself.
358
359
360
361perl v5.28.0 2013-08-23 Template::Alloy::Operator(3)