f51ce2f4742c089835f82b35ec554c5bfe8e735a
[catagits/Gitalist.git] / local-lib5 / man / man3 / Template::Manual::Variables.3pm
1 .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.3
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sh \" Subsection heading
6 .br
7 .if t .Sp
8 .ne 5
9 .PP
10 \fB\\$1\fR
11 .PP
12 ..
13 .de Sp \" Vertical space (when we can't use .PP)
14 .if t .sp .5v
15 .if n .sp
16 ..
17 .de Vb \" Begin verbatim text
18 .ft CW
19 .nf
20 .ne \\$1
21 ..
22 .de Ve \" End verbatim text
23 .ft R
24 .fi
25 ..
26 .\" Set up some character translations and predefined strings.  \*(-- will
27 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28 .\" double quote, and \*(R" will give a right double quote.  | will give a
29 .\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
30 .\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
31 .\" expand to `' in nroff, nothing in troff, for use with C<>.
32 .tr \(*W-|\(bv\*(Tr
33 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34 .ie n \{\
35 .    ds -- \(*W-
36 .    ds PI pi
37 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
39 .    ds L" ""
40 .    ds R" ""
41 .    ds C` ""
42 .    ds C' ""
43 'br\}
44 .el\{\
45 .    ds -- \|\(em\|
46 .    ds PI \(*p
47 .    ds L" ``
48 .    ds R" ''
49 'br\}
50 .\"
51 .\" If the F register is turned on, we'll generate index entries on stderr for
52 .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53 .\" entries marked with X<> in POD.  Of course, you'll have to process the
54 .\" output yourself in some meaningful fashion.
55 .if \nF \{\
56 .    de IX
57 .    tm Index:\\$1\t\\n%\t"\\$2"
58 ..
59 .    nr % 0
60 .    rr F
61 .\}
62 .\"
63 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
64 .\" way too many mistakes in technical documents.
65 .hy 0
66 .if n .na
67 .\"
68 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
70 .    \" fudge factors for nroff and troff
71 .if n \{\
72 .    ds #H 0
73 .    ds #V .8m
74 .    ds #F .3m
75 .    ds #[ \f1
76 .    ds #] \fP
77 .\}
78 .if t \{\
79 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80 .    ds #V .6m
81 .    ds #F 0
82 .    ds #[ \&
83 .    ds #] \&
84 .\}
85 .    \" simple accents for nroff and troff
86 .if n \{\
87 .    ds ' \&
88 .    ds ` \&
89 .    ds ^ \&
90 .    ds , \&
91 .    ds ~ ~
92 .    ds /
93 .\}
94 .if t \{\
95 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101 .\}
102 .    \" troff and (daisy-wheel) nroff accents
103 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110 .ds ae a\h'-(\w'a'u*4/10)'e
111 .ds Ae A\h'-(\w'A'u*4/10)'E
112 .    \" corrections for vroff
113 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115 .    \" for low resolution devices (crt and lpr)
116 .if \n(.H>23 .if \n(.V>19 \
117 \{\
118 .    ds : e
119 .    ds 8 ss
120 .    ds o a
121 .    ds d- d\h'-1'\(ga
122 .    ds D- D\h'-1'\(hy
123 .    ds th \o'bp'
124 .    ds Th \o'LP'
125 .    ds ae ae
126 .    ds Ae AE
127 .\}
128 .rm #[ #] #H #V #F C
129 .\" ========================================================================
130 .\"
131 .IX Title "Template::Manual::Variables 3"
132 .TH Template::Manual::Variables 3 "2008-11-13" "perl v5.8.7" "User Contributed Perl Documentation"
133 .SH "NAME"
134 Template::Manual::Variables \- Template variables and code bindings
135 .SH "Template Variables"
136 .IX Header "Template Variables"
137 A reference to a hash array may be passed as the second argument to the
138 \&\fIprocess()\fR method, containing definitions of template
139 variables. The \f(CW\*(C`VARIABLES\*(C'\fR (a.k.a. \f(CW\*(C`PRE_DEFINE\*(C'\fR) option can also be used to
140 pre-define variables for all templates processed by the object.
141 .PP
142 .Vb 6
143 \&    my $tt = Template\->new({
144 \&        VARIABLES => {
145 \&            version => 3.14,
146 \&            release => 'Sahara',
147 \&        },  
148 \&    });
149 .Ve
150 .PP
151 .Vb 3
152 \&    my $vars = {
153 \&        serial_no => 271828,
154 \&    };
155 .Ve
156 .PP
157 .Vb 1
158 \&    $tt\->process('myfile', $vars);
159 .Ve
160 .PP
161 \&\fImyfile\fR template:
162 .PP
163 .Vb 2
164 \&    This is version [% version %] ([% release %]).
165 \&    Serial number: [% serial_no %]
166 .Ve
167 .PP
168 Generated Output: 
169 .PP
170 .Vb 2
171 \&    This is version 3.14 (Sahara)
172 \&    Serial number: 271828
173 .Ve
174 .PP
175 Variable names may contain any alphanumeric characters or underscores. They
176 may be lower, upper or mixed case although the usual convention is to use
177 lower case. The case \fIis\fR significant however, and '\f(CW\*(C`foo\*(C'\fR', '\f(CW\*(C`Foo\*(C'\fR' and
178 \&'\f(CW\*(C`FOO\*(C'\fR' are all different variables. Upper case variable names are permitted,
179 but not recommended due to a possible conflict with an existing or future
180 reserved word.  As of version 2.00, these are:
181 .PP
182 .Vb 5
183 \&    GET CALL SET DEFAULT INSERT INCLUDE PROCESS WRAPPER 
184 \&    IF UNLESS ELSE ELSIF FOR FOREACH WHILE SWITCH CASE
185 \&    USE PLUGIN FILTER MACRO PERL RAWPERL BLOCK META
186 \&    TRY THROW CATCH FINAL NEXT LAST BREAK RETURN STOP 
187 \&    CLEAR TO STEP AND OR NOT MOD DIV END
188 .Ve
189 .PP
190 The variable values may be of virtually any Perl type, including
191 simple scalars, references to lists, hash arrays, subroutines or
192 objects.  The Template Toolkit will automatically apply the correct
193 procedure to accessing these values as they are used in the template.
194 .PP
195 Example data:
196 .PP
197 .Vb 11
198 \&    my $vars = {
199 \&        article => 'The Third Shoe',
200 \&        person  => { 
201 \&            id    => 314, 
202 \&            name  => 'Mr. Blue',
203 \&            email => 'blue@nowhere.org',
204 \&        },
205 \&        primes  => [ 2, 3, 5, 7, 11, 13 ],
206 \&        wizard  => sub { return join(' ', 'Abracadabra!', @_) },
207 \&        cgi     => CGI\->new('mode=submit&debug=1'),
208 \&    };
209 .Ve
210 .PP
211 Example template:
212 .PP
213 .Vb 1
214 \&    [% article %]
215 .Ve
216 .PP
217 .Vb 1
218 \&    [% person.id %]: [% person.name %] <[% person.email %]>
219 .Ve
220 .PP
221 .Vb 2
222 \&    [% primes.first %] \- [% primes.last %], including [% primes.3 %]
223 \&    [% primes.size %] prime numbers: [% primes.join(', ') %]
224 .Ve
225 .PP
226 .Vb 2
227 \&    [% wizard %]
228 \&    [% wizard('Hocus Pocus!') %]
229 .Ve
230 .PP
231 .Vb 1
232 \&    [% cgi.param('mode') %]
233 .Ve
234 .PP
235 Generated output:
236 .PP
237 .Vb 1
238 \&    The Third Shoe
239 .Ve
240 .PP
241 .Vb 1
242 \&    314: Mr. Blue <blue@nowhere.org>
243 .Ve
244 .PP
245 .Vb 2
246 \&    2 \- 13, including 7
247 \&    6 prime numbers: 2, 3, 5, 7, 11, 13
248 .Ve
249 .PP
250 .Vb 2
251 \&    Abracadabra!
252 \&    Abracadabra! Hocus Pocus!
253 .Ve
254 .PP
255 .Vb 1
256 \&    submit
257 .Ve
258 .Sh "Scalar Values"
259 .IX Subsection "Scalar Values"
260 Regular scalar variables are accessed by simply specifying their name.
261 As these are just entries in the top-level variable hash they can be 
262 considered special cases of hash array referencing as described below,
263 with the main namespace hash automatically implied.
264 .PP
265 .Vb 1
266 \&    [% article %]
267 .Ve
268 .Sh "Hash Array References"
269 .IX Subsection "Hash Array References"
270 Members of hash arrays are accessed by specifying the hash reference
271 and key separated by the dot '\f(CW\*(C`.\*(C'\fR' operator.
272 .PP
273 Example data:
274 .PP
275 .Vb 8
276 \&    my $vars = {
277 \&        'home' => 'http://www.myserver.com/homepage.html',
278 \&        'page' => {
279 \&            'this' => 'mypage.html',
280 \&            'next' => 'nextpage.html',
281 \&            'prev' => 'prevpage.html',
282 \&        },
283 \&    };
284 .Ve
285 .PP
286 Example template:
287 .PP
288 .Vb 3
289 \&    <a href="[% home %]">Home</a>
290 \&    <a href="[% page.prev %]">Previous Page</a>
291 \&    <a href="[% page.next %]">Next Page</a>
292 .Ve
293 .PP
294 Generated output:
295 .PP
296 .Vb 3
297 \&    <a href="http://www.myserver.com/homepage.html">Home</a>
298 \&    <a href="prevpage.html">Previous Page</a>
299 \&    <a href="nextpage.html">Next Page</a>
300 .Ve
301 .PP
302 Any key in a hash which starts with a '\f(CW\*(C`_\*(C'\fR' or '\f(CW\*(C`.\*(C'\fR' character will be
303 considered private and cannot be evaluated or updated from within a
304 template.  The undefined value will be returned for any such variable
305 accessed which the Template Toolkit will silently ignore (unless the
306 \&\f(CW\*(C`DEBUG\*(C'\fR option is enabled).
307 .PP
308 Example data:
309 .PP
310 .Vb 9
311 \&    my $vars = {
312 \&        message => 'Hello World!',
313 \&        _secret => "On the Internet, no\-one knows you're a dog",
314 \&        thing   => {
315 \&            public    => 123,
316 \&            _private  => 456,
317 \&            '.hidden' => 789,
318 \&        },
319 \&    };
320 .Ve
321 .PP
322 Example template:
323 .PP
324 .Vb 5
325 \&    [% message %]           # outputs "Hello World!"
326 \&    [% _secret %]           # no output
327 \&    [% thing.public %]      # outputs "123"
328 \&    [% thing._private %]    # no output
329 \&    [% thing..hidden %]     # ERROR: unexpected token (..)
330 .Ve
331 .PP
332 You can disable this feature by setting the \f(CW$Template::Stash::PRIVATE\fR
333 package variable to a false value.
334 .PP
335 .Vb 1
336 \&    $Template::Stash::PRIVATE = undef;   # now you can thing._private
337 .Ve
338 .PP
339 To access a hash entry using a key stored in another variable, prefix
340 the key variable with '\f(CW\*(C`$\*(C'\fR' to have it interpolated before use (see
341 \&\*(L"Variable Interpolation\*(R").
342 .PP
343 .Vb 2
344 \&    [% pagename = 'next' %]
345 \&    [% page.$pagename %]       # same as [% page.next %]
346 .Ve
347 .PP
348 When you assign to a variable that contains multiple namespace 
349 elements (i.e. it has one or more '\f(CW\*(C`.\*(C'\fR' characters in the name),
350 any hashes required to represent intermediate namespaces will be 
351 created automatically.  In this following example, the \f(CW\*(C`product\*(C'\fR 
352 variable automatically springs into life as a hash array unless
353 otherwise defined.
354 .PP
355 .Vb 4
356 \&    [% product.id    = 'XYZ\-2000' 
357 \&       product.desc  = 'Bogon Generator'
358 \&       product.price = 666 
359 \&    %]
360 .Ve
361 .PP
362 .Vb 2
363 \&    The [% product.id %] [% product.desc %] 
364 \&    costs $[% product.price %].00
365 .Ve
366 .PP
367 Generated output:
368 .PP
369 .Vb 2
370 \&    The XYZ\-2000 Bogon Generator 
371 \&    costs $666.00
372 .Ve
373 .PP
374 You can use Perl's familiar \f(CW\*(C`{\*(C'\fR ... \f(CW\*(C`}\*(C'\fR construct to explicitly create
375 a hash and assign it to a variable.  Note that commas are optional
376 between key/value pairs and \f(CW\*(C`=\*(C'\fR can be used in place of \f(CW\*(C`=>\*(C'\fR.
377 .PP
378 .Vb 7
379 \&    # minimal TT style
380 \&    [% product = {
381 \&         id    = 'XYZ\-2000'
382 \&         desc  = 'Bogon Generator'
383 \&         price = 666
384 \&       }
385 \&    %]
386 .Ve
387 .PP
388 .Vb 7
389 \&    # perl style
390 \&    [% product = {
391 \&         id    => 'XYZ\-2000',
392 \&         desc  => 'Bogon Generator',
393 \&         price => 666,
394 \&       }
395 \&    %]
396 .Ve
397 .Sh "List References"
398 .IX Subsection "List References"
399 Items in lists are also accessed by use of the dot operator.
400 .PP
401 Example data:
402 .PP
403 .Vb 3
404 \&    my $vars = {
405 \&        people => [ 'Tom', 'Dick', 'Larry' ],
406 \&    };
407 .Ve
408 .PP
409 Example template:
410 .PP
411 .Vb 3
412 \&    [% people.0 %]          # Tom
413 \&    [% people.1 %]          # Dick
414 \&    [% people.2 %]          # Larry
415 .Ve
416 .PP
417 The \f(CW\*(C`FOREACH\*(C'\fR directive can be used to iterate through items in a list.
418 .PP
419 .Vb 3
420 \&    [% FOREACH person IN people %]
421 \&    Hello [% person %]
422 \&    [% END %]
423 .Ve
424 .PP
425 Generated output:
426 .PP
427 .Vb 3
428 \&    Hello Tom
429 \&    Hello Dick
430 \&    Hello Larry
431 .Ve
432 .PP
433 Lists can be constructed in-situ using the regular anonymous list
434 \&\f(CW\*(C`[\*(C'\fR ... \f(CW\*(C`]\*(C'\fR construct.  Commas between items are optional.
435 .PP
436 .Vb 1
437 \&    [% cols = [ 'red', 'green', 'blue' ] %]
438 .Ve
439 .PP
440 .Vb 3
441 \&    [% FOREACH c IN cols %]
442 \&       [% c %]
443 \&    [% END %]
444 .Ve
445 .PP
446 or:
447 .PP
448 .Vb 3
449 \&    [% FOREACH c IN [ 'red', 'green', 'blue' ] %]
450 \&       [% c %]
451 \&    [% END %]
452 .Ve
453 .PP
454 You can also create simple numerical sequences using the \f(CW\*(C`..\*(C'\fR range
455 operator:
456 .PP
457 .Vb 1
458 \&    [% n = [ 1 .. 4 ] %]    # n is [ 1, 2, 3, 4 ]
459 .Ve
460 .PP
461 .Vb 4
462 \&    [% x = 4
463 \&       y = 8
464 \&       z = [x..y]           # z is [ 4, 5, 6, 7, 8 ]
465 \&    %]
466 .Ve
467 .Sh "Subroutines"
468 .IX Subsection "Subroutines"
469 Template variables can contain references to Perl subroutines.  When
470 the variable is used, the Template Toolkit will automatically call the
471 subroutine, passing any additional arguments specified.  The return
472 value from the subroutine is used as the variable value and inserted
473 into the document output.
474 .PP
475 .Vb 3
476 \&    my $vars = {
477 \&        wizard  => sub { return join(' ', 'Abracadabra!', @_) },
478 \&    };
479 .Ve
480 .PP
481 Example template:
482 .PP
483 .Vb 2
484 \&    [% wizard %]                    # Abracadabra!
485 \&    [% wizard('Hocus Pocus!') %]    # Abracadabra! Hocus Pocus!
486 .Ve
487 .Sh "Objects"
488 .IX Subsection "Objects"
489 Template variables can also contain references to Perl objects.
490 Methods are called using the dot operator to specify the method
491 against the object variable.  Additional arguments can be specified
492 as with subroutines.
493 .PP
494 .Vb 1
495 \&    use CGI;
496 .Ve
497 .PP
498 .Vb 4
499 \&    my $vars = {
500 \&        # hard coded CGI params for purpose of example
501 \&        cgi  => CGI\->new('mode=submit&debug=1'),
502 \&    };
503 .Ve
504 .PP
505 Example template:
506 .PP
507 .Vb 3
508 \&    [% FOREACH p IN cgi.param %]     # returns list of param keys
509 \&    [% p %] => [% cgi.param(p) %]   # fetch each param value
510 \&    [% END %]
511 .Ve
512 .PP
513 Generated output:
514 .PP
515 .Vb 2
516 \&    mode => submit
517 \&    debug => 1
518 .Ve
519 .PP
520 Object methods can also be called as lvalues.  That is, they can appear on 
521 the left side of an assignment.  The method will be called passing the 
522 assigning value as an argument.  
523 .PP
524 .Vb 1
525 \&    [% myobj.method = 10 %]
526 .Ve
527 .PP
528 equivalent to:
529 .PP
530 .Vb 1
531 \&    [% myobj.method(10) %]
532 .Ve
533 .Sh "Passing Parameters and Returning Values"
534 .IX Subsection "Passing Parameters and Returning Values"
535 Subroutines and methods will be passed any arguments specified in the
536 template.  Any template variables in the argument list will first be
537 evaluated and their resultant values passed to the code.
538 .PP
539 .Vb 3
540 \&    my $vars = {
541 \&        mycode => sub { return 'received ' . join(', ', @_) },
542 \&    };
543 .Ve
544 .PP
545 template:
546 .PP
547 .Vb 2
548 \&    [% foo = 10 %]
549 \&    [% mycode(foo, 20) %]       # received 10, 20
550 .Ve
551 .PP
552 Named parameters may also be specified.  These are automatically collected
553 into a single hash array which is passed by reference as the \fBlast\fR 
554 parameter to the sub\-routine.  Named parameters can be specified using
555 either \f(CW\*(C`=>\*(C'\fR or \f(CW\*(C`=\*(C'\fR and can appear anywhere in the argument list.
556 .PP
557 .Vb 3
558 \&    my $vars = {
559 \&        myjoin => \e&myjoin,
560 \&    };
561 .Ve
562 .PP
563 .Vb 5
564 \&    sub myjoin {
565 \&        # look for hash ref as last argument
566 \&        my $params = ref $_[\-1] eq 'HASH' ? pop : { };
567 \&        return join($params\->{ joint } || ' + ', @_);
568 \&    }
569 .Ve
570 .PP
571 Example template:
572 .PP
573 .Vb 3
574 \&    [% myjoin(10, 20, 30) %]
575 \&    [% myjoin(10, 20, 30, joint = ' \- ' %]
576 \&    [% myjoin(joint => ' * ', 10, 20, 30 %]
577 .Ve
578 .PP
579 Generated output:
580 .PP
581 .Vb 3
582 \&    10 + 20 + 30
583 \&    10 \- 20 \- 30
584 \&    10 * 20 * 30
585 .Ve
586 .PP
587 Parenthesised parameters may be added to any element of a variable,
588 not just those that are bound to code or object methods.  At present,
589 parameters will be ignored if the variable isn't \*(L"callable\*(R" but are 
590 supported for future extensions.  Think of them as \*(L"hints\*(R" to that 
591 variable, rather than just arguments passed to a function.
592 .PP
593 .Vb 2
594 \&    [% r = 'Romeo' %]
595 \&    [% r(100, 99, s, t, v) %]       # outputs "Romeo"
596 .Ve
597 .PP
598 User code should return a value for the variable it represents. This
599 can be any of the Perl data types described above: a scalar, or
600 reference to a list, hash, subroutine or object.  Where code returns a
601 list of multiple values the items will automatically be folded into a
602 list reference which can be accessed as per normal.
603 .PP
604 .Vb 5
605 \&    my $vars = {
606 \&        # either is OK, first is recommended
607 \&        items1 => sub { return [ 'foo', 'bar', 'baz' ] },
608 \&        items2 => sub { return ( 'foo', 'bar', 'baz' ) },
609 \&    };
610 .Ve
611 .PP
612 Example template:
613 .PP
614 .Vb 3
615 \&    [% FOREACH i IN items1 %]
616 \&       ...
617 \&    [% END %]
618 .Ve
619 .PP
620 .Vb 3
621 \&    [% FOREACH i IN items2 %]
622 \&       ...
623 \&    [% END %]
624 .Ve
625 .Sh "Error Handling"
626 .IX Subsection "Error Handling"
627 Errors can be reported from user code by calling \f(CW\*(C`die()\*(C'\fR.  Errors raised
628 in this way are caught by the Template Toolkit and converted to
629 structured exceptions which can be handled from within the template.
630 A reference to the exception object is then available as the \f(CW\*(C`error\*(C'\fR
631 variable.
632 .PP
633 .Vb 5
634 \&    my $vars = {
635 \&        barf => sub { 
636 \&            die "a sick error has occurred\en";
637 \&        },
638 \&    };
639 .Ve
640 .PP
641 Example template:
642 .PP
643 .Vb 5
644 \&    [% TRY %]
645 \&       [% barf %]       # calls sub which throws error via die()
646 \&    [% CATCH %]
647 \&       [% error.info %]     # outputs "a sick error has occurred\en"
648 \&    [% END %]
649 .Ve
650 .PP
651 Error messages thrown via \f(CW\*(C`die()\*(C'\fR are converted to exceptions of type
652 \&\f(CW\*(C`undef\*(C'\fR (the literal string \*(L"undef\*(R" rather than the undefined value).
653 Exceptions of user-defined types can be thrown by calling \f(CW\*(C`die()\*(C'\fR with 
654 a reference to a Template::Exception object.
655 .PP
656 .Vb 1
657 \&    use Template::Exception;
658 .Ve
659 .PP
660 .Vb 6
661 \&    my $vars = {
662 \&        login => sub { 
663 \&            ...do something...
664 \&            die Template::Exception\->new( badpwd => 'password too silly' );
665 \&        },
666 \&    };
667 .Ve
668 .PP
669 Example template:
670 .PP
671 .Vb 7
672 \&    [% TRY %]
673 \&       [% login %]
674 \&    [% CATCH badpwd %]
675 \&       Bad password: [% error.info %]
676 \&    [% CATCH %]
677 \&       Some other '[% error.type %]' error: [% error.info %]
678 \&    [% END %]
679 .Ve
680 .PP
681 The exception types \f(CW\*(C`stop\*(C'\fR and \f(CW\*(C`return\*(C'\fR are used to implement the 
682 \&\f(CW\*(C`STOP\*(C'\fR and \f(CW\*(C`RETURN\*(C'\fR directives.  Throwing an exception as:
683 .PP
684 .Vb 1
685 \&    die (Template::Exception\->new('stop'));
686 .Ve
687 .PP
688 has the same effect as the directive:
689 .PP
690 .Vb 1
691 \&    [% STOP %]
692 .Ve
693 .SH "Virtual Methods"
694 .IX Header "Virtual Methods"
695 The Template Toolkit implements a number of \*(L"virtual methods\*(R" which 
696 can be applied to scalars, hashes or lists.  For example:
697 .PP
698 .Vb 2
699 \&    [% mylist = [ 'foo', 'bar', 'baz' ] %]
700 \&    [% newlist = mylist.sort %]
701 .Ve
702 .PP
703 Here \f(CW\*(C`mylist\*(C'\fR is a regular reference to a list, and 'sort' is 
704 a virtual method that returns a new list of the items in sorted 
705 order.  You can chain multiple virtual methods together.  For
706 example:
707 .PP
708 .Vb 1
709 \&    [% mylist.sort.join(', ') %]
710 .Ve
711 .PP
712 Here the \f(CW\*(C`join\*(C'\fR virtual method is called to join the sorted list into
713 a single string, generating the following output:
714 .PP
715 .Vb 1
716 \&    bar, baz, foo
717 .Ve
718 .PP
719 See Template::Manual::VMethods for details of all the virtual 
720 methods available.
721 .SH "Variable Interpolation"
722 .IX Header "Variable Interpolation"
723 The Template Toolkit uses \f(CW\*(C`$\*(C'\fR consistently to indicate that a variable
724 should be interpolated in position.  Most frequently, you see this in 
725 double-quoted strings:
726 .PP
727 .Vb 1
728 \&    [% fullname = "$honorific $firstname $surname" %]
729 .Ve
730 .PP
731 Or embedded in plain text when the \f(CW\*(C`INTERPOLATE\*(C'\fR option is set:
732 .PP
733 .Vb 1
734 \&    Dear $honorific $firstname $surname,
735 .Ve
736 .PP
737 The same rules apply within directives.  If a variable is prefixed
738 with a \f(CW\*(C`$\*(C'\fR then it is replaced with its value before being used.  The
739 most common use is to retrieve an element from a hash where the key is
740 stored in a variable.
741 .PP
742 .Vb 2
743 \&    [% uid = 'abw' %]
744 \&    [% users.$uid %]         # same as 'userlist.abw'
745 .Ve
746 .PP
747 Curly braces can be used to delimit interpolated variable names where
748 necessary.
749 .PP
750 .Vb 1
751 \&    [% users.${me.id}.name %]
752 .Ve
753 .PP
754 Directives such as \f(CW\*(C`INCLUDE\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR, etc., that accept a template name
755 as the first argument, will automatically quote it for convenience.
756 .PP
757 .Vb 1
758 \&    [% INCLUDE foo/bar.txt %]
759 .Ve
760 .PP
761 The above example is equivalent to:
762 .PP
763 .Vb 1
764 \&    [% INCLUDE "foo/bar.txt" %]
765 .Ve
766 .PP
767 To \f(CW\*(C`INCLUDE\*(C'\fR a template whose name is stored in a variable, simply
768 prefix the variable name with \f(CW\*(C`$\*(C'\fR to have it interpolated.
769 .PP
770 .Vb 2
771 \&    [% myfile = 'header' %]
772 \&    [% INCLUDE $myfile %]
773 .Ve
774 .PP
775 This is equivalent to:
776 .PP
777 .Vb 1
778 \&    [% INCLUDE header %]
779 .Ve
780 .PP
781 Note also that a variable containing a reference to a Template::Document
782 object can also be processed in this way.
783 .PP
784 .Vb 3
785 \&    my $vars = {
786 \&        header => Template::Document\->new({ ... }),
787 \&    };
788 .Ve
789 .PP
790 Example template:
791 .PP
792 .Vb 1
793 \&    [% INCLUDE $header %]
794 .Ve
795 .SH "Local and Global Variables"
796 .IX Header "Local and Global Variables"
797 Any simple variables that you create, or any changes you make to
798 existing variables, will only persist while the template is being
799 processed.  The top-level variable hash is copied before processing
800 begins and any changes to variables are made in this copy, leaving the
801 original intact.  
802 .PP
803 The same thing happens when you \f(CW\*(C`INCLUDE\*(C'\fR another template. The current
804 namespace hash is cloned to prevent any variable changes made in the included
805 template from interfering with existing variables. The \f(CW\*(C`PROCESS\*(C'\fR option bypasses
806 the localisation step altogether making it slightly faster, but requiring
807 greater attention to the possibility of side effects caused by creating or
808 changing any variables within the processed template.
809 .PP
810 .Vb 3
811 \&    [% BLOCK change_name %]
812 \&       [% name = 'bar' %]
813 \&    [% END %]
814 .Ve
815 .PP
816 .Vb 5
817 \&    [% name = 'foo' %] 
818 \&    [% INCLUDE change_name %]
819 \&    [% name %]              # foo
820 \&    [% PROCESS change_name %]
821 \&    [% name %]              # bar
822 .Ve
823 .PP
824 Dotted compound variables behave slightly differently because the
825 localisation process is only skin deep.  The current variable
826 namespace hash is copied, but no attempt is made to perform a
827 deep-copy of other structures within it (hashes, arrays, objects,
828 etc).  A variable referencing a hash, for example, will be copied to
829 create a new reference but which points to the same hash.  Thus, the
830 general rule is that simple variables (undotted variables) are
831 localised, but existing complex structures (dotted variables) are not.
832 .PP
833 .Vb 4
834 \&    [% BLOCK all_change %]
835 \&       [% x = 20 %]         # changes copy
836 \&       [% y.z = 'zulu' %]       # changes original
837 \&    [% END %]
838 .Ve
839 .PP
840 .Vb 6
841 \&    [% x = 10
842 \&       y = { z => 'zebra' }
843 \&    %]
844 \&    [% INCLUDE all_change %]
845 \&    [% x %]             # still '10'
846 \&    [% y.z %]               # now 'zulu'
847 .Ve
848 .PP
849 If you create a complex structure such as a hash or list reference
850 within a local template context then it will cease to exist when 
851 the template is finished processing.  
852 .PP
853 .Vb 5
854 \&    [% BLOCK new_stuff %]
855 \&       [% # define a new 'y' hash array in local context
856 \&          y = { z => 'zulu' }
857 \&       %]
858 \&    [% END %]
859 .Ve
860 .PP
861 .Vb 4
862 \&    [% x = 10 %]
863 \&    [% INCLUDE new_stuff %]
864 \&    [% x %]             # outputs '10'
865 \&    [% y %]             # nothing, y is undefined
866 .Ve
867 .PP
868 Similarly, if you update an element of a compound variable which
869 \&\fIdoesn't\fR already exists then a hash will be created automatically
870 and deleted again at the end of the block.
871 .PP
872 .Vb 3
873 \&    [% BLOCK new_stuff %]
874 \&       [% y.z = 'zulu' %]
875 \&    [% END %]
876 .Ve
877 .PP
878 However, if the hash \fIdoes\fR already exist then you will modify the
879 original with permanent effect.  To avoid potential confusion, it is
880 recommended that you don't update elements of complex variables from
881 within blocks or templates included by another.
882 .PP
883 If you want to create or update truly global variables then you can 
884 use the 'global' namespace.  This is a hash array automatically created
885 in the top-level namespace which all templates, localised or otherwise
886 see the same reference to.  Changes made to variables within this
887 hash are visible across all templates.
888 .PP
889 .Vb 1
890 \&    [% global.version = 123 %]
891 .Ve
892 .SH "Compile Time Constant Folding"
893 .IX Header "Compile Time Constant Folding"
894 In addition to variables that get resolved each time a template is
895 processed, you can also define variables that get resolved just once
896 when the template is compiled.  This generally results in templates
897 processing faster because there is less work to be done.
898 .PP
899 To define compile-time constants, specify a \f(CW\*(C`CONSTANTS\*(C'\fR hash as a
900 constructor item as per \f(CW\*(C`VARIABLES\*(C'\fR.  The \f(CW\*(C`CONSTANTS\*(C'\fR hash can contain any
901 kind of complex, nested, or dynamic data structures, just like regular
902 variables.
903 .PP
904 .Vb 13
905 \&    my $tt = Template\->new({
906 \&        CONSTANTS => {
907 \&            version => 3.14,
908 \&            release => 'skyrocket',
909 \&            col     => {
910 \&                back => '#ffffff',
911 \&                fore => '#000000',
912 \&            },
913 \&            myobj => My::Object\->new(),
914 \&            mysub => sub { ... },
915 \&            joint => ', ',
916 \&        },
917 \&    });
918 .Ve
919 .PP
920 Within a template, you access these variables using the \f(CW\*(C`constants\*(C'\fR
921 namespace prefix.
922 .PP
923 .Vb 2
924 \&    Version [% constants.version %] ([% constants.release %])
925 \&    Background: [% constants.col.back %]
926 .Ve
927 .PP
928 When the template is compiled, these variable references are replaced
929 with the corresponding value.  No further variable lookup is then 
930 required when the template is processed.
931 .PP
932 You can call subroutines, object methods, and even virtual methods on
933 constant variables.
934 .PP
935 .Vb 3
936 \&    [% constants.mysub(10, 20) %]
937 \&    [% constants.myobj(30, 40) %]
938 \&    [% constants.col.keys.sort.join(', ') %]
939 .Ve
940 .PP
941 One important proviso is that any arguments you pass to subroutines
942 or methods must also be literal values or compile time constants.
943 .PP
944 For example, these are both fine:
945 .PP
946 .Vb 2
947 \&    # literal argument
948 \&    [% constants.col.keys.sort.join(', ') %]
949 .Ve
950 .PP
951 .Vb 2
952 \&    # constant argument
953 \&    [% constants.col.keys.sort.join(constants.joint) %]
954 .Ve
955 .PP
956 But this next example will raise an error at parse time because
957 \&\f(CW\*(C`joint\*(C'\fR is a runtime variable and cannot be determined at compile
958 time.
959 .PP
960 .Vb 2
961 \&    # ERROR: runtime variable argument!
962 \&    [% constants.col.keys.sort.join(joint) %]
963 .Ve
964 .PP
965 The \f(CW\*(C`CONSTANTS_NAMESPACE\*(C'\fR option can be used to provide a different 
966 namespace prefix for constant variables.  For example:
967 .PP
968 .Vb 7
969 \&    my $tt = Template\->new({
970 \&        CONSTANTS => {
971 \&            version => 3.14,
972 \&            # ...etc...
973 \&        },
974 \&        CONSTANTS_NAMESPACE => 'const',
975 \&    });
976 .Ve
977 .PP
978 Constants would then be referenced in templates as:
979 .PP
980 .Vb 1
981 \&    [% const.version %]
982 .Ve
983 .SH "Special Variables"
984 .IX Header "Special Variables"
985 A number of special variables are automatically defined by the Template 
986 Toolkit.
987 .Sh "template"
988 .IX Subsection "template"
989 The \f(CW\*(C`template\*(C'\fR variable contains a reference to the main template being
990 processed, in the form of a Template::Document object. This variable is
991 correctly defined within \f(CW\*(C`PRE_PROCESS\*(C'\fR, \f(CW\*(C`PROCESS\*(C'\fR and \f(CW\*(C`POST_PROCESS\*(C'\fR
992 templates, allowing standard headers, footers, etc., to access metadata items
993 from the main template. The \f(CW\*(C`name\*(C'\fR and \f(CW\*(C`modtime\*(C'\fR metadata items are
994 automatically provided, giving the template name and modification time in
995 seconds since the epoch.
996 .PP
997 Note that the \f(CW\*(C`template\*(C'\fR variable always references the top-level
998 template, even when processing other template components via \f(CW\*(C`INCLUDE\*(C'\fR,
999 \&\f(CW\*(C`PROCESS\*(C'\fR, etc.
1000 .Sh "component"
1001 .IX Subsection "component"
1002 The \f(CW\*(C`component\*(C'\fR variable is like \f(CW\*(C`template\*(C'\fR but always contains a
1003 reference to the current, innermost template component being processed.
1004 In the main template, the \f(CW\*(C`template\*(C'\fR and \f(CW\*(C`component\*(C'\fR variable will 
1005 reference the same Template::Document object.  In any other template
1006 component called from the main template, the \f(CW\*(C`template\*(C'\fR variable 
1007 will remain unchanged, but \f(CW\*(C`component\*(C'\fR will contain a new reference
1008 to the current component.
1009 .PP
1010 This example should demonstrate the difference:
1011 .PP
1012 .Vb 2
1013 \&    $template\->process('foo')
1014 \&        || die $template\->error(), "\en";
1015 .Ve
1016 .PP
1017 \&\fIfoo\fR template:
1018 .PP
1019 .Vb 3
1020 \&    [% template.name %]         # foo
1021 \&    [% component.name %]        # foo
1022 \&    [% PROCESS footer %]
1023 .Ve
1024 .PP
1025 \&\fIfooter\fR template:
1026 .PP
1027 .Vb 2
1028 \&    [% template.name %]         # foo
1029 \&    [% component.name %]        # footer
1030 .Ve
1031 .PP
1032 Additionally, the \f(CW\*(C`component\*(C'\fR variable has two special fields:
1033 \&\f(CW\*(C`caller\*(C'\fR and \f(CW\*(C`callers\*(C'\fR.  \f(CW\*(C`caller\*(C'\fR contains the name of the template
1034 that called the current template (or undef if the values of \f(CW\*(C`template\*(C'\fR
1035 and \f(CW\*(C`component\*(C'\fR are the same).  \f(CW\*(C`callers\*(C'\fR contains a reference to a
1036 list of all the templates that have been called on the road to calling
1037 the current component template (like a call stack), with the
1038 outer-most template first.
1039 .PP
1040 Here's an example:
1041 .PP
1042 \&\fIouter.tt2\fR template:
1043 .PP
1044 .Vb 4
1045 \&    [% component.name %]        # 'outer.tt2'
1046 \&    [% component.caller %]      # undef
1047 \&    [% component.callers %]     # undef
1048 \&    [% PROCESS 'middle.tt2' %]
1049 .Ve
1050 .PP
1051 \&\fImiddle.tt2\fR template:
1052 .PP
1053 .Vb 4
1054 \&    [% component.name %]        # 'middle.tt2'
1055 \&    [% component.caller %]      # 'outer.tt2'
1056 \&    [% component.callers %]     # [ 'outer.tt2' ]
1057 \&    [% PROCESS 'inner.tt2' %]
1058 .Ve
1059 .PP
1060 \&\fIinner.tt2\fR template:
1061 .PP
1062 .Vb 3
1063 \&    [% component.name %]        # 'inner.tt2'
1064 \&    [% component.caller %]      # 'middle.tt2'
1065 \&    [% component.callers %]     # [ 'outer.tt2', 'middle.tt2' ]
1066 .Ve
1067 .Sh "loop"
1068 .IX Subsection "loop"
1069 Within a \f(CW\*(C`FOREACH\*(C'\fR loop, the \f(CW\*(C`loop\*(C'\fR variable references the 
1070 Template::Iterator object responsible for controlling the loop.
1071 .PP
1072 .Vb 4
1073 \&    [% FOREACH item = [ 'foo', 'bar', 'baz' ] \-%]
1074 \&       [% "Items:\en" IF loop.first \-%]
1075 \&       [% loop.count %]/[% loop.size %]: [% item %]
1076 \&    [% END %]
1077 .Ve
1078 .Sh "error"
1079 .IX Subsection "error"
1080 Within a \f(CW\*(C`CATCH\*(C'\fR block, the \f(CW\*(C`error\*(C'\fR variable contains a reference to the 
1081 Template::Exception object thrown from within the \f(CW\*(C`TRY\*(C'\fR block.  The 
1082 \&\f(CW\*(C`type\*(C'\fR and \f(CW\*(C`info\*(C'\fR methods can be called or the variable itself can 
1083 be printed for automatic stringification into a message of the form
1084 "\f(CW\*(C`$type error \- $info\*(C'\fR".  See Template::Exception for further details.
1085 .PP
1086 .Vb 5
1087 \&    [% TRY %]
1088 \&       ...
1089 \&    [% CATCH %]
1090 \&       [% error %]
1091 \&    [% END %]
1092 .Ve
1093 .Sh "content"
1094 .IX Subsection "content"
1095 The \f(CW\*(C`WRAPPER\*(C'\fR method captures the output from a template block and then 
1096 includes a named template, passing the captured output as the 'content'
1097 variable.
1098 .PP
1099 .Vb 4
1100 \&    [% WRAPPER box %]
1101 \&    Be not afeard; the isle is full of noises,
1102 \&    Sounds and sweet airs, that give delight and hurt not.
1103 \&    [% END %]
1104 .Ve
1105 .PP
1106 .Vb 5
1107 \&    [% BLOCK box %]
1108 \&    <blockquote class="prose">
1109 \&      [% content %]
1110 \&    </blockquote>
1111 \&    [% END %]
1112 .Ve
1113 .SH "Compound Variables"
1114 .IX Header "Compound Variables"
1115 Compound 'dotted' variables may contain any number of separate
1116 elements.  Each element may evaluate to any of the permitted variable
1117 types and the processor will then correctly use this value to evaluate
1118 the rest of the variable.  Arguments may be passed to any of the
1119 intermediate elements.
1120 .PP
1121 .Vb 1
1122 \&    [% myorg.people.sort('surname').first.fullname %]
1123 .Ve
1124 .PP
1125 Intermediate variables may be used and will behave entirely as expected.
1126 .PP
1127 .Vb 2
1128 \&    [% sorted = myorg.people.sort('surname') %]
1129 \&    [% sorted.first.fullname %]
1130 .Ve
1131 .PP
1132 This simplified dotted notation has the benefit of hiding the
1133 implementation details of your data.  For example, you could implement
1134 a data structure as a hash array one day and then change it to an
1135 object the next without requiring any change to the templates.