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