1*e0c4386eSCy Schubert 2*e0c4386eSCy Schubertpackage Text::Template::Preprocess; 3*e0c4386eSCy Schubert$Text::Template::Preprocess::VERSION = '1.56'; 4*e0c4386eSCy Schubert# ABSTRACT: Expand template text with embedded Perl 5*e0c4386eSCy Schubert 6*e0c4386eSCy Schubertuse strict; 7*e0c4386eSCy Schubertuse warnings; 8*e0c4386eSCy Schubert 9*e0c4386eSCy Schubertuse Text::Template; 10*e0c4386eSCy Schubertour @ISA = qw(Text::Template); 11*e0c4386eSCy Schubert 12*e0c4386eSCy Schubertsub fill_in { 13*e0c4386eSCy Schubert my $self = shift; 14*e0c4386eSCy Schubert my (%args) = @_; 15*e0c4386eSCy Schubert 16*e0c4386eSCy Schubert my $pp = $args{PREPROCESSOR} || $self->{PREPROCESSOR}; 17*e0c4386eSCy Schubert 18*e0c4386eSCy Schubert if ($pp) { 19*e0c4386eSCy Schubert local $_ = $self->source(); 20*e0c4386eSCy Schubert my $type = $self->{TYPE}; 21*e0c4386eSCy Schubert 22*e0c4386eSCy Schubert # print "# fill_in: before <$_>\n"; 23*e0c4386eSCy Schubert &$pp; 24*e0c4386eSCy Schubert 25*e0c4386eSCy Schubert # print "# fill_in: after <$_>\n"; 26*e0c4386eSCy Schubert $self->set_source_data($_, $type); 27*e0c4386eSCy Schubert } 28*e0c4386eSCy Schubert 29*e0c4386eSCy Schubert $self->SUPER::fill_in(@_); 30*e0c4386eSCy Schubert} 31*e0c4386eSCy Schubert 32*e0c4386eSCy Schubertsub preprocessor { 33*e0c4386eSCy Schubert my ($self, $pp) = @_; 34*e0c4386eSCy Schubert 35*e0c4386eSCy Schubert my $old_pp = $self->{PREPROCESSOR}; 36*e0c4386eSCy Schubert 37*e0c4386eSCy Schubert $self->{PREPROCESSOR} = $pp if @_ > 1; # OK to pass $pp=undef 38*e0c4386eSCy Schubert 39*e0c4386eSCy Schubert $old_pp; 40*e0c4386eSCy Schubert} 41*e0c4386eSCy Schubert 42*e0c4386eSCy Schubert1; 43*e0c4386eSCy Schubert 44*e0c4386eSCy Schubert__END__ 45*e0c4386eSCy Schubert 46*e0c4386eSCy Schubert=pod 47*e0c4386eSCy Schubert 48*e0c4386eSCy Schubert=encoding UTF-8 49*e0c4386eSCy Schubert 50*e0c4386eSCy Schubert=head1 NAME 51*e0c4386eSCy Schubert 52*e0c4386eSCy SchubertText::Template::Preprocess - Expand template text with embedded Perl 53*e0c4386eSCy Schubert 54*e0c4386eSCy Schubert=head1 VERSION 55*e0c4386eSCy Schubert 56*e0c4386eSCy Schubertversion 1.56 57*e0c4386eSCy Schubert 58*e0c4386eSCy Schubert=head1 SYNOPSIS 59*e0c4386eSCy Schubert 60*e0c4386eSCy Schubert use Text::Template::Preprocess; 61*e0c4386eSCy Schubert 62*e0c4386eSCy Schubert my $t = Text::Template::Preprocess->new(...); # identical to Text::Template 63*e0c4386eSCy Schubert 64*e0c4386eSCy Schubert # Fill in template, but preprocess each code fragment with pp(). 65*e0c4386eSCy Schubert my $result = $t->fill_in(..., PREPROCESSOR => \&pp); 66*e0c4386eSCy Schubert 67*e0c4386eSCy Schubert my $old_pp = $t->preprocessor(\&new_pp); 68*e0c4386eSCy Schubert 69*e0c4386eSCy Schubert=head1 DESCRIPTION 70*e0c4386eSCy Schubert 71*e0c4386eSCy SchubertC<Text::Template::Preprocess> provides a new C<PREPROCESSOR> option to 72*e0c4386eSCy SchubertC<fill_in>. If the C<PREPROCESSOR> option is supplied, it must be a 73*e0c4386eSCy Schubertreference to a preprocessor subroutine. When filling out a template, 74*e0c4386eSCy SchubertC<Text::Template::Preprocessor> will use this subroutine to preprocess 75*e0c4386eSCy Schubertthe program fragment prior to evaluating the code. 76*e0c4386eSCy Schubert 77*e0c4386eSCy SchubertThe preprocessor subroutine will be called repeatedly, once for each 78*e0c4386eSCy Schubertprogram fragment. The program fragment will be in C<$_>. The 79*e0c4386eSCy Schubertsubroutine should modify the contents of C<$_> and return. 80*e0c4386eSCy SchubertC<Text::Template::Preprocess> will then execute contents of C<$_> and 81*e0c4386eSCy Schubertinsert the result into the appropriate part of the template. 82*e0c4386eSCy Schubert 83*e0c4386eSCy SchubertC<Text::Template::Preprocess> objects also support a utility method, 84*e0c4386eSCy SchubertC<preprocessor()>, which sets a new preprocessor for the object. This 85*e0c4386eSCy Schubertpreprocessor is used for all subsequent calls to C<fill_in> except 86*e0c4386eSCy Schubertwhere overridden by an explicit C<PREPROCESSOR> option. 87*e0c4386eSCy SchubertC<preprocessor()> returns the previous default preprocessor function, 88*e0c4386eSCy Schubertor undefined if there wasn't one. When invoked with no arguments, 89*e0c4386eSCy SchubertC<preprocessor()> returns the object's current default preprocessor 90*e0c4386eSCy Schubertfunction without changing it. 91*e0c4386eSCy Schubert 92*e0c4386eSCy SchubertIn all other respects, C<Text::Template::Preprocess> is identical to 93*e0c4386eSCy SchubertC<Text::Template>. 94*e0c4386eSCy Schubert 95*e0c4386eSCy Schubert=head1 WHY? 96*e0c4386eSCy Schubert 97*e0c4386eSCy SchubertOne possible purpose: If your files contain a lot of JavaScript, like 98*e0c4386eSCy Schubertthis: 99*e0c4386eSCy Schubert 100*e0c4386eSCy Schubert Plain text here... 101*e0c4386eSCy Schubert { perl code } 102*e0c4386eSCy Schubert <script language=JavaScript> 103*e0c4386eSCy Schubert if (br== "n3") { 104*e0c4386eSCy Schubert // etc. 105*e0c4386eSCy Schubert } 106*e0c4386eSCy Schubert </script> 107*e0c4386eSCy Schubert { more perl code } 108*e0c4386eSCy Schubert More plain text... 109*e0c4386eSCy Schubert 110*e0c4386eSCy SchubertYou don't want C<Text::Template> to confuse the curly braces in the 111*e0c4386eSCy SchubertJavaScript program with executable Perl code. One strategy: 112*e0c4386eSCy Schubert 113*e0c4386eSCy Schubert sub quote_scripts { 114*e0c4386eSCy Schubert s(<script(.*?)</script>)(q{$1})gsi; 115*e0c4386eSCy Schubert } 116*e0c4386eSCy Schubert 117*e0c4386eSCy SchubertThen use C<PREPROCESSOR =E<gt> \"e_scripts>. This will transform 118*e0c4386eSCy Schubert 119*e0c4386eSCy Schubert=head1 SEE ALSO 120*e0c4386eSCy Schubert 121*e0c4386eSCy SchubertL<Text::Template> 122*e0c4386eSCy Schubert 123*e0c4386eSCy Schubert=head1 SOURCE 124*e0c4386eSCy Schubert 125*e0c4386eSCy SchubertThe development version is on github at L<https://https://github.com/mschout/perl-text-template> 126*e0c4386eSCy Schubertand may be cloned from L<git://https://github.com/mschout/perl-text-template.git> 127*e0c4386eSCy Schubert 128*e0c4386eSCy Schubert=head1 BUGS 129*e0c4386eSCy Schubert 130*e0c4386eSCy SchubertPlease report any bugs or feature requests on the bugtracker website 131*e0c4386eSCy SchubertL<https://github.com/mschout/perl-text-template/issues> 132*e0c4386eSCy Schubert 133*e0c4386eSCy SchubertWhen submitting a bug or request, please include a test-file or a 134*e0c4386eSCy Schubertpatch to an existing test-file that illustrates the bug or desired 135*e0c4386eSCy Schubertfeature. 136*e0c4386eSCy Schubert 137*e0c4386eSCy Schubert=head1 AUTHOR 138*e0c4386eSCy Schubert 139*e0c4386eSCy SchubertMark Jason Dominus, Plover Systems 140*e0c4386eSCy Schubert 141*e0c4386eSCy SchubertPlease send questions and other remarks about this software to 142*e0c4386eSCy SchubertC<mjd-perl-template+@plover.com> 143*e0c4386eSCy Schubert 144*e0c4386eSCy SchubertYou can join a very low-volume (E<lt>10 messages per year) mailing 145*e0c4386eSCy Schubertlist for announcements about this package. Send an empty note to 146*e0c4386eSCy SchubertC<mjd-perl-template-request@plover.com> to join. 147*e0c4386eSCy Schubert 148*e0c4386eSCy SchubertFor updates, visit C<http://www.plover.com/~mjd/perl/Template/>. 149*e0c4386eSCy Schubert 150*e0c4386eSCy Schubert=head1 COPYRIGHT AND LICENSE 151*e0c4386eSCy Schubert 152*e0c4386eSCy SchubertThis software is copyright (c) 2013 by Mark Jason Dominus <mjd@cpan.org>. 153*e0c4386eSCy Schubert 154*e0c4386eSCy SchubertThis is free software; you can redistribute it and/or modify it under 155*e0c4386eSCy Schubertthe same terms as the Perl 5 programming language system itself. 156*e0c4386eSCy Schubert 157*e0c4386eSCy Schubert=cut 158