Subject: | T::T::Preprocess, &pp called only once, $_ contains entire template |
As tested, the code ref given to the PREPROCESSOR option is called only
once, at which time $_ contains the entire template code.
According to the docs:
The preprocessor subroutine will be called repeatedly, once for each
program fragment. The program fragment will be in $_. The subroutine
should modify the contents of $_ and return. Text::Template::Preprocess
will then execute contents of $_ and insert the result into the
appropriate part of the template.
Distribution: Debian Linux (Etch), Perl v5.8.0 built for i686-linux-
thread-multi
Test results:
$ perl -w test_frag2
got the whole thing
Form start
joel
Some stuff
48
Some more stuff
20000
End of stuff
fragments counted: 1
Test code:
#!/usr/bin/env perl
use 5.008;
use warnings;
use Text::Template::Preprocess;
my $count = 0;;
my $template = Text::Template::Preprocess->new(
TYPE => 'STRING',
SOURCE => &template,
);
#($name,$age,$income) = qw( joel 48 20000 ); # ??? XX
my $hash = { name => 'joel', age => 48, income => 20000};
my $text = $template->fill_in(
HASH => $hash,
PREPROCESSOR => \&preprocess,
);
print $text;
print "fragments counted: $count\n";
sub preprocess { ++$count, $_ eq &template and print "got the whole
thing\n"; };
sub template { <<'TEMPLATE'};
Form start
{ $name }
Some stuff
{ $age }
Some more stuff
{ $income }
End of stuff
TEMPLATE