2 package Text::Template::Preprocess;
4 @ISA = qw(Text::Template);
5 $Text::Template::Preprocess::VERSION = 1.46;
10 my $pp = $args{PREPROCESSOR} || $self->{PREPROCESSOR} ;
12 local $_ = $self->source();
13 # print "# fill_in: before <$_>\n";
15 # print "# fill_in: after <$_>\n";
16 $self->set_source_data($_);
18 $self->SUPER::fill_in(@_);
23 my $old_pp = $self->{PREPROCESSOR};
24 $self->{PREPROCESSOR} = $pp if @_ > 1; # OK to pass $pp=undef
33 Text::Template::Preprocess - Expand template text with embedded Perl
37 This file documents C<Text::Template::Preprocess> version B<1.46>
41 use Text::Template::Preprocess;
43 my $t = Text::Template::Preprocess->new(...); # identical to Text::Template
45 # Fill in template, but preprocess each code fragment with pp().
46 my $result = $t->fill_in(..., PREPROCESSOR => \&pp);
48 my $old_pp = $t->preprocessor(\&new_pp);
52 C<Text::Template::Preprocess> provides a new C<PREPROCESSOR> option to
53 C<fill_in>. If the C<PREPROCESSOR> option is supplied, it must be a
54 reference to a preprocessor subroutine. When filling out a template,
55 C<Text::Template::Preprocessor> will use this subroutine to preprocess
56 the program fragment prior to evaluating the code.
58 The preprocessor subroutine will be called repeatedly, once for each
59 program fragment. The program fragment will be in C<$_>. The
60 subroutine should modify the contents of C<$_> and return.
61 C<Text::Template::Preprocess> will then execute contents of C<$_> and
62 insert the result into the appropriate part of the template.
64 C<Text::Template::Preprocess> objects also support a utility method,
65 C<preprocessor()>, which sets a new preprocessor for the object. This
66 preprocessor is used for all subsequent calls to C<fill_in> except
67 where overridden by an explicit C<PREPROCESSOR> option.
68 C<preprocessor()> returns the previous default preprocessor function,
69 or undefined if there wasn't one. When invoked with no arguments,
70 C<preprocessor()> returns the object's current default preprocessor
71 function without changing it.
73 In all other respects, C<Text::Template::Preprocess> is identical to
78 One possible purpose: If your files contain a lot of JavaScript, like
84 <script language=JavaScript>
92 You don't want C<Text::Template> to confuse the curly braces in the
93 JavaScript program with executable Perl code. One strategy:
96 s(<script(.*?)</script>)(q{$1})gsi;
99 Then use C<PREPROCESSOR =E<gt> \"e_scripts>. This will transform
110 Mark Jason Dominus, Plover Systems
112 Please send questions and other remarks about this software to
113 C<mjd-perl-template+@plover.com>
115 You can join a very low-volume (E<lt>10 messages per year) mailing
116 list for announcements about this package. Send an empty note to
117 C<mjd-perl-template-request@plover.com> to join.
119 For updates, visit C<http://www.plover.com/~mjd/perl/Template/>.
123 Text::Template::Preprocess version 1.46
124 Copyright 2013 Mark Jason Dominus
126 This program is free software; you can redistribute it and/or
127 modify it under the terms of the GNU General Public License as
128 published by the Free Software Foundation; either version 2 of the
129 License, or (at your option) any later version. You may also can
130 redistribute it and/or modify it under the terms of the Perl
133 This program is distributed in the hope that it will be useful,
134 but WITHOUT ANY WARRANTY; without even the implied warranty of
135 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136 GNU General Public License for more details.
138 You should have received copies of the GNU General Public License
139 along with this program; if not, write to the Free Software
140 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.