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