Preprocess.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package Text::Template::Preprocess;
  2. use Text::Template;
  3. @ISA = qw(Text::Template);
  4. $Text::Template::Preprocess::VERSION = 1.46;
  5. sub fill_in {
  6. my $self = shift;
  7. my (%args) = @_;
  8. my $pp = $args{PREPROCESSOR} || $self->{PREPROCESSOR} ;
  9. if ($pp) {
  10. local $_ = $self->source();
  11. # print "# fill_in: before <$_>\n";
  12. &$pp;
  13. # print "# fill_in: after <$_>\n";
  14. $self->set_source_data($_);
  15. }
  16. $self->SUPER::fill_in(@_);
  17. }
  18. sub preprocessor {
  19. my ($self, $pp) = @_;
  20. my $old_pp = $self->{PREPROCESSOR};
  21. $self->{PREPROCESSOR} = $pp if @_ > 1; # OK to pass $pp=undef
  22. $old_pp;
  23. }
  24. 1;
  25. =head1 NAME
  26. Text::Template::Preprocess - Expand template text with embedded Perl
  27. =head1 VERSION
  28. This file documents C<Text::Template::Preprocess> version B<1.46>
  29. =head1 SYNOPSIS
  30. use Text::Template::Preprocess;
  31. my $t = Text::Template::Preprocess->new(...); # identical to Text::Template
  32. # Fill in template, but preprocess each code fragment with pp().
  33. my $result = $t->fill_in(..., PREPROCESSOR => \&pp);
  34. my $old_pp = $t->preprocessor(\&new_pp);
  35. =head1 DESCRIPTION
  36. C<Text::Template::Preprocess> provides a new C<PREPROCESSOR> option to
  37. C<fill_in>. If the C<PREPROCESSOR> option is supplied, it must be a
  38. reference to a preprocessor subroutine. When filling out a template,
  39. C<Text::Template::Preprocessor> will use this subroutine to preprocess
  40. the program fragment prior to evaluating the code.
  41. The preprocessor subroutine will be called repeatedly, once for each
  42. program fragment. The program fragment will be in C<$_>. The
  43. subroutine should modify the contents of C<$_> and return.
  44. C<Text::Template::Preprocess> will then execute contents of C<$_> and
  45. insert the result into the appropriate part of the template.
  46. C<Text::Template::Preprocess> objects also support a utility method,
  47. C<preprocessor()>, which sets a new preprocessor for the object. This
  48. preprocessor is used for all subsequent calls to C<fill_in> except
  49. where overridden by an explicit C<PREPROCESSOR> option.
  50. C<preprocessor()> returns the previous default preprocessor function,
  51. or undefined if there wasn't one. When invoked with no arguments,
  52. C<preprocessor()> returns the object's current default preprocessor
  53. function without changing it.
  54. In all other respects, C<Text::Template::Preprocess> is identical to
  55. C<Text::Template>.
  56. =head1 WHY?
  57. One possible purpose: If your files contain a lot of JavaScript, like
  58. this:
  59. Plain text here...
  60. { perl code }
  61. <script language=JavaScript>
  62. if (br== "n3") {
  63. // etc.
  64. }
  65. </script>
  66. { more perl code }
  67. More plain text...
  68. You don't want C<Text::Template> to confuse the curly braces in the
  69. JavaScript program with executable Perl code. One strategy:
  70. sub quote_scripts {
  71. s(<script(.*?)</script>)(q{$1})gsi;
  72. }
  73. Then use C<PREPROCESSOR =E<gt> \&quote_scripts>. This will transform
  74. =head1 SEE ALSO
  75. L<Text::Template>
  76. =head1 AUTHOR
  77. Mark Jason Dominus, Plover Systems
  78. Please send questions and other remarks about this software to
  79. C<mjd-perl-template+@plover.com>
  80. You can join a very low-volume (E<lt>10 messages per year) mailing
  81. list for announcements about this package. Send an empty note to
  82. C<mjd-perl-template-request@plover.com> to join.
  83. For updates, visit C<http://www.plover.com/~mjd/perl/Template/>.
  84. =head1 LICENSE
  85. Text::Template::Preprocess version 1.46
  86. Copyright 2013 Mark Jason Dominus
  87. This program is free software; you can redistribute it and/or
  88. modify it under the terms of the GNU General Public License as
  89. published by the Free Software Foundation; either version 2 of the
  90. License, or (at your option) any later version. You may also can
  91. redistribute it and/or modify it under the terms of the Perl
  92. Artistic License.
  93. This program is distributed in the hope that it will be useful,
  94. but WITHOUT ANY WARRANTY; without even the implied warranty of
  95. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  96. GNU General Public License for more details.
  97. You should have received copies of the GNU General Public License
  98. along with this program; if not, write to the Free Software
  99. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  100. =cut