xref: /freebsd/crypto/openssl/external/perl/Text-Template-1.56/lib/Text/Template.pm (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert# -*- perl -*-
2*e0c4386eSCy Schubert# Text::Template.pm
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Fill in `templates'
5*e0c4386eSCy Schubert#
6*e0c4386eSCy Schubert# Copyright 2013 M. J. Dominus.
7*e0c4386eSCy Schubert# You may copy and distribute this program under the
8*e0c4386eSCy Schubert# same terms as Perl itself.
9*e0c4386eSCy Schubert# If in doubt, write to mjd-perl-template+@plover.com for a license.
10*e0c4386eSCy Schubert#
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubertpackage Text::Template;
13*e0c4386eSCy Schubert$Text::Template::VERSION = '1.56';
14*e0c4386eSCy Schubert# ABSTRACT: Expand template text with embedded Perl
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubertuse strict;
17*e0c4386eSCy Schubertuse warnings;
18*e0c4386eSCy Schubert
19*e0c4386eSCy Schubertrequire 5.008;
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubertuse base 'Exporter';
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubertour @EXPORT_OK = qw(fill_in_file fill_in_string TTerror);
24*e0c4386eSCy Schubertour $ERROR;
25*e0c4386eSCy Schubert
26*e0c4386eSCy Schubertmy %GLOBAL_PREPEND = ('Text::Template' => '');
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubertsub Version {
29*e0c4386eSCy Schubert    $Text::Template::VERSION;
30*e0c4386eSCy Schubert}
31*e0c4386eSCy Schubert
32*e0c4386eSCy Schubertsub _param {
33*e0c4386eSCy Schubert    my ($k, %h) = @_;
34*e0c4386eSCy Schubert
35*e0c4386eSCy Schubert    for my $kk ($k, "\u$k", "\U$k", "-$k", "-\u$k", "-\U$k") {
36*e0c4386eSCy Schubert        return $h{$kk} if exists $h{$kk};
37*e0c4386eSCy Schubert    }
38*e0c4386eSCy Schubert
39*e0c4386eSCy Schubert    return undef;
40*e0c4386eSCy Schubert}
41*e0c4386eSCy Schubert
42*e0c4386eSCy Schubertsub always_prepend {
43*e0c4386eSCy Schubert    my $pack = shift;
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert    my $old = $GLOBAL_PREPEND{$pack};
46*e0c4386eSCy Schubert
47*e0c4386eSCy Schubert    $GLOBAL_PREPEND{$pack} = shift;
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert    $old;
50*e0c4386eSCy Schubert}
51*e0c4386eSCy Schubert
52*e0c4386eSCy Schubert{
53*e0c4386eSCy Schubert    my %LEGAL_TYPE;
54*e0c4386eSCy Schubert
55*e0c4386eSCy Schubert    BEGIN {
56*e0c4386eSCy Schubert        %LEGAL_TYPE = map { $_ => 1 } qw(FILE FILEHANDLE STRING ARRAY);
57*e0c4386eSCy Schubert    }
58*e0c4386eSCy Schubert
59*e0c4386eSCy Schubert    sub new {
60*e0c4386eSCy Schubert        my ($pack, %a) = @_;
61*e0c4386eSCy Schubert
62*e0c4386eSCy Schubert        my $stype     = uc(_param('type', %a) || "FILE");
63*e0c4386eSCy Schubert        my $source    = _param('source', %a);
64*e0c4386eSCy Schubert        my $untaint   = _param('untaint', %a);
65*e0c4386eSCy Schubert        my $prepend   = _param('prepend', %a);
66*e0c4386eSCy Schubert        my $alt_delim = _param('delimiters', %a);
67*e0c4386eSCy Schubert        my $broken    = _param('broken', %a);
68*e0c4386eSCy Schubert        my $encoding  = _param('encoding', %a);
69*e0c4386eSCy Schubert
70*e0c4386eSCy Schubert        unless (defined $source) {
71*e0c4386eSCy Schubert            require Carp;
72*e0c4386eSCy Schubert            Carp::croak("Usage: $ {pack}::new(TYPE => ..., SOURCE => ...)");
73*e0c4386eSCy Schubert        }
74*e0c4386eSCy Schubert
75*e0c4386eSCy Schubert        unless ($LEGAL_TYPE{$stype}) {
76*e0c4386eSCy Schubert            require Carp;
77*e0c4386eSCy Schubert            Carp::croak("Illegal value `$stype' for TYPE parameter");
78*e0c4386eSCy Schubert        }
79*e0c4386eSCy Schubert
80*e0c4386eSCy Schubert        my $self = {
81*e0c4386eSCy Schubert            TYPE     => $stype,
82*e0c4386eSCy Schubert            PREPEND  => $prepend,
83*e0c4386eSCy Schubert            UNTAINT  => $untaint,
84*e0c4386eSCy Schubert            BROKEN   => $broken,
85*e0c4386eSCy Schubert            ENCODING => $encoding,
86*e0c4386eSCy Schubert            (defined $alt_delim ? (DELIM => $alt_delim) : ())
87*e0c4386eSCy Schubert        };
88*e0c4386eSCy Schubert
89*e0c4386eSCy Schubert        # Under 5.005_03, if any of $stype, $prepend, $untaint, or $broken
90*e0c4386eSCy Schubert        # are tainted, all the others become tainted too as a result of
91*e0c4386eSCy Schubert        # sharing the expression with them.  We install $source separately
92*e0c4386eSCy Schubert        # to prevent it from acquiring a spurious taint.
93*e0c4386eSCy Schubert        $self->{SOURCE} = $source;
94*e0c4386eSCy Schubert
95*e0c4386eSCy Schubert        bless $self => $pack;
96*e0c4386eSCy Schubert        return unless $self->_acquire_data;
97*e0c4386eSCy Schubert
98*e0c4386eSCy Schubert        $self;
99*e0c4386eSCy Schubert    }
100*e0c4386eSCy Schubert}
101*e0c4386eSCy Schubert
102*e0c4386eSCy Schubert# Convert template objects of various types to type STRING,
103*e0c4386eSCy Schubert# in which the template data is embedded in the object itself.
104*e0c4386eSCy Schubertsub _acquire_data {
105*e0c4386eSCy Schubert    my $self = shift;
106*e0c4386eSCy Schubert
107*e0c4386eSCy Schubert    my $type = $self->{TYPE};
108*e0c4386eSCy Schubert
109*e0c4386eSCy Schubert    if ($type eq 'STRING') {
110*e0c4386eSCy Schubert        # nothing necessary
111*e0c4386eSCy Schubert    }
112*e0c4386eSCy Schubert    elsif ($type eq 'FILE') {
113*e0c4386eSCy Schubert        my $data = _load_text($self->{SOURCE});
114*e0c4386eSCy Schubert        unless (defined $data) {
115*e0c4386eSCy Schubert
116*e0c4386eSCy Schubert            # _load_text already set $ERROR
117*e0c4386eSCy Schubert            return undef;
118*e0c4386eSCy Schubert        }
119*e0c4386eSCy Schubert
120*e0c4386eSCy Schubert        if ($self->{UNTAINT} && _is_clean($self->{SOURCE})) {
121*e0c4386eSCy Schubert            _unconditionally_untaint($data);
122*e0c4386eSCy Schubert        }
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert        if (defined $self->{ENCODING}) {
125*e0c4386eSCy Schubert            require Encode;
126*e0c4386eSCy Schubert            $data = Encode::decode($self->{ENCODING}, $data, &Encode::FB_CROAK);
127*e0c4386eSCy Schubert        }
128*e0c4386eSCy Schubert
129*e0c4386eSCy Schubert        $self->{TYPE}     = 'STRING';
130*e0c4386eSCy Schubert        $self->{FILENAME} = $self->{SOURCE};
131*e0c4386eSCy Schubert        $self->{SOURCE}   = $data;
132*e0c4386eSCy Schubert    }
133*e0c4386eSCy Schubert    elsif ($type eq 'ARRAY') {
134*e0c4386eSCy Schubert        $self->{TYPE} = 'STRING';
135*e0c4386eSCy Schubert        $self->{SOURCE} = join '', @{ $self->{SOURCE} };
136*e0c4386eSCy Schubert    }
137*e0c4386eSCy Schubert    elsif ($type eq 'FILEHANDLE') {
138*e0c4386eSCy Schubert        $self->{TYPE} = 'STRING';
139*e0c4386eSCy Schubert        local $/;
140*e0c4386eSCy Schubert        my $fh   = $self->{SOURCE};
141*e0c4386eSCy Schubert        my $data = <$fh>;             # Extra assignment avoids bug in Solaris perl5.00[45].
142*e0c4386eSCy Schubert        if ($self->{UNTAINT}) {
143*e0c4386eSCy Schubert            _unconditionally_untaint($data);
144*e0c4386eSCy Schubert        }
145*e0c4386eSCy Schubert        $self->{SOURCE} = $data;
146*e0c4386eSCy Schubert    }
147*e0c4386eSCy Schubert    else {
148*e0c4386eSCy Schubert        # This should have been caught long ago, so it represents a
149*e0c4386eSCy Schubert        # drastic `can't-happen' sort of failure
150*e0c4386eSCy Schubert        my $pack = ref $self;
151*e0c4386eSCy Schubert        die "Can only acquire data for $pack objects of subtype STRING, but this is $type; aborting";
152*e0c4386eSCy Schubert    }
153*e0c4386eSCy Schubert
154*e0c4386eSCy Schubert    $self->{DATA_ACQUIRED} = 1;
155*e0c4386eSCy Schubert}
156*e0c4386eSCy Schubert
157*e0c4386eSCy Schubertsub source {
158*e0c4386eSCy Schubert    my $self = shift;
159*e0c4386eSCy Schubert
160*e0c4386eSCy Schubert    $self->_acquire_data unless $self->{DATA_ACQUIRED};
161*e0c4386eSCy Schubert
162*e0c4386eSCy Schubert    return $self->{SOURCE};
163*e0c4386eSCy Schubert}
164*e0c4386eSCy Schubert
165*e0c4386eSCy Schubertsub set_source_data {
166*e0c4386eSCy Schubert    my ($self, $newdata, $type) = @_;
167*e0c4386eSCy Schubert
168*e0c4386eSCy Schubert    $self->{SOURCE}        = $newdata;
169*e0c4386eSCy Schubert    $self->{DATA_ACQUIRED} = 1;
170*e0c4386eSCy Schubert    $self->{TYPE}          = $type || 'STRING';
171*e0c4386eSCy Schubert
172*e0c4386eSCy Schubert    1;
173*e0c4386eSCy Schubert}
174*e0c4386eSCy Schubert
175*e0c4386eSCy Schubertsub compile {
176*e0c4386eSCy Schubert    my $self = shift;
177*e0c4386eSCy Schubert
178*e0c4386eSCy Schubert    return 1 if $self->{TYPE} eq 'PREPARSED';
179*e0c4386eSCy Schubert
180*e0c4386eSCy Schubert    return undef unless $self->_acquire_data;
181*e0c4386eSCy Schubert
182*e0c4386eSCy Schubert    unless ($self->{TYPE} eq 'STRING') {
183*e0c4386eSCy Schubert        my $pack = ref $self;
184*e0c4386eSCy Schubert
185*e0c4386eSCy Schubert        # This should have been caught long ago, so it represents a
186*e0c4386eSCy Schubert        # drastic `can't-happen' sort of failure
187*e0c4386eSCy Schubert        die "Can only compile $pack objects of subtype STRING, but this is $self->{TYPE}; aborting";
188*e0c4386eSCy Schubert    }
189*e0c4386eSCy Schubert
190*e0c4386eSCy Schubert    my @tokens;
191*e0c4386eSCy Schubert    my $delim_pats = shift() || $self->{DELIM};
192*e0c4386eSCy Schubert
193*e0c4386eSCy Schubert    my ($t_open, $t_close) = ('{', '}');
194*e0c4386eSCy Schubert    my $DELIM;    # Regex matches a delimiter if $delim_pats
195*e0c4386eSCy Schubert
196*e0c4386eSCy Schubert    if (defined $delim_pats) {
197*e0c4386eSCy Schubert        ($t_open, $t_close) = @$delim_pats;
198*e0c4386eSCy Schubert        $DELIM = "(?:(?:\Q$t_open\E)|(?:\Q$t_close\E))";
199*e0c4386eSCy Schubert        @tokens = split /($DELIM|\n)/, $self->{SOURCE};
200*e0c4386eSCy Schubert    }
201*e0c4386eSCy Schubert    else {
202*e0c4386eSCy Schubert        @tokens = split /(\\\\(?=\\*[{}])|\\[{}]|[{}\n])/, $self->{SOURCE};
203*e0c4386eSCy Schubert    }
204*e0c4386eSCy Schubert
205*e0c4386eSCy Schubert    my $state  = 'TEXT';
206*e0c4386eSCy Schubert    my $depth  = 0;
207*e0c4386eSCy Schubert    my $lineno = 1;
208*e0c4386eSCy Schubert    my @content;
209*e0c4386eSCy Schubert    my $cur_item = '';
210*e0c4386eSCy Schubert    my $prog_start;
211*e0c4386eSCy Schubert
212*e0c4386eSCy Schubert    while (@tokens) {
213*e0c4386eSCy Schubert        my $t = shift @tokens;
214*e0c4386eSCy Schubert
215*e0c4386eSCy Schubert        next if $t eq '';
216*e0c4386eSCy Schubert
217*e0c4386eSCy Schubert        if ($t eq $t_open) {    # Brace or other opening delimiter
218*e0c4386eSCy Schubert            if ($depth == 0) {
219*e0c4386eSCy Schubert                push @content, [ $state, $cur_item, $lineno ] if $cur_item ne '';
220*e0c4386eSCy Schubert                $cur_item   = '';
221*e0c4386eSCy Schubert                $state      = 'PROG';
222*e0c4386eSCy Schubert                $prog_start = $lineno;
223*e0c4386eSCy Schubert            }
224*e0c4386eSCy Schubert            else {
225*e0c4386eSCy Schubert                $cur_item .= $t;
226*e0c4386eSCy Schubert            }
227*e0c4386eSCy Schubert            $depth++;
228*e0c4386eSCy Schubert        }
229*e0c4386eSCy Schubert        elsif ($t eq $t_close) {    # Brace or other closing delimiter
230*e0c4386eSCy Schubert            $depth--;
231*e0c4386eSCy Schubert            if ($depth < 0) {
232*e0c4386eSCy Schubert                $ERROR = "Unmatched close brace at line $lineno";
233*e0c4386eSCy Schubert                return undef;
234*e0c4386eSCy Schubert            }
235*e0c4386eSCy Schubert            elsif ($depth == 0) {
236*e0c4386eSCy Schubert                push @content, [ $state, $cur_item, $prog_start ] if $cur_item ne '';
237*e0c4386eSCy Schubert                $state    = 'TEXT';
238*e0c4386eSCy Schubert                $cur_item = '';
239*e0c4386eSCy Schubert            }
240*e0c4386eSCy Schubert            else {
241*e0c4386eSCy Schubert                $cur_item .= $t;
242*e0c4386eSCy Schubert            }
243*e0c4386eSCy Schubert        }
244*e0c4386eSCy Schubert        elsif (!$delim_pats && $t eq '\\\\') {    # precedes \\\..\\\{ or \\\..\\\}
245*e0c4386eSCy Schubert            $cur_item .= '\\';
246*e0c4386eSCy Schubert        }
247*e0c4386eSCy Schubert        elsif (!$delim_pats && $t =~ /^\\([{}])$/) {    # Escaped (literal) brace?
248*e0c4386eSCy Schubert            $cur_item .= $1;
249*e0c4386eSCy Schubert        }
250*e0c4386eSCy Schubert        elsif ($t eq "\n") {                            # Newline
251*e0c4386eSCy Schubert            $lineno++;
252*e0c4386eSCy Schubert            $cur_item .= $t;
253*e0c4386eSCy Schubert        }
254*e0c4386eSCy Schubert        else {                                          # Anything else
255*e0c4386eSCy Schubert            $cur_item .= $t;
256*e0c4386eSCy Schubert        }
257*e0c4386eSCy Schubert    }
258*e0c4386eSCy Schubert
259*e0c4386eSCy Schubert    if ($state eq 'PROG') {
260*e0c4386eSCy Schubert        $ERROR = "End of data inside program text that began at line $prog_start";
261*e0c4386eSCy Schubert        return undef;
262*e0c4386eSCy Schubert    }
263*e0c4386eSCy Schubert    elsif ($state eq 'TEXT') {
264*e0c4386eSCy Schubert        push @content, [ $state, $cur_item, $lineno ] if $cur_item ne '';
265*e0c4386eSCy Schubert    }
266*e0c4386eSCy Schubert    else {
267*e0c4386eSCy Schubert        die "Can't happen error #1";
268*e0c4386eSCy Schubert    }
269*e0c4386eSCy Schubert
270*e0c4386eSCy Schubert    $self->{TYPE}   = 'PREPARSED';
271*e0c4386eSCy Schubert    $self->{SOURCE} = \@content;
272*e0c4386eSCy Schubert
273*e0c4386eSCy Schubert    1;
274*e0c4386eSCy Schubert}
275*e0c4386eSCy Schubert
276*e0c4386eSCy Schubertsub prepend_text {
277*e0c4386eSCy Schubert    my $self = shift;
278*e0c4386eSCy Schubert
279*e0c4386eSCy Schubert    my $t = $self->{PREPEND};
280*e0c4386eSCy Schubert
281*e0c4386eSCy Schubert    unless (defined $t) {
282*e0c4386eSCy Schubert        $t = $GLOBAL_PREPEND{ ref $self };
283*e0c4386eSCy Schubert        unless (defined $t) {
284*e0c4386eSCy Schubert            $t = $GLOBAL_PREPEND{'Text::Template'};
285*e0c4386eSCy Schubert        }
286*e0c4386eSCy Schubert    }
287*e0c4386eSCy Schubert
288*e0c4386eSCy Schubert    $self->{PREPEND} = $_[1] if $#_ >= 1;
289*e0c4386eSCy Schubert
290*e0c4386eSCy Schubert    return $t;
291*e0c4386eSCy Schubert}
292*e0c4386eSCy Schubert
293*e0c4386eSCy Schubertsub fill_in {
294*e0c4386eSCy Schubert    my ($fi_self, %fi_a) = @_;
295*e0c4386eSCy Schubert
296*e0c4386eSCy Schubert    unless ($fi_self->{TYPE} eq 'PREPARSED') {
297*e0c4386eSCy Schubert        my $delims = _param('delimiters', %fi_a);
298*e0c4386eSCy Schubert        my @delim_arg = (defined $delims ? ($delims) : ());
299*e0c4386eSCy Schubert        $fi_self->compile(@delim_arg)
300*e0c4386eSCy Schubert            or return undef;
301*e0c4386eSCy Schubert    }
302*e0c4386eSCy Schubert
303*e0c4386eSCy Schubert    my $fi_varhash    = _param('hash',       %fi_a);
304*e0c4386eSCy Schubert    my $fi_package    = _param('package',    %fi_a);
305*e0c4386eSCy Schubert    my $fi_broken     = _param('broken',     %fi_a) || $fi_self->{BROKEN} || \&_default_broken;
306*e0c4386eSCy Schubert    my $fi_broken_arg = _param('broken_arg', %fi_a) || [];
307*e0c4386eSCy Schubert    my $fi_safe       = _param('safe',       %fi_a);
308*e0c4386eSCy Schubert    my $fi_ofh        = _param('output',     %fi_a);
309*e0c4386eSCy Schubert    my $fi_filename   = _param('filename',   %fi_a) || $fi_self->{FILENAME} || 'template';
310*e0c4386eSCy Schubert    my $fi_strict     = _param('strict',     %fi_a);
311*e0c4386eSCy Schubert    my $fi_prepend    = _param('prepend',    %fi_a);
312*e0c4386eSCy Schubert
313*e0c4386eSCy Schubert    my $fi_eval_package;
314*e0c4386eSCy Schubert    my $fi_scrub_package = 0;
315*e0c4386eSCy Schubert
316*e0c4386eSCy Schubert    unless (defined $fi_prepend) {
317*e0c4386eSCy Schubert        $fi_prepend = $fi_self->prepend_text;
318*e0c4386eSCy Schubert    }
319*e0c4386eSCy Schubert
320*e0c4386eSCy Schubert    if (defined $fi_safe) {
321*e0c4386eSCy Schubert        $fi_eval_package = 'main';
322*e0c4386eSCy Schubert    }
323*e0c4386eSCy Schubert    elsif (defined $fi_package) {
324*e0c4386eSCy Schubert        $fi_eval_package = $fi_package;
325*e0c4386eSCy Schubert    }
326*e0c4386eSCy Schubert    elsif (defined $fi_varhash) {
327*e0c4386eSCy Schubert        $fi_eval_package  = _gensym();
328*e0c4386eSCy Schubert        $fi_scrub_package = 1;
329*e0c4386eSCy Schubert    }
330*e0c4386eSCy Schubert    else {
331*e0c4386eSCy Schubert        $fi_eval_package = caller;
332*e0c4386eSCy Schubert    }
333*e0c4386eSCy Schubert
334*e0c4386eSCy Schubert    my @fi_varlist;
335*e0c4386eSCy Schubert    my $fi_install_package;
336*e0c4386eSCy Schubert
337*e0c4386eSCy Schubert    if (defined $fi_varhash) {
338*e0c4386eSCy Schubert        if (defined $fi_package) {
339*e0c4386eSCy Schubert            $fi_install_package = $fi_package;
340*e0c4386eSCy Schubert        }
341*e0c4386eSCy Schubert        elsif (defined $fi_safe) {
342*e0c4386eSCy Schubert            $fi_install_package = $fi_safe->root;
343*e0c4386eSCy Schubert        }
344*e0c4386eSCy Schubert        else {
345*e0c4386eSCy Schubert            $fi_install_package = $fi_eval_package;    # The gensymmed one
346*e0c4386eSCy Schubert        }
347*e0c4386eSCy Schubert        @fi_varlist = _install_hash($fi_varhash => $fi_install_package);
348*e0c4386eSCy Schubert        if ($fi_strict) {
349*e0c4386eSCy Schubert            $fi_prepend = "use vars qw(@fi_varlist);$fi_prepend" if @fi_varlist;
350*e0c4386eSCy Schubert            $fi_prepend = "use strict;$fi_prepend";
351*e0c4386eSCy Schubert        }
352*e0c4386eSCy Schubert    }
353*e0c4386eSCy Schubert
354*e0c4386eSCy Schubert    if (defined $fi_package && defined $fi_safe) {
355*e0c4386eSCy Schubert        no strict 'refs';
356*e0c4386eSCy Schubert
357*e0c4386eSCy Schubert        # Big fat magic here: Fix it so that the user-specified package
358*e0c4386eSCy Schubert        # is the default one available in the safe compartment.
359*e0c4386eSCy Schubert        *{ $fi_safe->root . '::' } = \%{ $fi_package . '::' };    # LOD
360*e0c4386eSCy Schubert    }
361*e0c4386eSCy Schubert
362*e0c4386eSCy Schubert    my $fi_r = '';
363*e0c4386eSCy Schubert    my $fi_item;
364*e0c4386eSCy Schubert    foreach $fi_item (@{ $fi_self->{SOURCE} }) {
365*e0c4386eSCy Schubert        my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;
366*e0c4386eSCy Schubert        if ($fi_type eq 'TEXT') {
367*e0c4386eSCy Schubert            $fi_self->append_text_to_output(
368*e0c4386eSCy Schubert                text   => $fi_text,
369*e0c4386eSCy Schubert                handle => $fi_ofh,
370*e0c4386eSCy Schubert                out    => \$fi_r,
371*e0c4386eSCy Schubert                type   => $fi_type,);
372*e0c4386eSCy Schubert        }
373*e0c4386eSCy Schubert        elsif ($fi_type eq 'PROG') {
374*e0c4386eSCy Schubert            no strict;
375*e0c4386eSCy Schubert
376*e0c4386eSCy Schubert            my $fi_lcomment = "#line $fi_lineno $fi_filename";
377*e0c4386eSCy Schubert            my $fi_progtext = "package $fi_eval_package; $fi_prepend;\n$fi_lcomment\n$fi_text;\n;";
378*e0c4386eSCy Schubert            my $fi_res;
379*e0c4386eSCy Schubert            my $fi_eval_err = '';
380*e0c4386eSCy Schubert
381*e0c4386eSCy Schubert            if ($fi_safe) {
382*e0c4386eSCy Schubert                no strict;
383*e0c4386eSCy Schubert                no warnings;
384*e0c4386eSCy Schubert
385*e0c4386eSCy Schubert                $fi_safe->reval(q{undef $OUT});
386*e0c4386eSCy Schubert                $fi_res      = $fi_safe->reval($fi_progtext);
387*e0c4386eSCy Schubert                $fi_eval_err = $@;
388*e0c4386eSCy Schubert                my $OUT = $fi_safe->reval('$OUT');
389*e0c4386eSCy Schubert                $fi_res = $OUT if defined $OUT;
390*e0c4386eSCy Schubert            }
391*e0c4386eSCy Schubert            else {
392*e0c4386eSCy Schubert                no strict;
393*e0c4386eSCy Schubert                no warnings;
394*e0c4386eSCy Schubert
395*e0c4386eSCy Schubert                my $OUT;
396*e0c4386eSCy Schubert                $fi_res      = eval $fi_progtext;
397*e0c4386eSCy Schubert                $fi_eval_err = $@;
398*e0c4386eSCy Schubert                $fi_res      = $OUT if defined $OUT;
399*e0c4386eSCy Schubert            }
400*e0c4386eSCy Schubert
401*e0c4386eSCy Schubert            # If the value of the filled-in text really was undef,
402*e0c4386eSCy Schubert            # change it to an explicit empty string to avoid undefined
403*e0c4386eSCy Schubert            # value warnings later.
404*e0c4386eSCy Schubert            $fi_res = '' unless defined $fi_res;
405*e0c4386eSCy Schubert
406*e0c4386eSCy Schubert            if ($fi_eval_err) {
407*e0c4386eSCy Schubert                $fi_res = $fi_broken->(
408*e0c4386eSCy Schubert                    text   => $fi_text,
409*e0c4386eSCy Schubert                    error  => $fi_eval_err,
410*e0c4386eSCy Schubert                    lineno => $fi_lineno,
411*e0c4386eSCy Schubert                    arg    => $fi_broken_arg,);
412*e0c4386eSCy Schubert                if (defined $fi_res) {
413*e0c4386eSCy Schubert                    $fi_self->append_text_to_output(
414*e0c4386eSCy Schubert                        text   => $fi_res,
415*e0c4386eSCy Schubert                        handle => $fi_ofh,
416*e0c4386eSCy Schubert                        out    => \$fi_r,
417*e0c4386eSCy Schubert                        type   => $fi_type,);
418*e0c4386eSCy Schubert                }
419*e0c4386eSCy Schubert                else {
420*e0c4386eSCy Schubert                    return $fi_r;    # Undefined means abort processing
421*e0c4386eSCy Schubert                }
422*e0c4386eSCy Schubert            }
423*e0c4386eSCy Schubert            else {
424*e0c4386eSCy Schubert                $fi_self->append_text_to_output(
425*e0c4386eSCy Schubert                    text   => $fi_res,
426*e0c4386eSCy Schubert                    handle => $fi_ofh,
427*e0c4386eSCy Schubert                    out    => \$fi_r,
428*e0c4386eSCy Schubert                    type   => $fi_type,);
429*e0c4386eSCy Schubert            }
430*e0c4386eSCy Schubert        }
431*e0c4386eSCy Schubert        else {
432*e0c4386eSCy Schubert            die "Can't happen error #2";
433*e0c4386eSCy Schubert        }
434*e0c4386eSCy Schubert    }
435*e0c4386eSCy Schubert
436*e0c4386eSCy Schubert    _scrubpkg($fi_eval_package) if $fi_scrub_package;
437*e0c4386eSCy Schubert
438*e0c4386eSCy Schubert    defined $fi_ofh ? 1 : $fi_r;
439*e0c4386eSCy Schubert}
440*e0c4386eSCy Schubert
441*e0c4386eSCy Schubertsub append_text_to_output {
442*e0c4386eSCy Schubert    my ($self, %arg) = @_;
443*e0c4386eSCy Schubert
444*e0c4386eSCy Schubert    if (defined $arg{handle}) {
445*e0c4386eSCy Schubert        print { $arg{handle} } $arg{text};
446*e0c4386eSCy Schubert    }
447*e0c4386eSCy Schubert    else {
448*e0c4386eSCy Schubert        ${ $arg{out} } .= $arg{text};
449*e0c4386eSCy Schubert    }
450*e0c4386eSCy Schubert
451*e0c4386eSCy Schubert    return;
452*e0c4386eSCy Schubert}
453*e0c4386eSCy Schubert
454*e0c4386eSCy Schubertsub fill_this_in {
455*e0c4386eSCy Schubert    my ($pack, $text) = splice @_, 0, 2;
456*e0c4386eSCy Schubert
457*e0c4386eSCy Schubert    my $templ = $pack->new(TYPE => 'STRING', SOURCE => $text, @_)
458*e0c4386eSCy Schubert        or return undef;
459*e0c4386eSCy Schubert
460*e0c4386eSCy Schubert    $templ->compile or return undef;
461*e0c4386eSCy Schubert
462*e0c4386eSCy Schubert    my $result = $templ->fill_in(@_);
463*e0c4386eSCy Schubert
464*e0c4386eSCy Schubert    $result;
465*e0c4386eSCy Schubert}
466*e0c4386eSCy Schubert
467*e0c4386eSCy Schubertsub fill_in_string {
468*e0c4386eSCy Schubert    my $string = shift;
469*e0c4386eSCy Schubert
470*e0c4386eSCy Schubert    my $package = _param('package', @_);
471*e0c4386eSCy Schubert
472*e0c4386eSCy Schubert    push @_, 'package' => scalar(caller) unless defined $package;
473*e0c4386eSCy Schubert
474*e0c4386eSCy Schubert    Text::Template->fill_this_in($string, @_);
475*e0c4386eSCy Schubert}
476*e0c4386eSCy Schubert
477*e0c4386eSCy Schubertsub fill_in_file {
478*e0c4386eSCy Schubert    my $fn = shift;
479*e0c4386eSCy Schubert    my $templ = Text::Template->new(TYPE => 'FILE', SOURCE => $fn, @_) or return undef;
480*e0c4386eSCy Schubert
481*e0c4386eSCy Schubert    $templ->compile or return undef;
482*e0c4386eSCy Schubert
483*e0c4386eSCy Schubert    my $text = $templ->fill_in(@_);
484*e0c4386eSCy Schubert
485*e0c4386eSCy Schubert    $text;
486*e0c4386eSCy Schubert}
487*e0c4386eSCy Schubert
488*e0c4386eSCy Schubertsub _default_broken {
489*e0c4386eSCy Schubert    my %a = @_;
490*e0c4386eSCy Schubert
491*e0c4386eSCy Schubert    my $prog_text = $a{text};
492*e0c4386eSCy Schubert    my $err       = $a{error};
493*e0c4386eSCy Schubert    my $lineno    = $a{lineno};
494*e0c4386eSCy Schubert
495*e0c4386eSCy Schubert    chomp $err;
496*e0c4386eSCy Schubert
497*e0c4386eSCy Schubert    #  $err =~ s/\s+at .*//s;
498*e0c4386eSCy Schubert    "Program fragment delivered error ``$err''";
499*e0c4386eSCy Schubert}
500*e0c4386eSCy Schubert
501*e0c4386eSCy Schubertsub _load_text {
502*e0c4386eSCy Schubert    my $fn = shift;
503*e0c4386eSCy Schubert
504*e0c4386eSCy Schubert    open my $fh, '<', $fn or do {
505*e0c4386eSCy Schubert        $ERROR = "Couldn't open file $fn: $!";
506*e0c4386eSCy Schubert        return undef;
507*e0c4386eSCy Schubert    };
508*e0c4386eSCy Schubert
509*e0c4386eSCy Schubert    local $/;
510*e0c4386eSCy Schubert
511*e0c4386eSCy Schubert    <$fh>;
512*e0c4386eSCy Schubert}
513*e0c4386eSCy Schubert
514*e0c4386eSCy Schubertsub _is_clean {
515*e0c4386eSCy Schubert    my $z;
516*e0c4386eSCy Schubert
517*e0c4386eSCy Schubert    eval { ($z = join('', @_)), eval '#' . substr($z, 0, 0); 1 }    # LOD
518*e0c4386eSCy Schubert}
519*e0c4386eSCy Schubert
520*e0c4386eSCy Schubertsub _unconditionally_untaint {
521*e0c4386eSCy Schubert    for (@_) {
522*e0c4386eSCy Schubert        ($_) = /(.*)/s;
523*e0c4386eSCy Schubert    }
524*e0c4386eSCy Schubert}
525*e0c4386eSCy Schubert
526*e0c4386eSCy Schubert{
527*e0c4386eSCy Schubert    my $seqno = 0;
528*e0c4386eSCy Schubert
529*e0c4386eSCy Schubert    sub _gensym {
530*e0c4386eSCy Schubert        __PACKAGE__ . '::GEN' . $seqno++;
531*e0c4386eSCy Schubert    }
532*e0c4386eSCy Schubert
533*e0c4386eSCy Schubert    sub _scrubpkg {
534*e0c4386eSCy Schubert        my $s = shift;
535*e0c4386eSCy Schubert
536*e0c4386eSCy Schubert        $s =~ s/^Text::Template:://;
537*e0c4386eSCy Schubert
538*e0c4386eSCy Schubert        no strict 'refs';
539*e0c4386eSCy Schubert
540*e0c4386eSCy Schubert        my $hash = $Text::Template::{ $s . "::" };
541*e0c4386eSCy Schubert
542*e0c4386eSCy Schubert        foreach my $key (keys %$hash) {
543*e0c4386eSCy Schubert            undef $hash->{$key};
544*e0c4386eSCy Schubert        }
545*e0c4386eSCy Schubert
546*e0c4386eSCy Schubert        %$hash = ();
547*e0c4386eSCy Schubert
548*e0c4386eSCy Schubert        delete $Text::Template::{ $s . "::" };
549*e0c4386eSCy Schubert    }
550*e0c4386eSCy Schubert}
551*e0c4386eSCy Schubert
552*e0c4386eSCy Schubert# Given a hashful of variables (or a list of such hashes)
553*e0c4386eSCy Schubert# install the variables into the specified package,
554*e0c4386eSCy Schubert# overwriting whatever variables were there before.
555*e0c4386eSCy Schubertsub _install_hash {
556*e0c4386eSCy Schubert    my $hashlist = shift;
557*e0c4386eSCy Schubert    my $dest     = shift;
558*e0c4386eSCy Schubert
559*e0c4386eSCy Schubert    if (UNIVERSAL::isa($hashlist, 'HASH')) {
560*e0c4386eSCy Schubert        $hashlist = [$hashlist];
561*e0c4386eSCy Schubert    }
562*e0c4386eSCy Schubert
563*e0c4386eSCy Schubert    my @varlist;
564*e0c4386eSCy Schubert
565*e0c4386eSCy Schubert    for my $hash (@$hashlist) {
566*e0c4386eSCy Schubert        for my $name (keys %$hash) {
567*e0c4386eSCy Schubert            my $val = $hash->{$name};
568*e0c4386eSCy Schubert
569*e0c4386eSCy Schubert            no strict 'refs';
570*e0c4386eSCy Schubert            no warnings 'redefine';
571*e0c4386eSCy Schubert
572*e0c4386eSCy Schubert            local *SYM = *{"$ {dest}::$name"};
573*e0c4386eSCy Schubert
574*e0c4386eSCy Schubert            if (!defined $val) {
575*e0c4386eSCy Schubert                delete ${"$ {dest}::"}{$name};
576*e0c4386eSCy Schubert                my $match = qr/^.\Q$name\E$/;
577*e0c4386eSCy Schubert                @varlist = grep { $_ !~ $match } @varlist;
578*e0c4386eSCy Schubert            }
579*e0c4386eSCy Schubert            elsif (ref $val) {
580*e0c4386eSCy Schubert                *SYM = $val;
581*e0c4386eSCy Schubert                push @varlist, do {
582*e0c4386eSCy Schubert                    if    (UNIVERSAL::isa($val, 'ARRAY')) { '@' }
583*e0c4386eSCy Schubert                    elsif (UNIVERSAL::isa($val, 'HASH'))  { '%' }
584*e0c4386eSCy Schubert                    else                                  { '$' }
585*e0c4386eSCy Schubert                    }
586*e0c4386eSCy Schubert                    . $name;
587*e0c4386eSCy Schubert            }
588*e0c4386eSCy Schubert            else {
589*e0c4386eSCy Schubert                *SYM = \$val;
590*e0c4386eSCy Schubert                push @varlist, '$' . $name;
591*e0c4386eSCy Schubert            }
592*e0c4386eSCy Schubert        }
593*e0c4386eSCy Schubert    }
594*e0c4386eSCy Schubert
595*e0c4386eSCy Schubert    @varlist;
596*e0c4386eSCy Schubert}
597*e0c4386eSCy Schubert
598*e0c4386eSCy Schubertsub TTerror { $ERROR }
599*e0c4386eSCy Schubert
600*e0c4386eSCy Schubert1;
601*e0c4386eSCy Schubert
602*e0c4386eSCy Schubert__END__
603*e0c4386eSCy Schubert
604*e0c4386eSCy Schubert=pod
605*e0c4386eSCy Schubert
606*e0c4386eSCy Schubert=encoding UTF-8
607*e0c4386eSCy Schubert
608*e0c4386eSCy Schubert=head1 NAME
609*e0c4386eSCy Schubert
610*e0c4386eSCy SchubertText::Template - Expand template text with embedded Perl
611*e0c4386eSCy Schubert
612*e0c4386eSCy Schubert=head1 VERSION
613*e0c4386eSCy Schubert
614*e0c4386eSCy Schubertversion 1.56
615*e0c4386eSCy Schubert
616*e0c4386eSCy Schubert=head1 SYNOPSIS
617*e0c4386eSCy Schubert
618*e0c4386eSCy Schubert use Text::Template;
619*e0c4386eSCy Schubert
620*e0c4386eSCy Schubert
621*e0c4386eSCy Schubert $template = Text::Template->new(TYPE => 'FILE',  SOURCE => 'filename.tmpl');
622*e0c4386eSCy Schubert $template = Text::Template->new(TYPE => 'ARRAY', SOURCE => [ ... ] );
623*e0c4386eSCy Schubert $template = Text::Template->new(TYPE => 'FILEHANDLE', SOURCE => $fh );
624*e0c4386eSCy Schubert $template = Text::Template->new(TYPE => 'STRING', SOURCE => '...' );
625*e0c4386eSCy Schubert $template = Text::Template->new(PREPEND => q{use strict;}, ...);
626*e0c4386eSCy Schubert
627*e0c4386eSCy Schubert # Use a different template file syntax:
628*e0c4386eSCy Schubert $template = Text::Template->new(DELIMITERS => [$open, $close], ...);
629*e0c4386eSCy Schubert
630*e0c4386eSCy Schubert $recipient = 'King';
631*e0c4386eSCy Schubert $text = $template->fill_in();  # Replaces `{$recipient}' with `King'
632*e0c4386eSCy Schubert print $text;
633*e0c4386eSCy Schubert
634*e0c4386eSCy Schubert $T::recipient = 'Josh';
635*e0c4386eSCy Schubert $text = $template->fill_in(PACKAGE => T);
636*e0c4386eSCy Schubert
637*e0c4386eSCy Schubert # Pass many variables explicitly
638*e0c4386eSCy Schubert $hash = { recipient => 'Abed-Nego',
639*e0c4386eSCy Schubert           friends => [ 'me', 'you' ],
640*e0c4386eSCy Schubert           enemies => { loathsome => 'Saruman',
641*e0c4386eSCy Schubert                        fearsome => 'Sauron' },
642*e0c4386eSCy Schubert         };
643*e0c4386eSCy Schubert $text = $template->fill_in(HASH => $hash, ...);
644*e0c4386eSCy Schubert # $recipient is Abed-Nego,
645*e0c4386eSCy Schubert # @friends is ( 'me', 'you' ),
646*e0c4386eSCy Schubert # %enemies is ( loathsome => ..., fearsome => ... )
647*e0c4386eSCy Schubert
648*e0c4386eSCy Schubert
649*e0c4386eSCy Schubert # Call &callback in case of programming errors in template
650*e0c4386eSCy Schubert $text = $template->fill_in(BROKEN => \&callback, BROKEN_ARG => $ref, ...);
651*e0c4386eSCy Schubert
652*e0c4386eSCy Schubert # Evaluate program fragments in Safe compartment with restricted permissions
653*e0c4386eSCy Schubert $text = $template->fill_in(SAFE => $compartment, ...);
654*e0c4386eSCy Schubert
655*e0c4386eSCy Schubert # Print result text instead of returning it
656*e0c4386eSCy Schubert $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
657*e0c4386eSCy Schubert
658*e0c4386eSCy Schubert # Parse template with different template file syntax:
659*e0c4386eSCy Schubert $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
660*e0c4386eSCy Schubert # Note that this is *faster* than using the default delimiters
661*e0c4386eSCy Schubert
662*e0c4386eSCy Schubert # Prepend specified perl code to each fragment before evaluating:
663*e0c4386eSCy Schubert $text = $template->fill_in(PREPEND => q{use strict 'vars';}, ...);
664*e0c4386eSCy Schubert
665*e0c4386eSCy Schubert use Text::Template 'fill_in_string';
666*e0c4386eSCy Schubert $text = fill_in_string( <<EOM, PACKAGE => 'T', ...);
667*e0c4386eSCy Schubert Dear {$recipient},
668*e0c4386eSCy Schubert Pay me at once.
669*e0c4386eSCy Schubert        Love,
670*e0c4386eSCy Schubert         G.V.
671*e0c4386eSCy Schubert EOM
672*e0c4386eSCy Schubert
673*e0c4386eSCy Schubert use Text::Template 'fill_in_file';
674*e0c4386eSCy Schubert $text = fill_in_file($filename, ...);
675*e0c4386eSCy Schubert
676*e0c4386eSCy Schubert # All templates will always have `use strict vars' attached to all fragments
677*e0c4386eSCy Schubert Text::Template->always_prepend(q{use strict 'vars';});
678*e0c4386eSCy Schubert
679*e0c4386eSCy Schubert=head1 DESCRIPTION
680*e0c4386eSCy Schubert
681*e0c4386eSCy SchubertThis is a library for generating form letters, building HTML pages, or
682*e0c4386eSCy Schubertfilling in templates generally.  A `template' is a piece of text that
683*e0c4386eSCy Schuberthas little Perl programs embedded in it here and there.  When you
684*e0c4386eSCy Schubert`fill in' a template, you evaluate the little programs and replace
685*e0c4386eSCy Schubertthem with their values.
686*e0c4386eSCy Schubert
687*e0c4386eSCy SchubertYou can store a template in a file outside your program.  People can
688*e0c4386eSCy Schubertmodify the template without modifying the program.  You can separate
689*e0c4386eSCy Schubertthe formatting details from the main code, and put the formatting
690*e0c4386eSCy Schubertparts of the program into the template.  That prevents code bloat and
691*e0c4386eSCy Schubertencourages functional separation.
692*e0c4386eSCy Schubert
693*e0c4386eSCy Schubert=head2 Example
694*e0c4386eSCy Schubert
695*e0c4386eSCy SchubertHere's an example of a template, which we'll suppose is stored in the
696*e0c4386eSCy Schubertfile C<formletter.tmpl>:
697*e0c4386eSCy Schubert
698*e0c4386eSCy Schubert    Dear {$title} {$lastname},
699*e0c4386eSCy Schubert
700*e0c4386eSCy Schubert    It has come to our attention that you are delinquent in your
701*e0c4386eSCy Schubert    {$monthname[$last_paid_month]} payment.  Please remit
702*e0c4386eSCy Schubert    ${sprintf("%.2f", $amount)} immediately, or your patellae may
703*e0c4386eSCy Schubert    be needlessly endangered.
704*e0c4386eSCy Schubert
705*e0c4386eSCy Schubert                    Love,
706*e0c4386eSCy Schubert
707*e0c4386eSCy Schubert                    Mark "Vizopteryx" Dominus
708*e0c4386eSCy Schubert
709*e0c4386eSCy SchubertThe result of filling in this template is a string, which might look
710*e0c4386eSCy Schubertsomething like this:
711*e0c4386eSCy Schubert
712*e0c4386eSCy Schubert    Dear Mr. Smith,
713*e0c4386eSCy Schubert
714*e0c4386eSCy Schubert    It has come to our attention that you are delinquent in your
715*e0c4386eSCy Schubert    February payment.  Please remit
716*e0c4386eSCy Schubert    $392.12 immediately, or your patellae may
717*e0c4386eSCy Schubert    be needlessly endangered.
718*e0c4386eSCy Schubert
719*e0c4386eSCy Schubert
720*e0c4386eSCy Schubert                    Love,
721*e0c4386eSCy Schubert
722*e0c4386eSCy Schubert                    Mark "Vizopteryx" Dominus
723*e0c4386eSCy Schubert
724*e0c4386eSCy SchubertHere is a complete program that transforms the example
725*e0c4386eSCy Schuberttemplate into the example result, and prints it out:
726*e0c4386eSCy Schubert
727*e0c4386eSCy Schubert    use Text::Template;
728*e0c4386eSCy Schubert
729*e0c4386eSCy Schubert    my $template = Text::Template->new(SOURCE => 'formletter.tmpl')
730*e0c4386eSCy Schubert      or die "Couldn't construct template: $Text::Template::ERROR";
731*e0c4386eSCy Schubert
732*e0c4386eSCy Schubert    my @monthname = qw(January February March April May June
733*e0c4386eSCy Schubert                       July August September October November December);
734*e0c4386eSCy Schubert    my %vars = (title           => 'Mr.',
735*e0c4386eSCy Schubert                firstname       => 'John',
736*e0c4386eSCy Schubert                lastname        => 'Smith',
737*e0c4386eSCy Schubert                last_paid_month => 1,   # February
738*e0c4386eSCy Schubert                amount          => 392.12,
739*e0c4386eSCy Schubert                monthname       => \@monthname);
740*e0c4386eSCy Schubert
741*e0c4386eSCy Schubert    my $result = $template->fill_in(HASH => \%vars);
742*e0c4386eSCy Schubert
743*e0c4386eSCy Schubert    if (defined $result) { print $result }
744*e0c4386eSCy Schubert    else { die "Couldn't fill in template: $Text::Template::ERROR" }
745*e0c4386eSCy Schubert
746*e0c4386eSCy Schubert=head2 Philosophy
747*e0c4386eSCy Schubert
748*e0c4386eSCy SchubertWhen people make a template module like this one, they almost always
749*e0c4386eSCy Schubertstart by inventing a special syntax for substitutions.  For example,
750*e0c4386eSCy Schubertthey build it so that a string like C<%%VAR%%> is replaced with the
751*e0c4386eSCy Schubertvalue of C<$VAR>.  Then they realize the need extra formatting, so
752*e0c4386eSCy Schubertthey put in some special syntax for formatting.  Then they need a
753*e0c4386eSCy Schubertloop, so they invent a loop syntax.  Pretty soon they have a new
754*e0c4386eSCy Schubertlittle template language.
755*e0c4386eSCy Schubert
756*e0c4386eSCy SchubertThis approach has two problems: First, their little language is
757*e0c4386eSCy Schubertcrippled. If you need to do something the author hasn't thought of,
758*e0c4386eSCy Schubertyou lose.  Second: Who wants to learn another language?  You already
759*e0c4386eSCy Schubertknow Perl, so why not use it?
760*e0c4386eSCy Schubert
761*e0c4386eSCy SchubertC<Text::Template> templates are programmed in I<Perl>.  You embed Perl
762*e0c4386eSCy Schubertcode in your template, with C<{> at the beginning and C<}> at the end.
763*e0c4386eSCy SchubertIf you want a variable interpolated, you write it the way you would in
764*e0c4386eSCy SchubertPerl.  If you need to make a loop, you can use any of the Perl loop
765*e0c4386eSCy Schubertconstructions.  All the Perl built-in functions are available.
766*e0c4386eSCy Schubert
767*e0c4386eSCy Schubert=head1 Details
768*e0c4386eSCy Schubert
769*e0c4386eSCy Schubert=head2 Template Parsing
770*e0c4386eSCy Schubert
771*e0c4386eSCy SchubertThe C<Text::Template> module scans the template source.  An open brace
772*e0c4386eSCy SchubertC<{> begins a program fragment, which continues until the matching
773*e0c4386eSCy Schubertclose brace C<}>.  When the template is filled in, the program
774*e0c4386eSCy Schubertfragments are evaluated, and each one is replaced with the resulting
775*e0c4386eSCy Schubertvalue to yield the text that is returned.
776*e0c4386eSCy Schubert
777*e0c4386eSCy SchubertA backslash C<\> in front of a brace (or another backslash that is in
778*e0c4386eSCy Schubertfront of a brace) escapes its special meaning.  The result of filling
779*e0c4386eSCy Schubertout this template:
780*e0c4386eSCy Schubert
781*e0c4386eSCy Schubert    \{ The sum of 1 and 2 is {1+2}  \}
782*e0c4386eSCy Schubert
783*e0c4386eSCy Schubertis
784*e0c4386eSCy Schubert
785*e0c4386eSCy Schubert    { The sum of 1 and 2 is 3  }
786*e0c4386eSCy Schubert
787*e0c4386eSCy SchubertIf you have an unmatched brace, C<Text::Template> will return a
788*e0c4386eSCy Schubertfailure code and a warning about where the problem is.  Backslashes
789*e0c4386eSCy Schubertthat do not precede a brace are passed through unchanged.  If you have
790*e0c4386eSCy Schuberta template like this:
791*e0c4386eSCy Schubert
792*e0c4386eSCy Schubert    { "String that ends in a newline.\n" }
793*e0c4386eSCy Schubert
794*e0c4386eSCy SchubertThe backslash inside the string is passed through to Perl unchanged,
795*e0c4386eSCy Schubertso the C<\n> really does turn into a newline.  See the note at the end
796*e0c4386eSCy Schubertfor details about the way backslashes work.  Backslash processing is
797*e0c4386eSCy SchubertI<not> done when you specify alternative delimiters with the
798*e0c4386eSCy SchubertC<DELIMITERS> option.  (See L<"Alternative Delimiters">, below.)
799*e0c4386eSCy Schubert
800*e0c4386eSCy SchubertEach program fragment should be a sequence of Perl statements, which
801*e0c4386eSCy Schubertare evaluated the usual way.  The result of the last statement
802*e0c4386eSCy Schubertexecuted will be evaluated in scalar context; the result of this
803*e0c4386eSCy Schubertstatement is a string, which is interpolated into the template in
804*e0c4386eSCy Schubertplace of the program fragment itself.
805*e0c4386eSCy Schubert
806*e0c4386eSCy SchubertThe fragments are evaluated in order, and side effects from earlier
807*e0c4386eSCy Schubertfragments will persist into later fragments:
808*e0c4386eSCy Schubert
809*e0c4386eSCy Schubert    {$x = @things; ''}The Lord High Chamberlain has gotten {$x}
810*e0c4386eSCy Schubert    things for me this year.
811*e0c4386eSCy Schubert    { $diff = $x - 17;
812*e0c4386eSCy Schubert      $more = 'more'
813*e0c4386eSCy Schubert      if ($diff == 0) {
814*e0c4386eSCy Schubert        $diff = 'no';
815*e0c4386eSCy Schubert      } elsif ($diff < 0) {
816*e0c4386eSCy Schubert        $more = 'fewer';
817*e0c4386eSCy Schubert      }
818*e0c4386eSCy Schubert      '';
819*e0c4386eSCy Schubert    }
820*e0c4386eSCy Schubert    That is {$diff} {$more} than he gave me last year.
821*e0c4386eSCy Schubert
822*e0c4386eSCy SchubertThe value of C<$x> set in the first line will persist into the next
823*e0c4386eSCy Schubertfragment that begins on the third line, and the values of C<$diff> and
824*e0c4386eSCy SchubertC<$more> set in the second fragment will persist and be interpolated
825*e0c4386eSCy Schubertinto the last line.  The output will look something like this:
826*e0c4386eSCy Schubert
827*e0c4386eSCy Schubert    The Lord High Chamberlain has gotten 42
828*e0c4386eSCy Schubert    things for me this year.
829*e0c4386eSCy Schubert
830*e0c4386eSCy Schubert    That is 25 more than he gave me last year.
831*e0c4386eSCy Schubert
832*e0c4386eSCy SchubertThat is all the syntax there is.
833*e0c4386eSCy Schubert
834*e0c4386eSCy Schubert=head2 The C<$OUT> variable
835*e0c4386eSCy Schubert
836*e0c4386eSCy SchubertThere is one special trick you can play in a template.  Here is the
837*e0c4386eSCy Schubertmotivation for it:  Suppose you are going to pass an array, C<@items>,
838*e0c4386eSCy Schubertinto the template, and you want the template to generate a bulleted
839*e0c4386eSCy Schubertlist with a header, like this:
840*e0c4386eSCy Schubert
841*e0c4386eSCy Schubert    Here is a list of the things I have got for you since 1907:
842*e0c4386eSCy Schubert      * Ivory
843*e0c4386eSCy Schubert      * Apes
844*e0c4386eSCy Schubert      * Peacocks
845*e0c4386eSCy Schubert      * ...
846*e0c4386eSCy Schubert
847*e0c4386eSCy SchubertOne way to do it is with a template like this:
848*e0c4386eSCy Schubert
849*e0c4386eSCy Schubert    Here is a list of the things I have got for you since 1907:
850*e0c4386eSCy Schubert    { my $blist = '';
851*e0c4386eSCy Schubert      foreach $i (@items) {
852*e0c4386eSCy Schubert          $blist .= qq{  * $i\n};
853*e0c4386eSCy Schubert      }
854*e0c4386eSCy Schubert      $blist;
855*e0c4386eSCy Schubert    }
856*e0c4386eSCy Schubert
857*e0c4386eSCy SchubertHere we construct the list in a variable called C<$blist>, which we
858*e0c4386eSCy Schubertreturn at the end.  This is a little cumbersome.  There is a shortcut.
859*e0c4386eSCy Schubert
860*e0c4386eSCy SchubertInside of templates, there is a special variable called C<$OUT>.
861*e0c4386eSCy SchubertAnything you append to this variable will appear in the output of the
862*e0c4386eSCy Schuberttemplate.  Also, if you use C<$OUT> in a program fragment, the normal
863*e0c4386eSCy Schubertbehavior, of replacing the fragment with its return value, is
864*e0c4386eSCy Schubertdisabled; instead the fragment is replaced with the value of C<$OUT>.
865*e0c4386eSCy SchubertThis means that you can write the template above like this:
866*e0c4386eSCy Schubert
867*e0c4386eSCy Schubert    Here is a list of the things I have got for you since 1907:
868*e0c4386eSCy Schubert    { foreach $i (@items) {
869*e0c4386eSCy Schubert        $OUT .= "  * $i\n";
870*e0c4386eSCy Schubert      }
871*e0c4386eSCy Schubert    }
872*e0c4386eSCy Schubert
873*e0c4386eSCy SchubertC<$OUT> is reinitialized to the empty string at the start of each
874*e0c4386eSCy Schubertprogram fragment.  It is private to C<Text::Template>, so
875*e0c4386eSCy Schubertyou can't use a variable named C<$OUT> in your template without
876*e0c4386eSCy Schubertinvoking the special behavior.
877*e0c4386eSCy Schubert
878*e0c4386eSCy Schubert=head2 General Remarks
879*e0c4386eSCy Schubert
880*e0c4386eSCy SchubertAll C<Text::Template> functions return C<undef> on failure, and set the
881*e0c4386eSCy Schubertvariable C<$Text::Template::ERROR> to contain an explanation of what
882*e0c4386eSCy Schubertwent wrong.  For example, if you try to create a template from a file
883*e0c4386eSCy Schubertthat does not exist, C<$Text::Template::ERROR> will contain something like:
884*e0c4386eSCy Schubert
885*e0c4386eSCy Schubert    Couldn't open file xyz.tmpl: No such file or directory
886*e0c4386eSCy Schubert
887*e0c4386eSCy Schubert=head2 C<new>
888*e0c4386eSCy Schubert
889*e0c4386eSCy Schubert    $template = Text::Template->new( TYPE => ..., SOURCE => ... );
890*e0c4386eSCy Schubert
891*e0c4386eSCy SchubertThis creates and returns a new template object.  C<new> returns
892*e0c4386eSCy SchubertC<undef> and sets C<$Text::Template::ERROR> if it can't create the
893*e0c4386eSCy Schuberttemplate object.  C<SOURCE> says where the template source code will
894*e0c4386eSCy Schubertcome from.  C<TYPE> says what kind of object the source is.
895*e0c4386eSCy Schubert
896*e0c4386eSCy SchubertThe most common type of source is a file:
897*e0c4386eSCy Schubert
898*e0c4386eSCy Schubert    Text::Template->new( TYPE => 'FILE', SOURCE => $filename );
899*e0c4386eSCy Schubert
900*e0c4386eSCy SchubertThis reads the template from the specified file.  The filename is
901*e0c4386eSCy Schubertopened with the Perl C<open> command, so it can be a pipe or anything
902*e0c4386eSCy Schubertelse that makes sense with C<open>.
903*e0c4386eSCy Schubert
904*e0c4386eSCy SchubertThe C<TYPE> can also be C<STRING>, in which case the C<SOURCE> should
905*e0c4386eSCy Schubertbe a string:
906*e0c4386eSCy Schubert
907*e0c4386eSCy Schubert    Text::Template->new( TYPE => 'STRING',
908*e0c4386eSCy Schubert                         SOURCE => "This is the actual template!" );
909*e0c4386eSCy Schubert
910*e0c4386eSCy SchubertThe C<TYPE> can be C<ARRAY>, in which case the source should be a
911*e0c4386eSCy Schubertreference to an array of strings.  The concatenation of these strings
912*e0c4386eSCy Schubertis the template:
913*e0c4386eSCy Schubert
914*e0c4386eSCy Schubert    Text::Template->new( TYPE => 'ARRAY',
915*e0c4386eSCy Schubert                             SOURCE => [ "This is ", "the actual",
916*e0c4386eSCy Schubert                                         " template!",
917*e0c4386eSCy Schubert                                       ]
918*e0c4386eSCy Schubert                       );
919*e0c4386eSCy Schubert
920*e0c4386eSCy SchubertThe C<TYPE> can be FILEHANDLE, in which case the source should be an
921*e0c4386eSCy Schubertopen filehandle (such as you got from the C<FileHandle> or C<IO::*>
922*e0c4386eSCy Schubertpackages, or a glob, or a reference to a glob).  In this case
923*e0c4386eSCy SchubertC<Text::Template> will read the text from the filehandle up to
924*e0c4386eSCy Schubertend-of-file, and that text is the template:
925*e0c4386eSCy Schubert
926*e0c4386eSCy Schubert    # Read template source code from STDIN:
927*e0c4386eSCy Schubert    Text::Template->new ( TYPE => 'FILEHANDLE',
928*e0c4386eSCy Schubert                          SOURCE => \*STDIN  );
929*e0c4386eSCy Schubert
930*e0c4386eSCy SchubertIf you omit the C<TYPE> attribute, it's taken to be C<FILE>.
931*e0c4386eSCy SchubertC<SOURCE> is required.  If you omit it, the program will abort.
932*e0c4386eSCy Schubert
933*e0c4386eSCy SchubertThe words C<TYPE> and C<SOURCE> can be spelled any of the following ways:
934*e0c4386eSCy Schubert
935*e0c4386eSCy Schubert    TYPE     SOURCE
936*e0c4386eSCy Schubert    Type     Source
937*e0c4386eSCy Schubert    type     source
938*e0c4386eSCy Schubert    -TYPE    -SOURCE
939*e0c4386eSCy Schubert    -Type    -Source
940*e0c4386eSCy Schubert    -type    -source
941*e0c4386eSCy Schubert
942*e0c4386eSCy SchubertPick a style you like and stick with it.
943*e0c4386eSCy Schubert
944*e0c4386eSCy Schubert=over 4
945*e0c4386eSCy Schubert
946*e0c4386eSCy Schubert=item C<DELIMITERS>
947*e0c4386eSCy Schubert
948*e0c4386eSCy SchubertYou may also add a C<DELIMITERS> option.  If this option is present,
949*e0c4386eSCy Schubertits value should be a reference to an array of two strings.  The first
950*e0c4386eSCy Schubertstring is the string that signals the beginning of each program
951*e0c4386eSCy Schubertfragment, and the second string is the string that signals the end of
952*e0c4386eSCy Schuberteach program fragment.  See L<"Alternative Delimiters">, below.
953*e0c4386eSCy Schubert
954*e0c4386eSCy Schubert=item C<ENCODING>
955*e0c4386eSCy Schubert
956*e0c4386eSCy SchubertYou may also add a C<ENCODING> option.  If this option is present, and the
957*e0c4386eSCy SchubertC<SOURCE> is a C<FILE>, then the data will be decoded from the given encoding
958*e0c4386eSCy Schubertusing the L<Encode> module.  You can use any encoding that L<Encode> recognizes.
959*e0c4386eSCy SchubertE.g.:
960*e0c4386eSCy Schubert
961*e0c4386eSCy Schubert    Text::Template->new(
962*e0c4386eSCy Schubert        TYPE     => 'FILE',
963*e0c4386eSCy Schubert        ENCODING => 'UTF-8',
964*e0c4386eSCy Schubert        SOURCE   => 'xyz.tmpl');
965*e0c4386eSCy Schubert
966*e0c4386eSCy Schubert=item C<UNTAINT>
967*e0c4386eSCy Schubert
968*e0c4386eSCy SchubertIf your program is running in taint mode, you may have problems if
969*e0c4386eSCy Schubertyour templates are stored in files.  Data read from files is
970*e0c4386eSCy Schubertconsidered 'untrustworthy', and taint mode will not allow you to
971*e0c4386eSCy Schubertevaluate the Perl code in the file.  (It is afraid that a malicious
972*e0c4386eSCy Schubertperson might have tampered with the file.)
973*e0c4386eSCy Schubert
974*e0c4386eSCy SchubertIn some environments, however, local files are trustworthy.  You can
975*e0c4386eSCy Schuberttell C<Text::Template> that a certain file is trustworthy by supplying
976*e0c4386eSCy SchubertC<UNTAINT =E<gt> 1> in the call to C<new>.  This will tell
977*e0c4386eSCy SchubertC<Text::Template> to disable taint checks on template code that has
978*e0c4386eSCy Schubertcome from a file, as long as the filename itself is considered
979*e0c4386eSCy Schuberttrustworthy.  It will also disable taint checks on template code that
980*e0c4386eSCy Schubertcomes from a filehandle.  When used with C<TYPE =E<gt> 'string'> or C<TYPE
981*e0c4386eSCy Schubert=E<gt> 'array'>, it has no effect.
982*e0c4386eSCy Schubert
983*e0c4386eSCy SchubertSee L<perlsec> for more complete information about tainting.
984*e0c4386eSCy Schubert
985*e0c4386eSCy SchubertThanks to Steve Palincsar, Gerard Vreeswijk, and Dr. Christoph Baehr
986*e0c4386eSCy Schubertfor help with this feature.
987*e0c4386eSCy Schubert
988*e0c4386eSCy Schubert=item C<PREPEND>
989*e0c4386eSCy Schubert
990*e0c4386eSCy SchubertThis option is passed along to the C<fill_in> call unless it is
991*e0c4386eSCy Schubertoverridden in the arguments to C<fill_in>.  See L<C<PREPEND> feature
992*e0c4386eSCy Schubertand using C<strict> in templates> below.
993*e0c4386eSCy Schubert
994*e0c4386eSCy Schubert=item C<BROKEN>
995*e0c4386eSCy Schubert
996*e0c4386eSCy SchubertThis option is passed along to the C<fill_in> call unless it is
997*e0c4386eSCy Schubertoverridden in the arguments to C<fill_in>.  See L<C<BROKEN>> below.
998*e0c4386eSCy Schubert
999*e0c4386eSCy Schubert=back
1000*e0c4386eSCy Schubert
1001*e0c4386eSCy Schubert=head2 C<compile>
1002*e0c4386eSCy Schubert
1003*e0c4386eSCy Schubert    $template->compile()
1004*e0c4386eSCy Schubert
1005*e0c4386eSCy SchubertLoads all the template text from the template's source, parses and
1006*e0c4386eSCy Schubertcompiles it.  If successful, returns true; otherwise returns false and
1007*e0c4386eSCy Schubertsets C<$Text::Template::ERROR>.  If the template is already compiled,
1008*e0c4386eSCy Schubertit returns true and does nothing.
1009*e0c4386eSCy Schubert
1010*e0c4386eSCy SchubertYou don't usually need to invoke this function, because C<fill_in>
1011*e0c4386eSCy Schubert(see below) compiles the template if it isn't compiled already.
1012*e0c4386eSCy Schubert
1013*e0c4386eSCy SchubertIf there is an argument to this function, it must be a reference to an
1014*e0c4386eSCy Schubertarray containing alternative delimiter strings.  See C<"Alternative
1015*e0c4386eSCy SchubertDelimiters">, below.
1016*e0c4386eSCy Schubert
1017*e0c4386eSCy Schubert=head2 C<fill_in>
1018*e0c4386eSCy Schubert
1019*e0c4386eSCy Schubert    $template->fill_in(OPTIONS);
1020*e0c4386eSCy Schubert
1021*e0c4386eSCy SchubertFills in a template.  Returns the resulting text if successful.
1022*e0c4386eSCy SchubertOtherwise, returns C<undef>  and sets C<$Text::Template::ERROR>.
1023*e0c4386eSCy Schubert
1024*e0c4386eSCy SchubertThe I<OPTIONS> are a hash, or a list of key-value pairs.  You can
1025*e0c4386eSCy Schubertwrite the key names in any of the six usual styles as above; this
1026*e0c4386eSCy Schubertmeans that where this manual says C<PACKAGE> (for example) you can
1027*e0c4386eSCy Schubertactually use any of
1028*e0c4386eSCy Schubert
1029*e0c4386eSCy Schubert    PACKAGE Package package -PACKAGE -Package -package
1030*e0c4386eSCy Schubert
1031*e0c4386eSCy SchubertPick a style you like and stick with it.  The all-lowercase versions
1032*e0c4386eSCy Schubertmay yield spurious warnings about
1033*e0c4386eSCy Schubert
1034*e0c4386eSCy Schubert    Ambiguous use of package => resolved to "package"
1035*e0c4386eSCy Schubert
1036*e0c4386eSCy Schubertso you might like to avoid them and use the capitalized versions.
1037*e0c4386eSCy Schubert
1038*e0c4386eSCy SchubertAt present, there are eight legal options:  C<PACKAGE>, C<BROKEN>,
1039*e0c4386eSCy SchubertC<BROKEN_ARG>, C<FILENAME>, C<SAFE>, C<HASH>, C<OUTPUT>, and C<DELIMITERS>.
1040*e0c4386eSCy Schubert
1041*e0c4386eSCy Schubert=over 4
1042*e0c4386eSCy Schubert
1043*e0c4386eSCy Schubert=item C<PACKAGE>
1044*e0c4386eSCy Schubert
1045*e0c4386eSCy SchubertC<PACKAGE> specifies the name of a package in which the program
1046*e0c4386eSCy Schubertfragments should be evaluated.  The default is to use the package from
1047*e0c4386eSCy Schubertwhich C<fill_in> was called.  For example, consider this template:
1048*e0c4386eSCy Schubert
1049*e0c4386eSCy Schubert    The value of the variable x is {$x}.
1050*e0c4386eSCy Schubert
1051*e0c4386eSCy SchubertIf you use C<$template-E<gt>fill_in(PACKAGE =E<gt> 'R')> , then the C<$x> in
1052*e0c4386eSCy Schubertthe template is actually replaced with the value of C<$R::x>.  If you
1053*e0c4386eSCy Schubertomit the C<PACKAGE> option, C<$x> will be replaced with the value of
1054*e0c4386eSCy Schubertthe C<$x> variable in the package that actually called C<fill_in>.
1055*e0c4386eSCy Schubert
1056*e0c4386eSCy SchubertYou should almost always use C<PACKAGE>.  If you don't, and your
1057*e0c4386eSCy Schuberttemplate makes changes to variables, those changes will be propagated
1058*e0c4386eSCy Schubertback into the main program.  Evaluating the template in a private
1059*e0c4386eSCy Schubertpackage helps prevent this.  The template can still modify variables
1060*e0c4386eSCy Schubertin your program if it wants to, but it will have to do so explicitly.
1061*e0c4386eSCy SchubertSee the section at the end on `Security'.
1062*e0c4386eSCy Schubert
1063*e0c4386eSCy SchubertHere's an example of using C<PACKAGE>:
1064*e0c4386eSCy Schubert
1065*e0c4386eSCy Schubert    Your Royal Highness,
1066*e0c4386eSCy Schubert
1067*e0c4386eSCy Schubert    Enclosed please find a list of things I have gotten
1068*e0c4386eSCy Schubert    for you since 1907:
1069*e0c4386eSCy Schubert
1070*e0c4386eSCy Schubert    { foreach $item (@items) {
1071*e0c4386eSCy Schubert            $item_no++;
1072*e0c4386eSCy Schubert        $OUT .= " $item_no. \u$item\n";
1073*e0c4386eSCy Schubert      }
1074*e0c4386eSCy Schubert    }
1075*e0c4386eSCy Schubert
1076*e0c4386eSCy Schubert    Signed,
1077*e0c4386eSCy Schubert    Lord High Chamberlain
1078*e0c4386eSCy Schubert
1079*e0c4386eSCy SchubertWe want to pass in an array which will be assigned to the array
1080*e0c4386eSCy SchubertC<@items>.  Here's how to do that:
1081*e0c4386eSCy Schubert
1082*e0c4386eSCy Schubert    @items = ('ivory', 'apes', 'peacocks', );
1083*e0c4386eSCy Schubert    $template->fill_in();
1084*e0c4386eSCy Schubert
1085*e0c4386eSCy SchubertThis is not very safe.  The reason this isn't as safe is that if you
1086*e0c4386eSCy Schuberthad a variable named C<$item_no> in scope in your program at the point
1087*e0c4386eSCy Schubertyou called C<fill_in>, its value would be clobbered by the act of
1088*e0c4386eSCy Schubertfilling out the template.  The problem is the same as if you had
1089*e0c4386eSCy Schubertwritten a subroutine that used those variables in the same way that
1090*e0c4386eSCy Schubertthe template does.  (C<$OUT> is special in templates and is always
1091*e0c4386eSCy Schubertsafe.)
1092*e0c4386eSCy Schubert
1093*e0c4386eSCy SchubertOne solution to this is to make the C<$item_no> variable private to the
1094*e0c4386eSCy Schuberttemplate by declaring it with C<my>.  If the template does this, you
1095*e0c4386eSCy Schubertare safe.
1096*e0c4386eSCy Schubert
1097*e0c4386eSCy SchubertBut if you use the C<PACKAGE> option, you will probably be safe even
1098*e0c4386eSCy Schubertif the template does I<not> declare its variables with C<my>:
1099*e0c4386eSCy Schubert
1100*e0c4386eSCy Schubert    @Q::items = ('ivory', 'apes', 'peacocks', );
1101*e0c4386eSCy Schubert    $template->fill_in(PACKAGE => 'Q');
1102*e0c4386eSCy Schubert
1103*e0c4386eSCy SchubertIn this case the template will clobber the variable C<$Q::item_no>,
1104*e0c4386eSCy Schubertwhich is not related to the one your program was using.
1105*e0c4386eSCy Schubert
1106*e0c4386eSCy SchubertTemplates cannot affect variables in the main program that are
1107*e0c4386eSCy Schubertdeclared with C<my>, unless you give the template references to those
1108*e0c4386eSCy Schubertvariables.
1109*e0c4386eSCy Schubert
1110*e0c4386eSCy Schubert=item C<HASH>
1111*e0c4386eSCy Schubert
1112*e0c4386eSCy SchubertYou may not want to put the template variables into a package.
1113*e0c4386eSCy SchubertPackages can be hard to manage:  You can't copy them, for example.
1114*e0c4386eSCy SchubertC<HASH> provides an alternative.
1115*e0c4386eSCy Schubert
1116*e0c4386eSCy SchubertThe value for C<HASH> should be a reference to a hash that maps
1117*e0c4386eSCy Schubertvariable names to values.  For example,
1118*e0c4386eSCy Schubert
1119*e0c4386eSCy Schubert    $template->fill_in(
1120*e0c4386eSCy Schubert        HASH => {
1121*e0c4386eSCy Schubert            recipient => "The King",
1122*e0c4386eSCy Schubert            items     => ['gold', 'frankincense', 'myrrh'],
1123*e0c4386eSCy Schubert            object    => \$self,
1124*e0c4386eSCy Schubert        }
1125*e0c4386eSCy Schubert    );
1126*e0c4386eSCy Schubert
1127*e0c4386eSCy Schubertwill fill out the template and use C<"The King"> as the value of
1128*e0c4386eSCy SchubertC<$recipient> and the list of items as the value of C<@items>.  Note
1129*e0c4386eSCy Schubertthat we pass an array reference, but inside the template it appears as
1130*e0c4386eSCy Schubertan array.  In general, anything other than a simple string or number
1131*e0c4386eSCy Schubertshould be passed by reference.
1132*e0c4386eSCy Schubert
1133*e0c4386eSCy SchubertWe also want to pass an object, which is in C<$self>; note that we
1134*e0c4386eSCy Schubertpass a reference to the object, C<\$self> instead.  Since we've passed
1135*e0c4386eSCy Schuberta reference to a scalar, inside the template the object appears as
1136*e0c4386eSCy SchubertC<$object>.
1137*e0c4386eSCy Schubert
1138*e0c4386eSCy SchubertThe full details of how it works are a little involved, so you might
1139*e0c4386eSCy Schubertwant to skip to the next section.
1140*e0c4386eSCy Schubert
1141*e0c4386eSCy SchubertSuppose the key in the hash is I<key> and the value is I<value>.
1142*e0c4386eSCy Schubert
1143*e0c4386eSCy Schubert=over 4
1144*e0c4386eSCy Schubert
1145*e0c4386eSCy Schubert=item *
1146*e0c4386eSCy Schubert
1147*e0c4386eSCy SchubertIf the I<value> is C<undef>, then any variables named C<$key>,
1148*e0c4386eSCy SchubertC<@key>, C<%key>, etc., are undefined.
1149*e0c4386eSCy Schubert
1150*e0c4386eSCy Schubert=item *
1151*e0c4386eSCy Schubert
1152*e0c4386eSCy SchubertIf the I<value> is a string or a number, then C<$key> is set to that
1153*e0c4386eSCy Schubertvalue in the template.
1154*e0c4386eSCy Schubert
1155*e0c4386eSCy Schubert=item *
1156*e0c4386eSCy Schubert
1157*e0c4386eSCy SchubertFor anything else, you must pass a reference.
1158*e0c4386eSCy Schubert
1159*e0c4386eSCy SchubertIf the I<value> is a reference to an array, then C<@key> is set to
1160*e0c4386eSCy Schubertthat array.  If the I<value> is a reference to a hash, then C<%key> is
1161*e0c4386eSCy Schubertset to that hash.  Similarly if I<value> is any other kind of
1162*e0c4386eSCy Schubertreference.  This means that
1163*e0c4386eSCy Schubert
1164*e0c4386eSCy Schubert    var => "foo"
1165*e0c4386eSCy Schubert
1166*e0c4386eSCy Schubertand
1167*e0c4386eSCy Schubert
1168*e0c4386eSCy Schubert    var => \"foo"
1169*e0c4386eSCy Schubert
1170*e0c4386eSCy Schuberthave almost exactly the same effect.  (The difference is that in the
1171*e0c4386eSCy Schubertformer case, the value is copied, and in the latter case it is
1172*e0c4386eSCy Schubertaliased.)
1173*e0c4386eSCy Schubert
1174*e0c4386eSCy Schubert=item *
1175*e0c4386eSCy Schubert
1176*e0c4386eSCy SchubertIn particular, if you want the template to get an object or any kind,
1177*e0c4386eSCy Schubertyou must pass a reference to it:
1178*e0c4386eSCy Schubert
1179*e0c4386eSCy Schubert    $template->fill_in(HASH => { database_handle => \$dbh, ... });
1180*e0c4386eSCy Schubert
1181*e0c4386eSCy SchubertIf you do this, the template will have a variable C<$database_handle>
1182*e0c4386eSCy Schubertwhich is the database handle object.  If you leave out the C<\>, the
1183*e0c4386eSCy Schuberttemplate will have a hash C<%database_handle>, which exposes the
1184*e0c4386eSCy Schubertinternal structure of the database handle object; you don't want that.
1185*e0c4386eSCy Schubert
1186*e0c4386eSCy Schubert=back
1187*e0c4386eSCy Schubert
1188*e0c4386eSCy SchubertNormally, the way this works is by allocating a private package,
1189*e0c4386eSCy Schubertloading all the variables into the package, and then filling out the
1190*e0c4386eSCy Schuberttemplate as if you had specified that package.  A new package is
1191*e0c4386eSCy Schubertallocated each time.  However, if you I<also> use the C<PACKAGE>
1192*e0c4386eSCy Schubertoption, C<Text::Template> loads the variables into the package you
1193*e0c4386eSCy Schubertspecified, and they stay there after the call returns.  Subsequent
1194*e0c4386eSCy Schubertcalls to C<fill_in> that use the same package will pick up the values
1195*e0c4386eSCy Schubertyou loaded in.
1196*e0c4386eSCy Schubert
1197*e0c4386eSCy SchubertIf the argument of C<HASH> is a reference to an array instead of a
1198*e0c4386eSCy Schubertreference to a hash, then the array should contain a list of hashes
1199*e0c4386eSCy Schubertwhose contents are loaded into the template package one after the
1200*e0c4386eSCy Schubertother.  You can use this feature if you want to combine several sets
1201*e0c4386eSCy Schubertof variables.  For example, one set of variables might be the defaults
1202*e0c4386eSCy Schubertfor a fill-in form, and the second set might be the user inputs, which
1203*e0c4386eSCy Schubertoverride the defaults when they are present:
1204*e0c4386eSCy Schubert
1205*e0c4386eSCy Schubert    $template->fill_in(HASH => [\%defaults, \%user_input]);
1206*e0c4386eSCy Schubert
1207*e0c4386eSCy SchubertYou can also use this to set two variables with the same name:
1208*e0c4386eSCy Schubert
1209*e0c4386eSCy Schubert    $template->fill_in(
1210*e0c4386eSCy Schubert        HASH => [
1211*e0c4386eSCy Schubert            { v => "The King" },
1212*e0c4386eSCy Schubert            { v => [1,2,3] }
1213*e0c4386eSCy Schubert        ]
1214*e0c4386eSCy Schubert    );
1215*e0c4386eSCy Schubert
1216*e0c4386eSCy SchubertThis sets C<$v> to C<"The King"> and C<@v> to C<(1,2,3)>.
1217*e0c4386eSCy Schubert
1218*e0c4386eSCy Schubert=item C<BROKEN>
1219*e0c4386eSCy Schubert
1220*e0c4386eSCy SchubertIf any of the program fragments fails to compile or aborts for any
1221*e0c4386eSCy Schubertreason, and you have set the C<BROKEN> option to a function reference,
1222*e0c4386eSCy SchubertC<Text::Template> will invoke the function.  This function is called
1223*e0c4386eSCy Schubertthe I<C<BROKEN> function>.  The C<BROKEN> function will tell
1224*e0c4386eSCy SchubertC<Text::Template> what to do next.
1225*e0c4386eSCy Schubert
1226*e0c4386eSCy SchubertIf the C<BROKEN> function returns C<undef>, C<Text::Template> will
1227*e0c4386eSCy Schubertimmediately abort processing the template and return the text that it
1228*e0c4386eSCy Schuberthas accumulated so far.  If your function does this, it should set a
1229*e0c4386eSCy Schubertflag that you can examine after C<fill_in> returns so that you can
1230*e0c4386eSCy Schuberttell whether there was a premature return or not.
1231*e0c4386eSCy Schubert
1232*e0c4386eSCy SchubertIf the C<BROKEN> function returns any other value, that value will be
1233*e0c4386eSCy Schubertinterpolated into the template as if that value had been the return
1234*e0c4386eSCy Schubertvalue of the program fragment to begin with.  For example, if the
1235*e0c4386eSCy SchubertC<BROKEN> function returns an error string, the error string will be
1236*e0c4386eSCy Schubertinterpolated into the output of the template in place of the program
1237*e0c4386eSCy Schubertfragment that cased the error.
1238*e0c4386eSCy Schubert
1239*e0c4386eSCy SchubertIf you don't specify a C<BROKEN> function, C<Text::Template> supplies
1240*e0c4386eSCy Schuberta default one that returns something like
1241*e0c4386eSCy Schubert
1242*e0c4386eSCy Schubert    Program fragment delivered error ``Illegal division by 0 at
1243*e0c4386eSCy Schubert    template line 37''
1244*e0c4386eSCy Schubert
1245*e0c4386eSCy Schubert(Note that the format of this message has changed slightly since
1246*e0c4386eSCy Schubertversion 1.31.)  The return value of the C<BROKEN> function is
1247*e0c4386eSCy Schubertinterpolated into the template at the place the error occurred, so
1248*e0c4386eSCy Schubertthat this template:
1249*e0c4386eSCy Schubert
1250*e0c4386eSCy Schubert    (3+4)*5 = { 3+4)*5 }
1251*e0c4386eSCy Schubert
1252*e0c4386eSCy Schubertyields this result:
1253*e0c4386eSCy Schubert
1254*e0c4386eSCy Schubert    (3+4)*5 = Program fragment delivered error ``syntax error at template line 1''
1255*e0c4386eSCy Schubert
1256*e0c4386eSCy SchubertIf you specify a value for the C<BROKEN> attribute, it should be a
1257*e0c4386eSCy Schubertreference to a function that C<fill_in> can call instead of the
1258*e0c4386eSCy Schubertdefault function.
1259*e0c4386eSCy Schubert
1260*e0c4386eSCy SchubertC<fill_in> will pass a hash to the C<broken> function.
1261*e0c4386eSCy SchubertThe hash will have at least these three members:
1262*e0c4386eSCy Schubert
1263*e0c4386eSCy Schubert=over 4
1264*e0c4386eSCy Schubert
1265*e0c4386eSCy Schubert=item C<text>
1266*e0c4386eSCy Schubert
1267*e0c4386eSCy SchubertThe source code of the program fragment that failed
1268*e0c4386eSCy Schubert
1269*e0c4386eSCy Schubert=item C<error>
1270*e0c4386eSCy Schubert
1271*e0c4386eSCy SchubertThe text of the error message (C<$@>) generated by eval.
1272*e0c4386eSCy Schubert
1273*e0c4386eSCy SchubertThe text has been modified to omit the trailing newline and to include
1274*e0c4386eSCy Schubertthe name of the template file (if there was one).  The line number
1275*e0c4386eSCy Schubertcounts from the beginning of the template, not from the beginning of
1276*e0c4386eSCy Schubertthe failed program fragment.
1277*e0c4386eSCy Schubert
1278*e0c4386eSCy Schubert=item C<lineno>
1279*e0c4386eSCy Schubert
1280*e0c4386eSCy SchubertThe line number of the template at which the program fragment began.
1281*e0c4386eSCy Schubert
1282*e0c4386eSCy Schubert=back
1283*e0c4386eSCy Schubert
1284*e0c4386eSCy SchubertThere may also be an C<arg> member.  See C<BROKEN_ARG>, below
1285*e0c4386eSCy Schubert
1286*e0c4386eSCy Schubert=item C<BROKEN_ARG>
1287*e0c4386eSCy Schubert
1288*e0c4386eSCy SchubertIf you supply the C<BROKEN_ARG> option to C<fill_in>, the value of the
1289*e0c4386eSCy Schubertoption is passed to the C<BROKEN> function whenever it is called.  The
1290*e0c4386eSCy Schubertdefault C<BROKEN> function ignores the C<BROKEN_ARG>, but you can
1291*e0c4386eSCy Schubertwrite a custom C<BROKEN> function that uses the C<BROKEN_ARG> to get
1292*e0c4386eSCy Schubertmore information about what went wrong.
1293*e0c4386eSCy Schubert
1294*e0c4386eSCy SchubertThe C<BROKEN> function could also use the C<BROKEN_ARG> as a reference
1295*e0c4386eSCy Schubertto store an error message or some other information that it wants to
1296*e0c4386eSCy Schubertcommunicate back to the caller.  For example:
1297*e0c4386eSCy Schubert
1298*e0c4386eSCy Schubert    $error = '';
1299*e0c4386eSCy Schubert
1300*e0c4386eSCy Schubert    sub my_broken {
1301*e0c4386eSCy Schubert       my %args = @_;
1302*e0c4386eSCy Schubert       my $err_ref = $args{arg};
1303*e0c4386eSCy Schubert       ...
1304*e0c4386eSCy Schubert       $$err_ref = "Some error message";
1305*e0c4386eSCy Schubert       return undef;
1306*e0c4386eSCy Schubert    }
1307*e0c4386eSCy Schubert
1308*e0c4386eSCy Schubert    $template->fill_in(
1309*e0c4386eSCy Schubert        BROKEN     => \&my_broken,
1310*e0c4386eSCy Schubert        BROKEN_ARG => \$error
1311*e0c4386eSCy Schubert    );
1312*e0c4386eSCy Schubert
1313*e0c4386eSCy Schubert    if ($error) {
1314*e0c4386eSCy Schubert      die "It didn't work: $error";
1315*e0c4386eSCy Schubert    }
1316*e0c4386eSCy Schubert
1317*e0c4386eSCy SchubertIf one of the program fragments in the template fails, it will call
1318*e0c4386eSCy Schubertthe C<BROKEN> function, C<my_broken>, and pass it the C<BROKEN_ARG>,
1319*e0c4386eSCy Schubertwhich is a reference to C<$error>.  C<my_broken> can store an error
1320*e0c4386eSCy Schubertmessage into C<$error> this way.  Then the function that called
1321*e0c4386eSCy SchubertC<fill_in> can see if C<my_broken> has left an error message for it
1322*e0c4386eSCy Schubertto find, and proceed accordingly.
1323*e0c4386eSCy Schubert
1324*e0c4386eSCy Schubert=item C<FILENAME>
1325*e0c4386eSCy Schubert
1326*e0c4386eSCy SchubertIf you give C<fill_in> a C<FILENAME> option, then this is the file name that
1327*e0c4386eSCy Schubertyou loaded the template source from.  This only affects the error message that
1328*e0c4386eSCy Schubertis given for template errors.  If you loaded the template from C<foo.txt> for
1329*e0c4386eSCy Schubertexample, and pass C<foo.txt> as the C<FILENAME> parameter, errors will look
1330*e0c4386eSCy Schubertlike C<... at foo.txt line N> rather than C<... at template line N>.
1331*e0c4386eSCy Schubert
1332*e0c4386eSCy SchubertNote that this does NOT have anything to do with loading a template from the
1333*e0c4386eSCy Schubertgiven filename.  See C<fill_in_file()> for that.
1334*e0c4386eSCy Schubert
1335*e0c4386eSCy SchubertFor example:
1336*e0c4386eSCy Schubert
1337*e0c4386eSCy Schubert my $template = Text::Template->new(
1338*e0c4386eSCy Schubert     TYPE   => 'string',
1339*e0c4386eSCy Schubert     SOURCE => 'The value is {1/0}');
1340*e0c4386eSCy Schubert
1341*e0c4386eSCy Schubert $template->fill_in(FILENAME => 'foo.txt') or die $Text::Template::ERROR;
1342*e0c4386eSCy Schubert
1343*e0c4386eSCy Schubertwill die with an error that contains
1344*e0c4386eSCy Schubert
1345*e0c4386eSCy Schubert Illegal division by zero at at foo.txt line 1
1346*e0c4386eSCy Schubert
1347*e0c4386eSCy Schubert=item C<SAFE>
1348*e0c4386eSCy Schubert
1349*e0c4386eSCy SchubertIf you give C<fill_in> a C<SAFE> option, its value should be a safe
1350*e0c4386eSCy Schubertcompartment object from the C<Safe> package.  All evaluation of
1351*e0c4386eSCy Schubertprogram fragments will be performed in this compartment.  See L<Safe>
1352*e0c4386eSCy Schubertfor full details about such compartments and how to restrict the
1353*e0c4386eSCy Schubertoperations that can be performed in them.
1354*e0c4386eSCy Schubert
1355*e0c4386eSCy SchubertIf you use the C<PACKAGE> option with C<SAFE>, the package you specify
1356*e0c4386eSCy Schubertwill be placed into the safe compartment and evaluation will take
1357*e0c4386eSCy Schubertplace in that package as usual.
1358*e0c4386eSCy Schubert
1359*e0c4386eSCy SchubertIf not, C<SAFE> operation is a little different from the default.
1360*e0c4386eSCy SchubertUsually, if you don't specify a package, evaluation of program
1361*e0c4386eSCy Schubertfragments occurs in the package from which the template was invoked.
1362*e0c4386eSCy SchubertBut in C<SAFE> mode the evaluation occurs inside the safe compartment
1363*e0c4386eSCy Schubertand cannot affect the calling package.  Normally, if you use C<HASH>
1364*e0c4386eSCy Schubertwithout C<PACKAGE>, the hash variables are imported into a private,
1365*e0c4386eSCy Schubertone-use-only package.  But if you use C<HASH> and C<SAFE> together
1366*e0c4386eSCy Schubertwithout C<PACKAGE>, the hash variables will just be loaded into the
1367*e0c4386eSCy Schubertroot namespace of the C<Safe> compartment.
1368*e0c4386eSCy Schubert
1369*e0c4386eSCy Schubert=item C<OUTPUT>
1370*e0c4386eSCy Schubert
1371*e0c4386eSCy SchubertIf your template is going to generate a lot of text that you are just
1372*e0c4386eSCy Schubertgoing to print out again anyway,  you can save memory by having
1373*e0c4386eSCy SchubertC<Text::Template> print out the text as it is generated instead of
1374*e0c4386eSCy Schubertmaking it into a big string and returning the string.  If you supply
1375*e0c4386eSCy Schubertthe C<OUTPUT> option to C<fill_in>, the value should be a filehandle.
1376*e0c4386eSCy SchubertThe generated text will be printed to this filehandle as it is
1377*e0c4386eSCy Schubertconstructed.  For example:
1378*e0c4386eSCy Schubert
1379*e0c4386eSCy Schubert    $template->fill_in(OUTPUT => \*STDOUT, ...);
1380*e0c4386eSCy Schubert
1381*e0c4386eSCy Schubertfills in the C<$template> as usual, but the results are immediately
1382*e0c4386eSCy Schubertprinted to STDOUT.  This may result in the output appearing more
1383*e0c4386eSCy Schubertquickly than it would have otherwise.
1384*e0c4386eSCy Schubert
1385*e0c4386eSCy SchubertIf you use C<OUTPUT>, the return value from C<fill_in> is still true on
1386*e0c4386eSCy Schubertsuccess and false on failure, but the complete text is not returned to
1387*e0c4386eSCy Schubertthe caller.
1388*e0c4386eSCy Schubert
1389*e0c4386eSCy Schubert=item C<PREPEND>
1390*e0c4386eSCy Schubert
1391*e0c4386eSCy SchubertYou can have some Perl code prepended automatically to the beginning
1392*e0c4386eSCy Schubertof every program fragment.  See L<C<PREPEND> feature and using
1393*e0c4386eSCy SchubertC<strict> in templates> below.
1394*e0c4386eSCy Schubert
1395*e0c4386eSCy Schubert=item C<DELIMITERS>
1396*e0c4386eSCy Schubert
1397*e0c4386eSCy SchubertIf this option is present, its value should be a reference to a list
1398*e0c4386eSCy Schubertof two strings.  The first string is the string that signals the
1399*e0c4386eSCy Schubertbeginning of each program fragment, and the second string is the
1400*e0c4386eSCy Schubertstring that signals the end of each program fragment.  See
1401*e0c4386eSCy SchubertL<"Alternative Delimiters">, below.
1402*e0c4386eSCy Schubert
1403*e0c4386eSCy SchubertIf you specify C<DELIMITERS> in the call to C<fill_in>, they override
1404*e0c4386eSCy Schubertany delimiters you set when you created the template object with
1405*e0c4386eSCy SchubertC<new>.
1406*e0c4386eSCy Schubert
1407*e0c4386eSCy Schubert=back
1408*e0c4386eSCy Schubert
1409*e0c4386eSCy Schubert=head1 Convenience Functions
1410*e0c4386eSCy Schubert
1411*e0c4386eSCy Schubert=head2 C<fill_this_in>
1412*e0c4386eSCy Schubert
1413*e0c4386eSCy SchubertThe basic way to fill in a template is to create a template object and
1414*e0c4386eSCy Schubertthen call C<fill_in> on it.   This is useful if you want to fill in
1415*e0c4386eSCy Schubertthe same template more than once.
1416*e0c4386eSCy Schubert
1417*e0c4386eSCy SchubertIn some programs, this can be cumbersome.  C<fill_this_in> accepts a
1418*e0c4386eSCy Schubertstring, which contains the template, and a list of options, which are
1419*e0c4386eSCy Schubertpassed to C<fill_in> as above.  It constructs the template object for
1420*e0c4386eSCy Schubertyou, fills it in as specified, and returns the results.  It returns
1421*e0c4386eSCy SchubertC<undef> and sets C<$Text::Template::ERROR> if it couldn't generate
1422*e0c4386eSCy Schubertany results.
1423*e0c4386eSCy Schubert
1424*e0c4386eSCy SchubertAn example:
1425*e0c4386eSCy Schubert
1426*e0c4386eSCy Schubert    $Q::name = 'Donald';
1427*e0c4386eSCy Schubert    $Q::amount = 141.61;
1428*e0c4386eSCy Schubert    $Q::part = 'hyoid bone';
1429*e0c4386eSCy Schubert
1430*e0c4386eSCy Schubert    $text = Text::Template->fill_this_in( <<'EOM', PACKAGE => Q);
1431*e0c4386eSCy Schubert    Dear {$name},
1432*e0c4386eSCy Schubert    You owe me \\${sprintf('%.2f', $amount)}.
1433*e0c4386eSCy Schubert    Pay or I will break your {$part}.
1434*e0c4386eSCy Schubert        Love,
1435*e0c4386eSCy Schubert        Grand Vizopteryx of Irkutsk.
1436*e0c4386eSCy Schubert    EOM
1437*e0c4386eSCy Schubert
1438*e0c4386eSCy SchubertNotice how we included the template in-line in the program by using a
1439*e0c4386eSCy Schubert`here document' with the C<E<lt>E<lt>> notation.
1440*e0c4386eSCy Schubert
1441*e0c4386eSCy SchubertC<fill_this_in> is a deprecated feature.  It is only here for
1442*e0c4386eSCy Schubertbackwards compatibility, and may be removed in some far-future version
1443*e0c4386eSCy Schubertin C<Text::Template>.  You should use C<fill_in_string> instead.  It
1444*e0c4386eSCy Schubertis described in the next section.
1445*e0c4386eSCy Schubert
1446*e0c4386eSCy Schubert=head2 C<fill_in_string>
1447*e0c4386eSCy Schubert
1448*e0c4386eSCy SchubertIt is stupid that C<fill_this_in> is a class method.  It should have
1449*e0c4386eSCy Schubertbeen just an imported function, so that you could omit the
1450*e0c4386eSCy SchubertC<Text::Template-E<gt>> in the example above.  But I made the mistake
1451*e0c4386eSCy Schubertfour years ago and it is too late to change it.
1452*e0c4386eSCy Schubert
1453*e0c4386eSCy SchubertC<fill_in_string> is exactly like C<fill_this_in> except that it is
1454*e0c4386eSCy Schubertnot a method and you can omit the C<Text::Template-E<gt>> and just say
1455*e0c4386eSCy Schubert
1456*e0c4386eSCy Schubert    print fill_in_string(<<'EOM', ...);
1457*e0c4386eSCy Schubert    Dear {$name},
1458*e0c4386eSCy Schubert      ...
1459*e0c4386eSCy Schubert    EOM
1460*e0c4386eSCy Schubert
1461*e0c4386eSCy SchubertTo use C<fill_in_string>, you need to say
1462*e0c4386eSCy Schubert
1463*e0c4386eSCy Schubert    use Text::Template 'fill_in_string';
1464*e0c4386eSCy Schubert
1465*e0c4386eSCy Schubertat the top of your program.   You should probably use
1466*e0c4386eSCy SchubertC<fill_in_string> instead of C<fill_this_in>.
1467*e0c4386eSCy Schubert
1468*e0c4386eSCy Schubert=head2 C<fill_in_file>
1469*e0c4386eSCy Schubert
1470*e0c4386eSCy SchubertIf you import C<fill_in_file>, you can say
1471*e0c4386eSCy Schubert
1472*e0c4386eSCy Schubert    $text = fill_in_file(filename, ...);
1473*e0c4386eSCy Schubert
1474*e0c4386eSCy SchubertThe C<...> are passed to C<fill_in> as above.  The filename is the
1475*e0c4386eSCy Schubertname of the file that contains the template you want to fill in.  It
1476*e0c4386eSCy Schubertreturns the result text. or C<undef>, as usual.
1477*e0c4386eSCy Schubert
1478*e0c4386eSCy SchubertIf you are going to fill in the same file more than once in the same
1479*e0c4386eSCy Schubertprogram you should use the longer C<new> / C<fill_in> sequence instead.
1480*e0c4386eSCy SchubertIt will be a lot faster because it only has to read and parse the file
1481*e0c4386eSCy Schubertonce.
1482*e0c4386eSCy Schubert
1483*e0c4386eSCy Schubert=head2 Including files into templates
1484*e0c4386eSCy Schubert
1485*e0c4386eSCy SchubertPeople always ask for this.  ``Why don't you have an include
1486*e0c4386eSCy Schubertfunction?'' they want to know.  The short answer is this is Perl, and
1487*e0c4386eSCy SchubertPerl already has an include function.  If you want it, you can just put
1488*e0c4386eSCy Schubert
1489*e0c4386eSCy Schubert    {qx{cat filename}}
1490*e0c4386eSCy Schubert
1491*e0c4386eSCy Schubertinto your template.  VoilE<agrave>.
1492*e0c4386eSCy Schubert
1493*e0c4386eSCy SchubertIf you don't want to use C<cat>, you can write a little four-line
1494*e0c4386eSCy Schubertfunction that opens a file and dumps out its contents, and call it
1495*e0c4386eSCy Schubertfrom the template.  I wrote one for you.  In the template, you can say
1496*e0c4386eSCy Schubert
1497*e0c4386eSCy Schubert    {Text::Template::_load_text(filename)}
1498*e0c4386eSCy Schubert
1499*e0c4386eSCy SchubertIf that is too verbose, here is a trick.  Suppose the template package
1500*e0c4386eSCy Schubertthat you are going to be mentioning in the C<fill_in> call is package
1501*e0c4386eSCy SchubertC<Q>.  Then in the main program, write
1502*e0c4386eSCy Schubert
1503*e0c4386eSCy Schubert    *Q::include = \&Text::Template::_load_text;
1504*e0c4386eSCy Schubert
1505*e0c4386eSCy SchubertThis imports the C<_load_text> function into package C<Q> with the
1506*e0c4386eSCy Schubertname C<include>.  From then on, any template that you fill in with
1507*e0c4386eSCy Schubertpackage C<Q> can say
1508*e0c4386eSCy Schubert
1509*e0c4386eSCy Schubert    {include(filename)}
1510*e0c4386eSCy Schubert
1511*e0c4386eSCy Schubertto insert the text from the named file at that point.  If you are
1512*e0c4386eSCy Schubertusing the C<HASH> option instead, just put C<include =E<gt>
1513*e0c4386eSCy Schubert\&Text::Template::_load_text> into the hash instead of importing it
1514*e0c4386eSCy Schubertexplicitly.
1515*e0c4386eSCy Schubert
1516*e0c4386eSCy SchubertSuppose you don't want to insert a plain text file, but rather you
1517*e0c4386eSCy Schubertwant to include one template within another?  Just use C<fill_in_file>
1518*e0c4386eSCy Schubertin the template itself:
1519*e0c4386eSCy Schubert
1520*e0c4386eSCy Schubert    {Text::Template::fill_in_file(filename)}
1521*e0c4386eSCy Schubert
1522*e0c4386eSCy SchubertYou can do the same importing trick if this is too much to type.
1523*e0c4386eSCy Schubert
1524*e0c4386eSCy Schubert=head1 Miscellaneous
1525*e0c4386eSCy Schubert
1526*e0c4386eSCy Schubert=head2 C<my> variables
1527*e0c4386eSCy Schubert
1528*e0c4386eSCy SchubertPeople are frequently surprised when this doesn't work:
1529*e0c4386eSCy Schubert
1530*e0c4386eSCy Schubert    my $recipient = 'The King';
1531*e0c4386eSCy Schubert    my $text = fill_in_file('formletter.tmpl');
1532*e0c4386eSCy Schubert
1533*e0c4386eSCy SchubertThe text C<The King> doesn't get into the form letter.  Why not?
1534*e0c4386eSCy SchubertBecause C<$recipient> is a C<my> variable, and the whole point of
1535*e0c4386eSCy SchubertC<my> variables is that they're private and inaccessible except in the
1536*e0c4386eSCy Schubertscope in which they're declared.  The template is not part of that
1537*e0c4386eSCy Schubertscope, so the template can't see C<$recipient>.
1538*e0c4386eSCy Schubert
1539*e0c4386eSCy SchubertIf that's not the behavior you want, don't use C<my>.  C<my> means a
1540*e0c4386eSCy Schubertprivate variable, and in this case you don't want the variable to be
1541*e0c4386eSCy Schubertprivate.  Put the variables into package variables in some other
1542*e0c4386eSCy Schubertpackage, and use the C<PACKAGE> option to C<fill_in>:
1543*e0c4386eSCy Schubert
1544*e0c4386eSCy Schubert    $Q::recipient = $recipient;
1545*e0c4386eSCy Schubert    my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q');
1546*e0c4386eSCy Schubert
1547*e0c4386eSCy Schubertor pass the names and values in a hash with the C<HASH> option:
1548*e0c4386eSCy Schubert
1549*e0c4386eSCy Schubert    my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient });
1550*e0c4386eSCy Schubert
1551*e0c4386eSCy Schubert=head2 Security Matters
1552*e0c4386eSCy Schubert
1553*e0c4386eSCy SchubertAll variables are evaluated in the package you specify with the
1554*e0c4386eSCy SchubertC<PACKAGE> option of C<fill_in>.  if you use this option, and if your
1555*e0c4386eSCy Schuberttemplates don't do anything egregiously stupid, you won't have to
1556*e0c4386eSCy Schubertworry that evaluation of the little programs will creep out into the
1557*e0c4386eSCy Schubertrest of your program and wreck something.
1558*e0c4386eSCy Schubert
1559*e0c4386eSCy SchubertNevertheless, there's really no way (except with C<Safe>) to protect
1560*e0c4386eSCy Schubertagainst a template that says
1561*e0c4386eSCy Schubert
1562*e0c4386eSCy Schubert    { $Important::Secret::Security::Enable = 0;
1563*e0c4386eSCy Schubert      # Disable security checks in this program
1564*e0c4386eSCy Schubert    }
1565*e0c4386eSCy Schubert
1566*e0c4386eSCy Schubertor
1567*e0c4386eSCy Schubert
1568*e0c4386eSCy Schubert    { $/ = "ho ho ho";   # Sabotage future uses of <FH>.
1569*e0c4386eSCy Schubert      # $/ is always a global variable
1570*e0c4386eSCy Schubert    }
1571*e0c4386eSCy Schubert
1572*e0c4386eSCy Schubertor even
1573*e0c4386eSCy Schubert
1574*e0c4386eSCy Schubert    { system("rm -rf /") }
1575*e0c4386eSCy Schubert
1576*e0c4386eSCy Schubertso B<don't> go filling in templates unless you're sure you know what's
1577*e0c4386eSCy Schubertin them.  If you're worried, or you can't trust the person who wrote
1578*e0c4386eSCy Schubertthe template, use the C<SAFE> option.
1579*e0c4386eSCy Schubert
1580*e0c4386eSCy SchubertA final warning: program fragments run a small risk of accidentally
1581*e0c4386eSCy Schubertclobbering local variables in the C<fill_in> function itself.  These
1582*e0c4386eSCy Schubertvariables all have names that begin with C<$fi_>, so if you stay away
1583*e0c4386eSCy Schubertfrom those names you'll be safe.  (Of course, if you're a real wizard
1584*e0c4386eSCy Schubertyou can tamper with them deliberately for exciting effects; this is
1585*e0c4386eSCy Schubertactually how C<$OUT> works.)  I can fix this, but it will make the
1586*e0c4386eSCy Schubertpackage slower to do it, so I would prefer not to.  If you are worried
1587*e0c4386eSCy Schubertabout this, send me mail and I will show you what to do about it.
1588*e0c4386eSCy Schubert
1589*e0c4386eSCy Schubert=head2 Alternative Delimiters
1590*e0c4386eSCy Schubert
1591*e0c4386eSCy SchubertLorenzo Valdettaro pointed out that if you are using C<Text::Template>
1592*e0c4386eSCy Schubertto generate TeX output, the choice of braces as the program fragment
1593*e0c4386eSCy Schubertdelimiters makes you suffer suffer suffer.  Starting in version 1.20,
1594*e0c4386eSCy Schubertyou can change the choice of delimiters to something other than curly
1595*e0c4386eSCy Schubertbraces.
1596*e0c4386eSCy Schubert
1597*e0c4386eSCy SchubertIn either the C<new()> call or the C<fill_in()> call, you can specify
1598*e0c4386eSCy Schubertan alternative set of delimiters with the C<DELIMITERS> option.  For
1599*e0c4386eSCy Schubertexample, if you would like code fragments to be delimited by C<[@-->
1600*e0c4386eSCy Schubertand C<--@]> instead of C<{> and C<}>, use
1601*e0c4386eSCy Schubert
1602*e0c4386eSCy Schubert    ... DELIMITERS => [ '[@--', '--@]' ], ...
1603*e0c4386eSCy Schubert
1604*e0c4386eSCy SchubertNote that these delimiters are I<literal strings>, not regexes.  (I
1605*e0c4386eSCy Schuberttried for regexes, but it complicates the lexical analysis too much.)
1606*e0c4386eSCy SchubertNote also that C<DELIMITERS> disables the special meaning of the
1607*e0c4386eSCy Schubertbackslash, so if you want to include the delimiters in the literal
1608*e0c4386eSCy Schuberttext of your template file, you are out of luck---it is up to you to
1609*e0c4386eSCy Schubertchoose delimiters that do not conflict with what you are doing.  The
1610*e0c4386eSCy Schubertdelimiter strings may still appear inside of program fragments as long
1611*e0c4386eSCy Schubertas they nest properly.  This means that if for some reason you
1612*e0c4386eSCy Schubertabsolutely must have a program fragment that mentions one of the
1613*e0c4386eSCy Schubertdelimiters, like this:
1614*e0c4386eSCy Schubert
1615*e0c4386eSCy Schubert    [@--
1616*e0c4386eSCy Schubert        print "Oh no, a delimiter: --@]\n"
1617*e0c4386eSCy Schubert    --@]
1618*e0c4386eSCy Schubert
1619*e0c4386eSCy Schubertyou may be able to make it work by doing this instead:
1620*e0c4386eSCy Schubert
1621*e0c4386eSCy Schubert    [@--
1622*e0c4386eSCy Schubert        # Fake matching delimiter in a comment: [@--
1623*e0c4386eSCy Schubert        print "Oh no, a delimiter: --@]\n"
1624*e0c4386eSCy Schubert    --@]
1625*e0c4386eSCy Schubert
1626*e0c4386eSCy SchubertIt may be safer to choose delimiters that begin with a newline
1627*e0c4386eSCy Schubertcharacter.
1628*e0c4386eSCy Schubert
1629*e0c4386eSCy SchubertBecause the parsing of templates is simplified by the absence of
1630*e0c4386eSCy Schubertbackslash escapes, using alternative C<DELIMITERS> may speed up the
1631*e0c4386eSCy Schubertparsing process by 20-25%.  This shows that my original choice of C<{>
1632*e0c4386eSCy Schubertand C<}> was very bad.
1633*e0c4386eSCy Schubert
1634*e0c4386eSCy Schubert=head2 C<PREPEND> feature and using C<strict> in templates
1635*e0c4386eSCy Schubert
1636*e0c4386eSCy SchubertSuppose you would like to use C<strict> in your templates to detect
1637*e0c4386eSCy Schubertundeclared variables and the like.  But each code fragment is a
1638*e0c4386eSCy Schubertseparate lexical scope, so you have to turn on C<strict> at the top of
1639*e0c4386eSCy Schuberteach and every code fragment:
1640*e0c4386eSCy Schubert
1641*e0c4386eSCy Schubert    { use strict;
1642*e0c4386eSCy Schubert      use vars '$foo';
1643*e0c4386eSCy Schubert      $foo = 14;
1644*e0c4386eSCy Schubert      ...
1645*e0c4386eSCy Schubert    }
1646*e0c4386eSCy Schubert
1647*e0c4386eSCy Schubert    ...
1648*e0c4386eSCy Schubert
1649*e0c4386eSCy Schubert    { # we forgot to put `use strict' here
1650*e0c4386eSCy Schubert      my $result = $boo + 12;    # $boo is misspelled and should be $foo
1651*e0c4386eSCy Schubert      # No error is raised on `$boo'
1652*e0c4386eSCy Schubert    }
1653*e0c4386eSCy Schubert
1654*e0c4386eSCy SchubertBecause we didn't put C<use strict> at the top of the second fragment,
1655*e0c4386eSCy Schubertit was only active in the first fragment, and we didn't get any
1656*e0c4386eSCy SchubertC<strict> checking in the second fragment.  Then we misspelled C<$foo>
1657*e0c4386eSCy Schubertand the error wasn't caught.
1658*e0c4386eSCy Schubert
1659*e0c4386eSCy SchubertC<Text::Template> version 1.22 and higher has a new feature to make
1660*e0c4386eSCy Schubertthis easier.  You can specify that any text at all be automatically
1661*e0c4386eSCy Schubertadded to the beginning of each program fragment.
1662*e0c4386eSCy Schubert
1663*e0c4386eSCy SchubertWhen you make a call to C<fill_in>, you can specify a
1664*e0c4386eSCy Schubert
1665*e0c4386eSCy Schubert    PREPEND => 'some perl statements here'
1666*e0c4386eSCy Schubert
1667*e0c4386eSCy Schubertoption; the statements will be prepended to each program fragment for
1668*e0c4386eSCy Schubertthat one call only.  Suppose that the C<fill_in> call included a
1669*e0c4386eSCy Schubert
1670*e0c4386eSCy Schubert    PREPEND => 'use strict;'
1671*e0c4386eSCy Schubert
1672*e0c4386eSCy Schubertoption, and that the template looked like this:
1673*e0c4386eSCy Schubert
1674*e0c4386eSCy Schubert    { use vars '$foo';
1675*e0c4386eSCy Schubert      $foo = 14;
1676*e0c4386eSCy Schubert      ...
1677*e0c4386eSCy Schubert    }
1678*e0c4386eSCy Schubert
1679*e0c4386eSCy Schubert    ...
1680*e0c4386eSCy Schubert
1681*e0c4386eSCy Schubert    { my $result = $boo + 12;    # $boo is misspelled and should be $foo
1682*e0c4386eSCy Schubert      ...
1683*e0c4386eSCy Schubert    }
1684*e0c4386eSCy Schubert
1685*e0c4386eSCy SchubertThe code in the second fragment would fail, because C<$boo> has not
1686*e0c4386eSCy Schubertbeen declared.  C<use strict> was implied, even though you did not
1687*e0c4386eSCy Schubertwrite it explicitly, because the C<PREPEND> option added it for you
1688*e0c4386eSCy Schubertautomatically.
1689*e0c4386eSCy Schubert
1690*e0c4386eSCy SchubertThere are three other ways to do this.  At the time you create the
1691*e0c4386eSCy Schuberttemplate object with C<new>, you can also supply a C<PREPEND> option,
1692*e0c4386eSCy Schubertin which case the statements will be prepended each time you fill in
1693*e0c4386eSCy Schubertthat template.  If the C<fill_in> call has its own C<PREPEND> option,
1694*e0c4386eSCy Schubertthis overrides the one specified at the time you created the
1695*e0c4386eSCy Schuberttemplate.  Finally, you can make the class method call
1696*e0c4386eSCy Schubert
1697*e0c4386eSCy Schubert    Text::Template->always_prepend('perl statements');
1698*e0c4386eSCy Schubert
1699*e0c4386eSCy SchubertIf you do this, then call calls to C<fill_in> for I<any> template will
1700*e0c4386eSCy Schubertattach the perl statements to the beginning of each program fragment,
1701*e0c4386eSCy Schubertexcept where overridden by C<PREPEND> options to C<new> or C<fill_in>.
1702*e0c4386eSCy Schubert
1703*e0c4386eSCy SchubertAn alternative to adding "use strict;" to the PREPEND option, you can
1704*e0c4386eSCy Schubertpass STRICT => 1 to fill_in when also passing the HASH option.
1705*e0c4386eSCy Schubert
1706*e0c4386eSCy SchubertSuppose that the C<fill_in> call included both
1707*e0c4386eSCy Schubert
1708*e0c4386eSCy Schubert    HASH   => {$foo => ''} and
1709*e0c4386eSCy Schubert    STRICT => 1
1710*e0c4386eSCy Schubert
1711*e0c4386eSCy Schubertoptions, and that the template looked like this:
1712*e0c4386eSCy Schubert
1713*e0c4386eSCy Schubert    {
1714*e0c4386eSCy Schubert      $foo = 14;
1715*e0c4386eSCy Schubert      ...
1716*e0c4386eSCy Schubert    }
1717*e0c4386eSCy Schubert
1718*e0c4386eSCy Schubert    ...
1719*e0c4386eSCy Schubert
1720*e0c4386eSCy Schubert    { my $result = $boo + 12;    # $boo is misspelled and should be $foo
1721*e0c4386eSCy Schubert      ...
1722*e0c4386eSCy Schubert    }
1723*e0c4386eSCy Schubert
1724*e0c4386eSCy SchubertThe code in the second fragment would fail, because C<$boo> has not
1725*e0c4386eSCy Schubertbeen declared. C<use strict> was implied, even though you did not
1726*e0c4386eSCy Schubertwrite it explicitly, because the C<STRICT> option added it for you
1727*e0c4386eSCy Schubertautomatically. Any variable referenced in the template that is not in the
1728*e0c4386eSCy SchubertC<HASH> option will be an error.
1729*e0c4386eSCy Schubert
1730*e0c4386eSCy Schubert=head2 Prepending in Derived Classes
1731*e0c4386eSCy Schubert
1732*e0c4386eSCy SchubertThis section is technical, and you should skip it on the first few
1733*e0c4386eSCy Schubertreadings.
1734*e0c4386eSCy Schubert
1735*e0c4386eSCy SchubertNormally there are three places that prepended text could come from.
1736*e0c4386eSCy SchubertIt could come from the C<PREPEND> option in the C<fill_in> call, from
1737*e0c4386eSCy Schubertthe C<PREPEND> option in the C<new> call that created the template
1738*e0c4386eSCy Schubertobject, or from the argument of the C<always_prepend> call.
1739*e0c4386eSCy SchubertC<Text::Template> looks for these three things in order and takes the
1740*e0c4386eSCy Schubertfirst one that it finds.
1741*e0c4386eSCy Schubert
1742*e0c4386eSCy SchubertIn a subclass of C<Text::Template>, this last possibility is
1743*e0c4386eSCy Schubertambiguous.  Suppose C<S> is a subclass of C<Text::Template>.  Should
1744*e0c4386eSCy Schubert
1745*e0c4386eSCy Schubert    Text::Template->always_prepend(...);
1746*e0c4386eSCy Schubert
1747*e0c4386eSCy Schubertaffect objects in class C<Derived>?  The answer is that you can have it
1748*e0c4386eSCy Schuberteither way.
1749*e0c4386eSCy Schubert
1750*e0c4386eSCy SchubertThe C<always_prepend> value for C<Text::Template> is normally stored
1751*e0c4386eSCy Schubertin  a hash variable named C<%GLOBAL_PREPEND> under the key
1752*e0c4386eSCy SchubertC<Text::Template>.  When C<Text::Template> looks to see what text to
1753*e0c4386eSCy Schubertprepend, it first looks in the template object itself, and if not, it
1754*e0c4386eSCy Schubertlooks in C<$GLOBAL_PREPEND{I<class>}> where I<class> is the class to
1755*e0c4386eSCy Schubertwhich the template object belongs.  If it doesn't find any value, it
1756*e0c4386eSCy Schubertlooks in C<$GLOBAL_PREPEND{'Text::Template'}>.  This means that
1757*e0c4386eSCy Schubertobjects in class C<Derived> I<will> be affected by
1758*e0c4386eSCy Schubert
1759*e0c4386eSCy Schubert    Text::Template->always_prepend(...);
1760*e0c4386eSCy Schubert
1761*e0c4386eSCy SchubertI<unless> there is also a call to
1762*e0c4386eSCy Schubert
1763*e0c4386eSCy Schubert    Derived->always_prepend(...);
1764*e0c4386eSCy Schubert
1765*e0c4386eSCy SchubertSo when you're designing your derived class, you can arrange to have
1766*e0c4386eSCy Schubertyour objects ignore C<Text::Template::always_prepend> calls by simply
1767*e0c4386eSCy Schubertputting C<Derived-E<gt>always_prepend('')> at the top of your module.
1768*e0c4386eSCy Schubert
1769*e0c4386eSCy SchubertOf course, there is also a final escape hatch: Templates support a
1770*e0c4386eSCy SchubertC<prepend_text> that is used to look up the appropriate text to be
1771*e0c4386eSCy Schubertprepended at C<fill_in> time.  Your derived class can override this
1772*e0c4386eSCy Schubertmethod to get an arbitrary effect.
1773*e0c4386eSCy Schubert
1774*e0c4386eSCy Schubert=head2 JavaScript
1775*e0c4386eSCy Schubert
1776*e0c4386eSCy SchubertJennifer D. St Clair asks:
1777*e0c4386eSCy Schubert
1778*e0c4386eSCy Schubert    > Most of my pages contain JavaScript and Stylesheets.
1779*e0c4386eSCy Schubert    > How do I change the template identifier?
1780*e0c4386eSCy Schubert
1781*e0c4386eSCy SchubertJennifer is worried about the braces in the JavaScript being taken as
1782*e0c4386eSCy Schubertthe delimiters of the Perl program fragments.  Of course, disaster
1783*e0c4386eSCy Schubertwill ensue when perl tries to evaluate these as if they were Perl
1784*e0c4386eSCy Schubertprograms.  The best choice is to find some unambiguous delimiter
1785*e0c4386eSCy Schubertstrings that you can use in your template instead of curly braces, and
1786*e0c4386eSCy Schubertthen use the C<DELIMITERS> option.  However, if you can't do this for
1787*e0c4386eSCy Schubertsome reason, there are  two easy workarounds:
1788*e0c4386eSCy Schubert
1789*e0c4386eSCy Schubert1. You can put C<\> in front of C<{>, C<}>, or C<\> to remove its
1790*e0c4386eSCy Schubertspecial meaning.  So, for example, instead of
1791*e0c4386eSCy Schubert
1792*e0c4386eSCy Schubert    if (br== "n3") {
1793*e0c4386eSCy Schubert        // etc.
1794*e0c4386eSCy Schubert    }
1795*e0c4386eSCy Schubert
1796*e0c4386eSCy Schubertyou can put
1797*e0c4386eSCy Schubert
1798*e0c4386eSCy Schubert    if (br== "n3") \{
1799*e0c4386eSCy Schubert        // etc.
1800*e0c4386eSCy Schubert    \}
1801*e0c4386eSCy Schubert
1802*e0c4386eSCy Schubertand it'll come out of the template engine the way you want.
1803*e0c4386eSCy Schubert
1804*e0c4386eSCy SchubertBut here is another method that is probably better.  To see how it
1805*e0c4386eSCy Schubertworks, first consider what happens if you put this into a template:
1806*e0c4386eSCy Schubert
1807*e0c4386eSCy Schubert    { 'foo' }
1808*e0c4386eSCy Schubert
1809*e0c4386eSCy SchubertSince it's in braces, it gets evaluated, and obviously, this is going
1810*e0c4386eSCy Schubertto turn into
1811*e0c4386eSCy Schubert
1812*e0c4386eSCy Schubert    foo
1813*e0c4386eSCy Schubert
1814*e0c4386eSCy SchubertSo now here's the trick: In Perl, C<q{...}> is the same as C<'...'>.
1815*e0c4386eSCy SchubertSo if we wrote
1816*e0c4386eSCy Schubert
1817*e0c4386eSCy Schubert    {q{foo}}
1818*e0c4386eSCy Schubert
1819*e0c4386eSCy Schubertit would turn into
1820*e0c4386eSCy Schubert
1821*e0c4386eSCy Schubert    foo
1822*e0c4386eSCy Schubert
1823*e0c4386eSCy SchubertSo for your JavaScript, just write
1824*e0c4386eSCy Schubert
1825*e0c4386eSCy Schubert    {q{if (br== "n3") {
1826*e0c4386eSCy Schubert       // etc.
1827*e0c4386eSCy Schubert       }}
1828*e0c4386eSCy Schubert    }
1829*e0c4386eSCy Schubert
1830*e0c4386eSCy Schubertand it'll come out as
1831*e0c4386eSCy Schubert
1832*e0c4386eSCy Schubert    if (br== "n3") {
1833*e0c4386eSCy Schubert        // etc.
1834*e0c4386eSCy Schubert    }
1835*e0c4386eSCy Schubert
1836*e0c4386eSCy Schubertwhich is what you want.
1837*e0c4386eSCy Schubert
1838*e0c4386eSCy Schuberthead2 Shut Up!
1839*e0c4386eSCy Schubert
1840*e0c4386eSCy SchubertPeople sometimes try to put an initialization section at the top of
1841*e0c4386eSCy Schuberttheir templates, like this:
1842*e0c4386eSCy Schubert
1843*e0c4386eSCy Schubert    { ...
1844*e0c4386eSCy Schubert        $var = 17;
1845*e0c4386eSCy Schubert    }
1846*e0c4386eSCy Schubert
1847*e0c4386eSCy SchubertThen they complain because there is a C<17> at the top of the output
1848*e0c4386eSCy Schubertthat they didn't want to have there.
1849*e0c4386eSCy Schubert
1850*e0c4386eSCy SchubertRemember that a program fragment is replaced with its own return
1851*e0c4386eSCy Schubertvalue, and that in Perl the return value of a code block is the value
1852*e0c4386eSCy Schubertof the last expression that was evaluated, which in this case is 17.
1853*e0c4386eSCy SchubertIf it didn't do that, you wouldn't be able to write C<{$recipient}>
1854*e0c4386eSCy Schubertand have the recipient filled in.
1855*e0c4386eSCy Schubert
1856*e0c4386eSCy SchubertTo prevent the 17 from appearing in the output is very simple:
1857*e0c4386eSCy Schubert
1858*e0c4386eSCy Schubert    { ...
1859*e0c4386eSCy Schubert        $var = 17;
1860*e0c4386eSCy Schubert        '';
1861*e0c4386eSCy Schubert    }
1862*e0c4386eSCy Schubert
1863*e0c4386eSCy SchubertNow the last expression evaluated yields the empty string, which is
1864*e0c4386eSCy Schubertinvisible.  If you don't like the way this looks, use
1865*e0c4386eSCy Schubert
1866*e0c4386eSCy Schubert    { ...
1867*e0c4386eSCy Schubert        $var = 17;
1868*e0c4386eSCy Schubert        ($SILENTLY);
1869*e0c4386eSCy Schubert    }
1870*e0c4386eSCy Schubert
1871*e0c4386eSCy Schubertinstead.  Presumably, C<$SILENTLY> has no value, so nothing will be
1872*e0c4386eSCy Schubertinterpolated.  This is what is known as a `trick'.
1873*e0c4386eSCy Schubert
1874*e0c4386eSCy Schubert=head2 Compatibility
1875*e0c4386eSCy Schubert
1876*e0c4386eSCy SchubertEvery effort has been made to make this module compatible with older
1877*e0c4386eSCy Schubertversions.  The only known exceptions follow:
1878*e0c4386eSCy Schubert
1879*e0c4386eSCy SchubertThe output format of the default C<BROKEN> subroutine has changed
1880*e0c4386eSCy Schuberttwice, most recently between versions 1.31 and 1.40.
1881*e0c4386eSCy Schubert
1882*e0c4386eSCy SchubertStarting in version 1.10, the C<$OUT> variable is arrogated for a
1883*e0c4386eSCy Schubertspecial meaning.  If you had templates before version 1.10 that
1884*e0c4386eSCy Schuberthappened to use a variable named C<$OUT>, you will have to change them
1885*e0c4386eSCy Schubertto use some other variable or all sorts of strangeness will result.
1886*e0c4386eSCy Schubert
1887*e0c4386eSCy SchubertBetween versions 0.1b and 1.00 the behavior of the \ metacharacter
1888*e0c4386eSCy Schubertchanged.  In 0.1b, \\ was special everywhere, and the template
1889*e0c4386eSCy Schubertprocessor always replaced it with a single backslash before passing
1890*e0c4386eSCy Schubertthe code to Perl for evaluation.  The rule now is more complicated but
1891*e0c4386eSCy Schubertprobably more convenient.  See the section on backslash processing,
1892*e0c4386eSCy Schubertbelow, for a full discussion.
1893*e0c4386eSCy Schubert
1894*e0c4386eSCy Schubert=head2 Backslash Processing
1895*e0c4386eSCy Schubert
1896*e0c4386eSCy SchubertIn C<Text::Template> beta versions, the backslash was special whenever
1897*e0c4386eSCy Schubertit appeared before a brace or another backslash.  That meant that
1898*e0c4386eSCy Schubertwhile C<{"\n"}> did indeed generate a newline, C<{"\\"}> did not
1899*e0c4386eSCy Schubertgenerate a backslash, because the code passed to Perl for evaluation
1900*e0c4386eSCy Schubertwas C<"\"> which is a syntax error.  If you wanted a backslash, you
1901*e0c4386eSCy Schubertwould have had to write C<{"\\\\"}>.
1902*e0c4386eSCy Schubert
1903*e0c4386eSCy SchubertIn C<Text::Template> versions 1.00 through 1.10, there was a bug:
1904*e0c4386eSCy SchubertBackslash was special everywhere.  In these versions, C<{"\n"}>
1905*e0c4386eSCy Schubertgenerated the letter C<n>.
1906*e0c4386eSCy Schubert
1907*e0c4386eSCy SchubertThe bug has been corrected in version 1.11, but I did not go back to
1908*e0c4386eSCy Schubertexactly the old rule, because I did not like the idea of having to
1909*e0c4386eSCy Schubertwrite C<{"\\\\"}> to get one backslash.  The rule is now more
1910*e0c4386eSCy Schubertcomplicated to remember, but probably easier to use.  The rule is now:
1911*e0c4386eSCy SchubertBackslashes are always passed to Perl unchanged I<unless> they occur
1912*e0c4386eSCy Schubertas part of a sequence like C<\\\\\\{> or C<\\\\\\}>.  In these
1913*e0c4386eSCy Schubertcontexts, they are special; C<\\> is replaced with C<\>, and C<\{> and
1914*e0c4386eSCy SchubertC<\}> signal a literal brace.
1915*e0c4386eSCy Schubert
1916*e0c4386eSCy SchubertExamples:
1917*e0c4386eSCy Schubert
1918*e0c4386eSCy Schubert    \{ foo \}
1919*e0c4386eSCy Schubert
1920*e0c4386eSCy Schubertis I<not> evaluated, because the C<\> before the braces signals that
1921*e0c4386eSCy Schubertthey should be taken literally.  The result in the output looks like this:
1922*e0c4386eSCy Schubert
1923*e0c4386eSCy Schubert    { foo }
1924*e0c4386eSCy Schubert
1925*e0c4386eSCy SchubertThis is a syntax error:
1926*e0c4386eSCy Schubert
1927*e0c4386eSCy Schubert    { "foo}" }
1928*e0c4386eSCy Schubert
1929*e0c4386eSCy Schubertbecause C<Text::Template> thinks that the code ends at the first C<}>,
1930*e0c4386eSCy Schubertand then gets upset when it sees the second one.  To make this work
1931*e0c4386eSCy Schubertcorrectly, use
1932*e0c4386eSCy Schubert
1933*e0c4386eSCy Schubert    { "foo\}" }
1934*e0c4386eSCy Schubert
1935*e0c4386eSCy SchubertThis passes C<"foo}"> to Perl for evaluation.  Note there's no C<\> in
1936*e0c4386eSCy Schubertthe evaluated code.  If you really want a C<\> in the evaluated code,
1937*e0c4386eSCy Schubertuse
1938*e0c4386eSCy Schubert
1939*e0c4386eSCy Schubert    { "foo\\\}" }
1940*e0c4386eSCy Schubert
1941*e0c4386eSCy SchubertThis passes C<"foo\}"> to Perl for evaluation.
1942*e0c4386eSCy Schubert
1943*e0c4386eSCy SchubertStarting with C<Text::Template> version 1.20, backslash processing is
1944*e0c4386eSCy Schubertdisabled if you use the C<DELIMITERS> option to specify alternative
1945*e0c4386eSCy Schubertdelimiter strings.
1946*e0c4386eSCy Schubert
1947*e0c4386eSCy Schubert=head2 A short note about C<$Text::Template::ERROR>
1948*e0c4386eSCy Schubert
1949*e0c4386eSCy SchubertIn the past some people have fretted about `violating the package
1950*e0c4386eSCy Schubertboundary' by examining a variable inside the C<Text::Template>
1951*e0c4386eSCy Schubertpackage.  Don't feel this way.  C<$Text::Template::ERROR> is part of
1952*e0c4386eSCy Schubertthe published, official interface to this package.  It is perfectly OK
1953*e0c4386eSCy Schubertto inspect this variable.  The interface is not going to change.
1954*e0c4386eSCy Schubert
1955*e0c4386eSCy SchubertIf it really, really bothers you, you can import a function called
1956*e0c4386eSCy SchubertC<TTerror> that returns the current value of the C<$ERROR> variable.
1957*e0c4386eSCy SchubertSo you can say:
1958*e0c4386eSCy Schubert
1959*e0c4386eSCy Schubert    use Text::Template 'TTerror';
1960*e0c4386eSCy Schubert
1961*e0c4386eSCy Schubert    my $template = Text::Template->new(SOURCE => $filename);
1962*e0c4386eSCy Schubert    unless ($template) {
1963*e0c4386eSCy Schubert        my $err = TTerror;
1964*e0c4386eSCy Schubert        die "Couldn't make template: $err; aborting";
1965*e0c4386eSCy Schubert    }
1966*e0c4386eSCy Schubert
1967*e0c4386eSCy SchubertI don't see what benefit this has over just doing this:
1968*e0c4386eSCy Schubert
1969*e0c4386eSCy Schubert    use Text::Template;
1970*e0c4386eSCy Schubert
1971*e0c4386eSCy Schubert    my $template = Text::Template->new(SOURCE => $filename)
1972*e0c4386eSCy Schubert        or die "Couldn't make template: $Text::Template::ERROR; aborting";
1973*e0c4386eSCy Schubert
1974*e0c4386eSCy SchubertBut if it makes you happy to do it that way, go ahead.
1975*e0c4386eSCy Schubert
1976*e0c4386eSCy Schubert=head2 Sticky Widgets in Template Files
1977*e0c4386eSCy Schubert
1978*e0c4386eSCy SchubertThe C<CGI> module provides functions for `sticky widgets', which are
1979*e0c4386eSCy Schubertform input controls that retain their values from one page to the
1980*e0c4386eSCy Schubertnext.   Sometimes people want to know how to include these widgets
1981*e0c4386eSCy Schubertinto their template output.
1982*e0c4386eSCy Schubert
1983*e0c4386eSCy SchubertIt's totally straightforward.  Just call the C<CGI> functions from
1984*e0c4386eSCy Schubertinside the template:
1985*e0c4386eSCy Schubert
1986*e0c4386eSCy Schubert    { $q->checkbox_group(NAME      => 'toppings',
1987*e0c4386eSCy Schubert                         LINEBREAK => true,
1988*e0c4386eSCy Schubert                         COLUMNS   => 3,
1989*e0c4386eSCy Schubert                         VALUES    => \@toppings,
1990*e0c4386eSCy Schubert                        );
1991*e0c4386eSCy Schubert    }
1992*e0c4386eSCy Schubert
1993*e0c4386eSCy Schubert=head2 Automatic preprocessing of program fragments
1994*e0c4386eSCy Schubert
1995*e0c4386eSCy SchubertIt may be useful to preprocess the program fragments before they are
1996*e0c4386eSCy Schubertevaluated.  See C<Text::Template::Preprocess> for more details.
1997*e0c4386eSCy Schubert
1998*e0c4386eSCy Schubert=head2 Automatic postprocessing of template hunks
1999*e0c4386eSCy Schubert
2000*e0c4386eSCy SchubertIt may be useful to process hunks of output before they are appended to
2001*e0c4386eSCy Schubertthe result text.  For this, subclass and replace the C<append_text_to_result>
2002*e0c4386eSCy Schubertmethod.  It is passed a list of pairs with these entries:
2003*e0c4386eSCy Schubert
2004*e0c4386eSCy Schubert  handle - a filehandle to which to print the desired output
2005*e0c4386eSCy Schubert  out    - a ref to a string to which to append, to use if handle is not given
2006*e0c4386eSCy Schubert  text   - the text that will be appended
2007*e0c4386eSCy Schubert  type   - where the text came from: TEXT for literal text, PROG for code
2008*e0c4386eSCy Schubert
2009*e0c4386eSCy Schubert=head1 HISTORY
2010*e0c4386eSCy Schubert
2011*e0c4386eSCy SchubertOriginally written by Mark Jason Dominus, Plover Systems (versions 0.01 - 1.46)
2012*e0c4386eSCy Schubert
2013*e0c4386eSCy SchubertMaintainership transferred to Michael Schout E<lt>mschout@cpan.orgE<gt> in version
2014*e0c4386eSCy Schubert1.47
2015*e0c4386eSCy Schubert
2016*e0c4386eSCy Schubert=head1 THANKS
2017*e0c4386eSCy Schubert
2018*e0c4386eSCy SchubertMany thanks to the following people for offering support,
2019*e0c4386eSCy Schubertencouragement, advice, bug reports, and all the other good stuff.
2020*e0c4386eSCy Schubert
2021*e0c4386eSCy Schubert=over 4
2022*e0c4386eSCy Schubert
2023*e0c4386eSCy Schubert=item *
2024*e0c4386eSCy Schubert
2025*e0c4386eSCy SchubertAndrew G Wood
2026*e0c4386eSCy Schubert
2027*e0c4386eSCy Schubert=item *
2028*e0c4386eSCy Schubert
2029*e0c4386eSCy SchubertAndy Wardley
2030*e0c4386eSCy Schubert
2031*e0c4386eSCy Schubert=item *
2032*e0c4386eSCy Schubert
2033*e0c4386eSCy SchubertAntónio Aragão
2034*e0c4386eSCy Schubert
2035*e0c4386eSCy Schubert=item *
2036*e0c4386eSCy Schubert
2037*e0c4386eSCy SchubertArchie Warnock
2038*e0c4386eSCy Schubert
2039*e0c4386eSCy Schubert=item *
2040*e0c4386eSCy Schubert
2041*e0c4386eSCy SchubertBek Oberin
2042*e0c4386eSCy Schubert
2043*e0c4386eSCy Schubert=item *
2044*e0c4386eSCy Schubert
2045*e0c4386eSCy SchubertBob Dougherty
2046*e0c4386eSCy Schubert
2047*e0c4386eSCy Schubert=item *
2048*e0c4386eSCy Schubert
2049*e0c4386eSCy SchubertBrian C. Shensky
2050*e0c4386eSCy Schubert
2051*e0c4386eSCy Schubert=item *
2052*e0c4386eSCy Schubert
2053*e0c4386eSCy SchubertChris Nandor
2054*e0c4386eSCy Schubert
2055*e0c4386eSCy Schubert=item *
2056*e0c4386eSCy Schubert
2057*e0c4386eSCy SchubertChris Wesley
2058*e0c4386eSCy Schubert
2059*e0c4386eSCy Schubert=item *
2060*e0c4386eSCy Schubert
2061*e0c4386eSCy SchubertChris.Brezil
2062*e0c4386eSCy Schubert
2063*e0c4386eSCy Schubert=item *
2064*e0c4386eSCy Schubert
2065*e0c4386eSCy SchubertDaini Xie
2066*e0c4386eSCy Schubert
2067*e0c4386eSCy Schubert=item *
2068*e0c4386eSCy Schubert
2069*e0c4386eSCy SchubertDan Franklin
2070*e0c4386eSCy Schubert
2071*e0c4386eSCy Schubert=item *
2072*e0c4386eSCy Schubert
2073*e0c4386eSCy SchubertDaniel LaLiberte
2074*e0c4386eSCy Schubert
2075*e0c4386eSCy Schubert=item *
2076*e0c4386eSCy Schubert
2077*e0c4386eSCy SchubertDavid H. Adler
2078*e0c4386eSCy Schubert
2079*e0c4386eSCy Schubert=item *
2080*e0c4386eSCy Schubert
2081*e0c4386eSCy SchubertDavid Marshall
2082*e0c4386eSCy Schubert
2083*e0c4386eSCy Schubert=item *
2084*e0c4386eSCy Schubert
2085*e0c4386eSCy SchubertDennis Taylor
2086*e0c4386eSCy Schubert
2087*e0c4386eSCy Schubert=item *
2088*e0c4386eSCy Schubert
2089*e0c4386eSCy SchubertDonald L. Greer Jr.
2090*e0c4386eSCy Schubert
2091*e0c4386eSCy Schubert=item *
2092*e0c4386eSCy Schubert
2093*e0c4386eSCy SchubertDr. Frank Bucolo
2094*e0c4386eSCy Schubert
2095*e0c4386eSCy Schubert=item *
2096*e0c4386eSCy Schubert
2097*e0c4386eSCy SchubertFred Steinberg
2098*e0c4386eSCy Schubert
2099*e0c4386eSCy Schubert=item *
2100*e0c4386eSCy Schubert
2101*e0c4386eSCy SchubertGene Damon
2102*e0c4386eSCy Schubert
2103*e0c4386eSCy Schubert=item *
2104*e0c4386eSCy Schubert
2105*e0c4386eSCy SchubertHans Persson
2106*e0c4386eSCy Schubert
2107*e0c4386eSCy Schubert=item *
2108*e0c4386eSCy Schubert
2109*e0c4386eSCy SchubertHans Stoop
2110*e0c4386eSCy Schubert
2111*e0c4386eSCy Schubert=item *
2112*e0c4386eSCy Schubert
2113*e0c4386eSCy SchubertItamar Almeida de Carvalho
2114*e0c4386eSCy Schubert
2115*e0c4386eSCy Schubert=item *
2116*e0c4386eSCy Schubert
2117*e0c4386eSCy SchubertJames H. Thompson
2118*e0c4386eSCy Schubert
2119*e0c4386eSCy Schubert=item *
2120*e0c4386eSCy Schubert
2121*e0c4386eSCy SchubertJames Mastros
2122*e0c4386eSCy Schubert
2123*e0c4386eSCy Schubert=item *
2124*e0c4386eSCy Schubert
2125*e0c4386eSCy SchubertJarko Hietaniemi
2126*e0c4386eSCy Schubert
2127*e0c4386eSCy Schubert=item *
2128*e0c4386eSCy Schubert
2129*e0c4386eSCy SchubertJason Moore
2130*e0c4386eSCy Schubert
2131*e0c4386eSCy Schubert=item *
2132*e0c4386eSCy Schubert
2133*e0c4386eSCy SchubertJennifer D. St Clair
2134*e0c4386eSCy Schubert
2135*e0c4386eSCy Schubert=item *
2136*e0c4386eSCy Schubert
2137*e0c4386eSCy SchubertJoel Appelbaum
2138*e0c4386eSCy Schubert
2139*e0c4386eSCy Schubert=item *
2140*e0c4386eSCy Schubert
2141*e0c4386eSCy SchubertJoel Meulenberg
2142*e0c4386eSCy Schubert
2143*e0c4386eSCy Schubert=item *
2144*e0c4386eSCy Schubert
2145*e0c4386eSCy SchubertJonathan Roy
2146*e0c4386eSCy Schubert
2147*e0c4386eSCy Schubert=item *
2148*e0c4386eSCy Schubert
2149*e0c4386eSCy SchubertJoseph Cheek
2150*e0c4386eSCy Schubert
2151*e0c4386eSCy Schubert=item *
2152*e0c4386eSCy Schubert
2153*e0c4386eSCy SchubertJuan E. Camacho
2154*e0c4386eSCy Schubert
2155*e0c4386eSCy Schubert=item *
2156*e0c4386eSCy Schubert
2157*e0c4386eSCy SchubertKevin Atteson
2158*e0c4386eSCy Schubert
2159*e0c4386eSCy Schubert=item *
2160*e0c4386eSCy Schubert
2161*e0c4386eSCy SchubertKevin Madsen
2162*e0c4386eSCy Schubert
2163*e0c4386eSCy Schubert=item *
2164*e0c4386eSCy Schubert
2165*e0c4386eSCy SchubertKlaus Arnhold
2166*e0c4386eSCy Schubert
2167*e0c4386eSCy Schubert=item *
2168*e0c4386eSCy Schubert
2169*e0c4386eSCy SchubertLarry Virden
2170*e0c4386eSCy Schubert
2171*e0c4386eSCy Schubert=item *
2172*e0c4386eSCy Schubert
2173*e0c4386eSCy SchubertLieven Tomme
2174*e0c4386eSCy Schubert
2175*e0c4386eSCy Schubert=item *
2176*e0c4386eSCy Schubert
2177*e0c4386eSCy SchubertLorenzo Valdettaro
2178*e0c4386eSCy Schubert
2179*e0c4386eSCy Schubert=item *
2180*e0c4386eSCy Schubert
2181*e0c4386eSCy SchubertMarek Grac
2182*e0c4386eSCy Schubert
2183*e0c4386eSCy Schubert=item *
2184*e0c4386eSCy Schubert
2185*e0c4386eSCy SchubertMatt Womer
2186*e0c4386eSCy Schubert
2187*e0c4386eSCy Schubert=item *
2188*e0c4386eSCy Schubert
2189*e0c4386eSCy SchubertMatt X. Hunter
2190*e0c4386eSCy Schubert
2191*e0c4386eSCy Schubert=item *
2192*e0c4386eSCy Schubert
2193*e0c4386eSCy SchubertMichael G Schwern
2194*e0c4386eSCy Schubert
2195*e0c4386eSCy Schubert=item *
2196*e0c4386eSCy Schubert
2197*e0c4386eSCy SchubertMichael J. Suzio
2198*e0c4386eSCy Schubert
2199*e0c4386eSCy Schubert=item *
2200*e0c4386eSCy Schubert
2201*e0c4386eSCy SchubertMichaely Yeung
2202*e0c4386eSCy Schubert
2203*e0c4386eSCy Schubert=item *
2204*e0c4386eSCy Schubert
2205*e0c4386eSCy SchubertMichelangelo Grigni
2206*e0c4386eSCy Schubert
2207*e0c4386eSCy Schubert=item *
2208*e0c4386eSCy Schubert
2209*e0c4386eSCy SchubertMike Brodhead
2210*e0c4386eSCy Schubert
2211*e0c4386eSCy Schubert=item *
2212*e0c4386eSCy Schubert
2213*e0c4386eSCy SchubertNiklas Skoglund
2214*e0c4386eSCy Schubert
2215*e0c4386eSCy Schubert=item *
2216*e0c4386eSCy Schubert
2217*e0c4386eSCy SchubertRandal L. Schwartz
2218*e0c4386eSCy Schubert
2219*e0c4386eSCy Schubert=item *
2220*e0c4386eSCy Schubert
2221*e0c4386eSCy SchubertReuven M. Lerner
2222*e0c4386eSCy Schubert
2223*e0c4386eSCy Schubert=item *
2224*e0c4386eSCy Schubert
2225*e0c4386eSCy SchubertRobert M. Ioffe
2226*e0c4386eSCy Schubert
2227*e0c4386eSCy Schubert=item *
2228*e0c4386eSCy Schubert
2229*e0c4386eSCy SchubertRon Pero
2230*e0c4386eSCy Schubert
2231*e0c4386eSCy Schubert=item *
2232*e0c4386eSCy Schubert
2233*e0c4386eSCy SchubertSan Deng
2234*e0c4386eSCy Schubert
2235*e0c4386eSCy Schubert=item *
2236*e0c4386eSCy Schubert
2237*e0c4386eSCy SchubertSean Roehnelt
2238*e0c4386eSCy Schubert
2239*e0c4386eSCy Schubert=item *
2240*e0c4386eSCy Schubert
2241*e0c4386eSCy SchubertSergey Myasnikov
2242*e0c4386eSCy Schubert
2243*e0c4386eSCy Schubert=item *
2244*e0c4386eSCy Schubert
2245*e0c4386eSCy SchubertShabbir J. Safdar
2246*e0c4386eSCy Schubert
2247*e0c4386eSCy Schubert=item *
2248*e0c4386eSCy Schubert
2249*e0c4386eSCy SchubertShad Todd
2250*e0c4386eSCy Schubert
2251*e0c4386eSCy Schubert=item *
2252*e0c4386eSCy Schubert
2253*e0c4386eSCy SchubertSteve Palincsar
2254*e0c4386eSCy Schubert
2255*e0c4386eSCy Schubert=item *
2256*e0c4386eSCy Schubert
2257*e0c4386eSCy SchubertTim Bunce
2258*e0c4386eSCy Schubert
2259*e0c4386eSCy Schubert=item *
2260*e0c4386eSCy Schubert
2261*e0c4386eSCy SchubertTodd A. Green
2262*e0c4386eSCy Schubert
2263*e0c4386eSCy Schubert=item *
2264*e0c4386eSCy Schubert
2265*e0c4386eSCy SchubertTom Brown
2266*e0c4386eSCy Schubert
2267*e0c4386eSCy Schubert=item *
2268*e0c4386eSCy Schubert
2269*e0c4386eSCy SchubertTom Henry
2270*e0c4386eSCy Schubert
2271*e0c4386eSCy Schubert=item *
2272*e0c4386eSCy Schubert
2273*e0c4386eSCy SchubertTom Snee
2274*e0c4386eSCy Schubert
2275*e0c4386eSCy Schubert=item *
2276*e0c4386eSCy Schubert
2277*e0c4386eSCy SchubertTrip Lilley
2278*e0c4386eSCy Schubert
2279*e0c4386eSCy Schubert=item *
2280*e0c4386eSCy Schubert
2281*e0c4386eSCy SchubertUwe Schneider
2282*e0c4386eSCy Schubert
2283*e0c4386eSCy Schubert=item *
2284*e0c4386eSCy Schubert
2285*e0c4386eSCy SchubertVal Luck
2286*e0c4386eSCy Schubert
2287*e0c4386eSCy Schubert=item *
2288*e0c4386eSCy Schubert
2289*e0c4386eSCy SchubertYannis Livassof
2290*e0c4386eSCy Schubert
2291*e0c4386eSCy Schubert=item *
2292*e0c4386eSCy Schubert
2293*e0c4386eSCy SchubertYonat Sharon
2294*e0c4386eSCy Schubert
2295*e0c4386eSCy Schubert=item *
2296*e0c4386eSCy Schubert
2297*e0c4386eSCy SchubertZac Hansen
2298*e0c4386eSCy Schubert
2299*e0c4386eSCy Schubert=item *
2300*e0c4386eSCy Schubert
2301*e0c4386eSCy Schubertgary at dls.net
2302*e0c4386eSCy Schubert
2303*e0c4386eSCy Schubert=back
2304*e0c4386eSCy Schubert
2305*e0c4386eSCy SchubertSpecial thanks to:
2306*e0c4386eSCy Schubert
2307*e0c4386eSCy Schubert=over 2
2308*e0c4386eSCy Schubert
2309*e0c4386eSCy Schubert=item Jonathan Roy
2310*e0c4386eSCy Schubert
2311*e0c4386eSCy Schubertfor telling me how to do the C<Safe> support (I spent two years
2312*e0c4386eSCy Schubertworrying about it, and then Jonathan pointed out that it was trivial.)
2313*e0c4386eSCy Schubert
2314*e0c4386eSCy Schubert=item Ranjit Bhatnagar
2315*e0c4386eSCy Schubert
2316*e0c4386eSCy Schubertfor demanding less verbose fragments like they have in ASP, for
2317*e0c4386eSCy Schuberthelping me figure out the Right Thing, and, especially, for talking me
2318*e0c4386eSCy Schubertout of adding any new syntax.  These discussions resulted in the
2319*e0c4386eSCy SchubertC<$OUT> feature.
2320*e0c4386eSCy Schubert
2321*e0c4386eSCy Schubert=back
2322*e0c4386eSCy Schubert
2323*e0c4386eSCy Schubert=head2 Bugs and Caveats
2324*e0c4386eSCy Schubert
2325*e0c4386eSCy SchubertC<my> variables in C<fill_in> are still susceptible to being clobbered
2326*e0c4386eSCy Schubertby template evaluation.  They all begin with C<fi_>, so avoid those
2327*e0c4386eSCy Schubertnames in your templates.
2328*e0c4386eSCy Schubert
2329*e0c4386eSCy SchubertThe line number information will be wrong if the template's lines are
2330*e0c4386eSCy Schubertnot terminated by C<"\n">.  You should let me know if this is a
2331*e0c4386eSCy Schubertproblem.  If you do, I will fix it.
2332*e0c4386eSCy Schubert
2333*e0c4386eSCy SchubertThe C<$OUT> variable has a special meaning in templates, so you cannot
2334*e0c4386eSCy Schubertuse it as if it were a regular variable.
2335*e0c4386eSCy Schubert
2336*e0c4386eSCy SchubertThere are not quite enough tests in the test suite.
2337*e0c4386eSCy Schubert
2338*e0c4386eSCy Schubert=head1 SOURCE
2339*e0c4386eSCy Schubert
2340*e0c4386eSCy SchubertThe development version is on github at L<https://https://github.com/mschout/perl-text-template>
2341*e0c4386eSCy Schubertand may be cloned from L<git://https://github.com/mschout/perl-text-template.git>
2342*e0c4386eSCy Schubert
2343*e0c4386eSCy Schubert=head1 BUGS
2344*e0c4386eSCy Schubert
2345*e0c4386eSCy SchubertPlease report any bugs or feature requests on the bugtracker website
2346*e0c4386eSCy SchubertL<https://github.com/mschout/perl-text-template/issues>
2347*e0c4386eSCy Schubert
2348*e0c4386eSCy SchubertWhen submitting a bug or request, please include a test-file or a
2349*e0c4386eSCy Schubertpatch to an existing test-file that illustrates the bug or desired
2350*e0c4386eSCy Schubertfeature.
2351*e0c4386eSCy Schubert
2352*e0c4386eSCy Schubert=head1 AUTHOR
2353*e0c4386eSCy Schubert
2354*e0c4386eSCy SchubertMichael Schout <mschout@cpan.org>
2355*e0c4386eSCy Schubert
2356*e0c4386eSCy Schubert=head1 COPYRIGHT AND LICENSE
2357*e0c4386eSCy Schubert
2358*e0c4386eSCy SchubertThis software is copyright (c) 2013 by Mark Jason Dominus <mjd@cpan.org>.
2359*e0c4386eSCy Schubert
2360*e0c4386eSCy SchubertThis is free software; you can redistribute it and/or modify it under
2361*e0c4386eSCy Schubertthe same terms as the Perl 5 programming language system itself.
2362*e0c4386eSCy Schubert
2363*e0c4386eSCy Schubert=cut
2364