Preprocess.pm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package Text::Template::Preprocess;
  2. $Text::Template::Preprocess::VERSION = '1.56';
  3. # ABSTRACT: Expand template text with embedded Perl
  4. use strict;
  5. use warnings;
  6. use Text::Template;
  7. our @ISA = qw(Text::Template);
  8. sub fill_in {
  9. my $self = shift;
  10. my (%args) = @_;
  11. my $pp = $args{PREPROCESSOR} || $self->{PREPROCESSOR};
  12. if ($pp) {
  13. local $_ = $self->source();
  14. my $type = $self->{TYPE};
  15. # print "# fill_in: before <$_>\n";
  16. &$pp;
  17. # print "# fill_in: after <$_>\n";
  18. $self->set_source_data($_, $type);
  19. }
  20. $self->SUPER::fill_in(@_);
  21. }
  22. sub preprocessor {
  23. my ($self, $pp) = @_;
  24. my $old_pp = $self->{PREPROCESSOR};
  25. $self->{PREPROCESSOR} = $pp if @_ > 1; # OK to pass $pp=undef
  26. $old_pp;
  27. }
  28. 1;
  29. __END__
  30. =pod
  31. =encoding UTF-8
  32. =head1 NAME
  33. Text::Template::Preprocess - Expand template text with embedded Perl
  34. =head1 VERSION
  35. version 1.56
  36. =head1 SYNOPSIS
  37. use Text::Template::Preprocess;
  38. my $t = Text::Template::Preprocess->new(...); # identical to Text::Template
  39. # Fill in template, but preprocess each code fragment with pp().
  40. my $result = $t->fill_in(..., PREPROCESSOR => \&pp);
  41. my $old_pp = $t->preprocessor(\&new_pp);
  42. =head1 DESCRIPTION
  43. C<Text::Template::Preprocess> provides a new C<PREPROCESSOR> option to
  44. C<fill_in>. If the C<PREPROCESSOR> option is supplied, it must be a
  45. reference to a preprocessor subroutine. When filling out a template,
  46. C<Text::Template::Preprocessor> will use this subroutine to preprocess
  47. the program fragment prior to evaluating the code.
  48. The preprocessor subroutine will be called repeatedly, once for each
  49. program fragment. The program fragment will be in C<$_>. The
  50. subroutine should modify the contents of C<$_> and return.
  51. C<Text::Template::Preprocess> will then execute contents of C<$_> and
  52. insert the result into the appropriate part of the template.
  53. C<Text::Template::Preprocess> objects also support a utility method,
  54. C<preprocessor()>, which sets a new preprocessor for the object. This
  55. preprocessor is used for all subsequent calls to C<fill_in> except
  56. where overridden by an explicit C<PREPROCESSOR> option.
  57. C<preprocessor()> returns the previous default preprocessor function,
  58. or undefined if there wasn't one. When invoked with no arguments,
  59. C<preprocessor()> returns the object's current default preprocessor
  60. function without changing it.
  61. In all other respects, C<Text::Template::Preprocess> is identical to
  62. C<Text::Template>.
  63. =head1 WHY?
  64. One possible purpose: If your files contain a lot of JavaScript, like
  65. this:
  66. Plain text here...
  67. { perl code }
  68. <script language=JavaScript>
  69. if (br== "n3") {
  70. // etc.
  71. }
  72. </script>
  73. { more perl code }
  74. More plain text...
  75. You don't want C<Text::Template> to confuse the curly braces in the
  76. JavaScript program with executable Perl code. One strategy:
  77. sub quote_scripts {
  78. s(<script(.*?)</script>)(q{$1})gsi;
  79. }
  80. Then use C<PREPROCESSOR =E<gt> \&quote_scripts>. This will transform
  81. =head1 SEE ALSO
  82. L<Text::Template>
  83. =head1 SOURCE
  84. The development version is on github at L<https://https://github.com/mschout/perl-text-template>
  85. and may be cloned from L<git://https://github.com/mschout/perl-text-template.git>
  86. =head1 BUGS
  87. Please report any bugs or feature requests on the bugtracker website
  88. L<https://github.com/mschout/perl-text-template/issues>
  89. When submitting a bug or request, please include a test-file or a
  90. patch to an existing test-file that illustrates the bug or desired
  91. feature.
  92. =head1 AUTHOR
  93. Mark Jason Dominus, Plover Systems
  94. Please send questions and other remarks about this software to
  95. C<mjd-perl-template+@plover.com>
  96. You can join a very low-volume (E<lt>10 messages per year) mailing
  97. list for announcements about this package. Send an empty note to
  98. C<mjd-perl-template-request@plover.com> to join.
  99. For updates, visit C<http://www.plover.com/~mjd/perl/Template/>.
  100. =head1 COPYRIGHT AND LICENSE
  101. This software is copyright (c) 2013 by Mark Jason Dominus <mjd@cpan.org>.
  102. This is free software; you can redistribute it and/or modify it under
  103. the same terms as the Perl 5 programming language system itself.
  104. =cut