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 lead‐
22 ing "undef" to disabiguate operators in a normal Alloy expression
23 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 pre‐
111 fix 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 con‐
230 taining the values between and including the first and last argu‐
231 ments.
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 opera‐
252 tions, 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 "{}"
278 This operator is not exposed for external use. It is used inter‐
279 nally by Template::Alloy to delay the creation of a hash until the
280 execution of the compiled template.
281
282 "[]"
283 This operator is not exposed for external use. It is used inter‐
284 nally by Template::Alloy to delay the creation of an array until
285 the execution of the compiled template.
286
287 "qr"
288 This operator is not exposed for external use. It is used inter‐
289 nally by Template::Alloy to store a regular expression and its
290 options. It will return a compiled Regexp object when compiled.
291
292 "-temp-"
293 This operator is not exposed for external use. It is used inter‐
294 nally by some directives to pass temporary, literal data into
295 play_expr to allow additional vmethods or filters to be called on
296 existing data.
297
299 Paul Seamons <paul at seamons dot com>
300
302 This module may be distributed under the same terms as Perl itself.
303
304
305
306perl v5.8.8 2007-07-24 Template::Alloy::Operator(3)