Template.pm 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364
  1. # -*- perl -*-
  2. # Text::Template.pm
  3. #
  4. # Fill in `templates'
  5. #
  6. # Copyright 2013 M. J. Dominus.
  7. # You may copy and distribute this program under the
  8. # same terms as Perl itself.
  9. # If in doubt, write to mjd-perl-template+@plover.com for a license.
  10. #
  11. package Text::Template;
  12. $Text::Template::VERSION = '1.59';
  13. # ABSTRACT: Expand template text with embedded Perl
  14. use strict;
  15. use warnings;
  16. require 5.008;
  17. use base 'Exporter';
  18. our @EXPORT_OK = qw(fill_in_file fill_in_string TTerror);
  19. our $ERROR;
  20. my %GLOBAL_PREPEND = ('Text::Template' => '');
  21. sub Version {
  22. $Text::Template::VERSION;
  23. }
  24. sub _param {
  25. my ($k, %h) = @_;
  26. for my $kk ($k, "\u$k", "\U$k", "-$k", "-\u$k", "-\U$k") {
  27. return $h{$kk} if exists $h{$kk};
  28. }
  29. return undef;
  30. }
  31. sub always_prepend {
  32. my $pack = shift;
  33. my $old = $GLOBAL_PREPEND{$pack};
  34. $GLOBAL_PREPEND{$pack} = shift;
  35. $old;
  36. }
  37. {
  38. my %LEGAL_TYPE;
  39. BEGIN {
  40. %LEGAL_TYPE = map { $_ => 1 } qw(FILE FILEHANDLE STRING ARRAY);
  41. }
  42. sub new {
  43. my ($pack, %a) = @_;
  44. my $stype = uc(_param('type', %a) || "FILE");
  45. my $source = _param('source', %a);
  46. my $untaint = _param('untaint', %a);
  47. my $prepend = _param('prepend', %a);
  48. my $alt_delim = _param('delimiters', %a);
  49. my $broken = _param('broken', %a);
  50. my $encoding = _param('encoding', %a);
  51. unless (defined $source) {
  52. require Carp;
  53. Carp::croak("Usage: $ {pack}::new(TYPE => ..., SOURCE => ...)");
  54. }
  55. unless ($LEGAL_TYPE{$stype}) {
  56. require Carp;
  57. Carp::croak("Illegal value `$stype' for TYPE parameter");
  58. }
  59. my $self = {
  60. TYPE => $stype,
  61. PREPEND => $prepend,
  62. UNTAINT => $untaint,
  63. BROKEN => $broken,
  64. ENCODING => $encoding,
  65. (defined $alt_delim ? (DELIM => $alt_delim) : ())
  66. };
  67. # Under 5.005_03, if any of $stype, $prepend, $untaint, or $broken
  68. # are tainted, all the others become tainted too as a result of
  69. # sharing the expression with them. We install $source separately
  70. # to prevent it from acquiring a spurious taint.
  71. $self->{SOURCE} = $source;
  72. bless $self => $pack;
  73. return unless $self->_acquire_data;
  74. $self;
  75. }
  76. }
  77. # Convert template objects of various types to type STRING,
  78. # in which the template data is embedded in the object itself.
  79. sub _acquire_data {
  80. my $self = shift;
  81. my $type = $self->{TYPE};
  82. if ($type eq 'STRING') {
  83. # nothing necessary
  84. }
  85. elsif ($type eq 'FILE') {
  86. my $data = _load_text($self->{SOURCE});
  87. unless (defined $data) {
  88. # _load_text already set $ERROR
  89. return undef;
  90. }
  91. if ($self->{UNTAINT} && _is_clean($self->{SOURCE})) {
  92. _unconditionally_untaint($data);
  93. }
  94. if (defined $self->{ENCODING}) {
  95. require Encode;
  96. $data = Encode::decode($self->{ENCODING}, $data, &Encode::FB_CROAK);
  97. }
  98. $self->{TYPE} = 'STRING';
  99. $self->{FILENAME} = $self->{SOURCE};
  100. $self->{SOURCE} = $data;
  101. }
  102. elsif ($type eq 'ARRAY') {
  103. $self->{TYPE} = 'STRING';
  104. $self->{SOURCE} = join '', @{ $self->{SOURCE} };
  105. }
  106. elsif ($type eq 'FILEHANDLE') {
  107. $self->{TYPE} = 'STRING';
  108. local $/;
  109. my $fh = $self->{SOURCE};
  110. my $data = <$fh>; # Extra assignment avoids bug in Solaris perl5.00[45].
  111. if ($self->{UNTAINT}) {
  112. _unconditionally_untaint($data);
  113. }
  114. $self->{SOURCE} = $data;
  115. }
  116. else {
  117. # This should have been caught long ago, so it represents a
  118. # drastic `can't-happen' sort of failure
  119. my $pack = ref $self;
  120. die "Can only acquire data for $pack objects of subtype STRING, but this is $type; aborting";
  121. }
  122. $self->{DATA_ACQUIRED} = 1;
  123. }
  124. sub source {
  125. my $self = shift;
  126. $self->_acquire_data unless $self->{DATA_ACQUIRED};
  127. return $self->{SOURCE};
  128. }
  129. sub set_source_data {
  130. my ($self, $newdata, $type) = @_;
  131. $self->{SOURCE} = $newdata;
  132. $self->{DATA_ACQUIRED} = 1;
  133. $self->{TYPE} = $type || 'STRING';
  134. 1;
  135. }
  136. sub compile {
  137. my $self = shift;
  138. return 1 if $self->{TYPE} eq 'PREPARSED';
  139. return undef unless $self->_acquire_data;
  140. unless ($self->{TYPE} eq 'STRING') {
  141. my $pack = ref $self;
  142. # This should have been caught long ago, so it represents a
  143. # drastic `can't-happen' sort of failure
  144. die "Can only compile $pack objects of subtype STRING, but this is $self->{TYPE}; aborting";
  145. }
  146. my @tokens;
  147. my $delim_pats = shift() || $self->{DELIM};
  148. my ($t_open, $t_close) = ('{', '}');
  149. my $DELIM; # Regex matches a delimiter if $delim_pats
  150. if (defined $delim_pats) {
  151. ($t_open, $t_close) = @$delim_pats;
  152. $DELIM = "(?:(?:\Q$t_open\E)|(?:\Q$t_close\E))";
  153. @tokens = split /($DELIM|\n)/, $self->{SOURCE};
  154. }
  155. else {
  156. @tokens = split /(\\\\(?=\\*[{}])|\\[{}]|[{}\n])/, $self->{SOURCE};
  157. }
  158. my $state = 'TEXT';
  159. my $depth = 0;
  160. my $lineno = 1;
  161. my @content;
  162. my $cur_item = '';
  163. my $prog_start;
  164. while (@tokens) {
  165. my $t = shift @tokens;
  166. next if $t eq '';
  167. if ($t eq $t_open) { # Brace or other opening delimiter
  168. if ($depth == 0) {
  169. push @content, [ $state, $cur_item, $lineno ] if $cur_item ne '';
  170. $cur_item = '';
  171. $state = 'PROG';
  172. $prog_start = $lineno;
  173. }
  174. else {
  175. $cur_item .= $t;
  176. }
  177. $depth++;
  178. }
  179. elsif ($t eq $t_close) { # Brace or other closing delimiter
  180. $depth--;
  181. if ($depth < 0) {
  182. $ERROR = "Unmatched close brace at line $lineno";
  183. return undef;
  184. }
  185. elsif ($depth == 0) {
  186. push @content, [ $state, $cur_item, $prog_start ] if $cur_item ne '';
  187. $state = 'TEXT';
  188. $cur_item = '';
  189. }
  190. else {
  191. $cur_item .= $t;
  192. }
  193. }
  194. elsif (!$delim_pats && $t eq '\\\\') { # precedes \\\..\\\{ or \\\..\\\}
  195. $cur_item .= '\\';
  196. }
  197. elsif (!$delim_pats && $t =~ /^\\([{}])$/) { # Escaped (literal) brace?
  198. $cur_item .= $1;
  199. }
  200. elsif ($t eq "\n") { # Newline
  201. $lineno++;
  202. $cur_item .= $t;
  203. }
  204. else { # Anything else
  205. $cur_item .= $t;
  206. }
  207. }
  208. if ($state eq 'PROG') {
  209. $ERROR = "End of data inside program text that began at line $prog_start";
  210. return undef;
  211. }
  212. elsif ($state eq 'TEXT') {
  213. push @content, [ $state, $cur_item, $lineno ] if $cur_item ne '';
  214. }
  215. else {
  216. die "Can't happen error #1";
  217. }
  218. $self->{TYPE} = 'PREPARSED';
  219. $self->{SOURCE} = \@content;
  220. 1;
  221. }
  222. sub prepend_text {
  223. my $self = shift;
  224. my $t = $self->{PREPEND};
  225. unless (defined $t) {
  226. $t = $GLOBAL_PREPEND{ ref $self };
  227. unless (defined $t) {
  228. $t = $GLOBAL_PREPEND{'Text::Template'};
  229. }
  230. }
  231. $self->{PREPEND} = $_[1] if $#_ >= 1;
  232. return $t;
  233. }
  234. sub fill_in {
  235. my ($fi_self, %fi_a) = @_;
  236. unless ($fi_self->{TYPE} eq 'PREPARSED') {
  237. my $delims = _param('delimiters', %fi_a);
  238. my @delim_arg = (defined $delims ? ($delims) : ());
  239. $fi_self->compile(@delim_arg)
  240. or return undef;
  241. }
  242. my $fi_varhash = _param('hash', %fi_a);
  243. my $fi_package = _param('package', %fi_a);
  244. my $fi_broken = _param('broken', %fi_a) || $fi_self->{BROKEN} || \&_default_broken;
  245. my $fi_broken_arg = _param('broken_arg', %fi_a) || [];
  246. my $fi_safe = _param('safe', %fi_a);
  247. my $fi_ofh = _param('output', %fi_a);
  248. my $fi_filename = _param('filename', %fi_a) || $fi_self->{FILENAME} || 'template';
  249. my $fi_strict = _param('strict', %fi_a);
  250. my $fi_prepend = _param('prepend', %fi_a);
  251. my $fi_eval_package;
  252. my $fi_scrub_package = 0;
  253. unless (defined $fi_prepend) {
  254. $fi_prepend = $fi_self->prepend_text;
  255. }
  256. if (defined $fi_safe) {
  257. $fi_eval_package = 'main';
  258. }
  259. elsif (defined $fi_package) {
  260. $fi_eval_package = $fi_package;
  261. }
  262. elsif (defined $fi_varhash) {
  263. $fi_eval_package = _gensym();
  264. $fi_scrub_package = 1;
  265. }
  266. else {
  267. $fi_eval_package = caller;
  268. }
  269. my @fi_varlist;
  270. my $fi_install_package;
  271. if (defined $fi_varhash) {
  272. if (defined $fi_package) {
  273. $fi_install_package = $fi_package;
  274. }
  275. elsif (defined $fi_safe) {
  276. $fi_install_package = $fi_safe->root;
  277. }
  278. else {
  279. $fi_install_package = $fi_eval_package; # The gensymmed one
  280. }
  281. @fi_varlist = _install_hash($fi_varhash => $fi_install_package);
  282. if ($fi_strict) {
  283. $fi_prepend = "use vars qw(@fi_varlist);$fi_prepend" if @fi_varlist;
  284. $fi_prepend = "use strict;$fi_prepend";
  285. }
  286. }
  287. if (defined $fi_package && defined $fi_safe) {
  288. no strict 'refs';
  289. # Big fat magic here: Fix it so that the user-specified package
  290. # is the default one available in the safe compartment.
  291. *{ $fi_safe->root . '::' } = \%{ $fi_package . '::' }; # LOD
  292. }
  293. my $fi_r = '';
  294. my $fi_item;
  295. foreach $fi_item (@{ $fi_self->{SOURCE} }) {
  296. my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;
  297. if ($fi_type eq 'TEXT') {
  298. $fi_self->append_text_to_output(
  299. text => $fi_text,
  300. handle => $fi_ofh,
  301. out => \$fi_r,
  302. type => $fi_type,);
  303. }
  304. elsif ($fi_type eq 'PROG') {
  305. no strict;
  306. my $fi_lcomment = "#line $fi_lineno $fi_filename";
  307. my $fi_progtext = "package $fi_eval_package; $fi_prepend;\n$fi_lcomment\n$fi_text;\n;";
  308. my $fi_res;
  309. my $fi_eval_err = '';
  310. if ($fi_safe) {
  311. no strict;
  312. no warnings;
  313. $fi_safe->reval(q{undef $OUT});
  314. $fi_res = $fi_safe->reval($fi_progtext);
  315. $fi_eval_err = $@;
  316. my $OUT = $fi_safe->reval('$OUT');
  317. $fi_res = $OUT if defined $OUT;
  318. }
  319. else {
  320. no strict;
  321. no warnings;
  322. my $OUT;
  323. $fi_res = eval $fi_progtext;
  324. $fi_eval_err = $@;
  325. $fi_res = $OUT if defined $OUT;
  326. }
  327. # If the value of the filled-in text really was undef,
  328. # change it to an explicit empty string to avoid undefined
  329. # value warnings later.
  330. $fi_res = '' unless defined $fi_res;
  331. if ($fi_eval_err) {
  332. $fi_res = $fi_broken->(
  333. text => $fi_text,
  334. error => $fi_eval_err,
  335. lineno => $fi_lineno,
  336. arg => $fi_broken_arg,);
  337. if (defined $fi_res) {
  338. $fi_self->append_text_to_output(
  339. text => $fi_res,
  340. handle => $fi_ofh,
  341. out => \$fi_r,
  342. type => $fi_type,);
  343. }
  344. else {
  345. return $fi_r; # Undefined means abort processing
  346. }
  347. }
  348. else {
  349. $fi_self->append_text_to_output(
  350. text => $fi_res,
  351. handle => $fi_ofh,
  352. out => \$fi_r,
  353. type => $fi_type,);
  354. }
  355. }
  356. else {
  357. die "Can't happen error #2";
  358. }
  359. }
  360. _scrubpkg($fi_eval_package) if $fi_scrub_package;
  361. defined $fi_ofh ? 1 : $fi_r;
  362. }
  363. sub append_text_to_output {
  364. my ($self, %arg) = @_;
  365. if (defined $arg{handle}) {
  366. print { $arg{handle} } $arg{text};
  367. }
  368. else {
  369. ${ $arg{out} } .= $arg{text};
  370. }
  371. return;
  372. }
  373. sub fill_this_in {
  374. my ($pack, $text) = splice @_, 0, 2;
  375. my $templ = $pack->new(TYPE => 'STRING', SOURCE => $text, @_)
  376. or return undef;
  377. $templ->compile or return undef;
  378. my $result = $templ->fill_in(@_);
  379. $result;
  380. }
  381. sub fill_in_string {
  382. my $string = shift;
  383. my $package = _param('package', @_);
  384. push @_, 'package' => scalar(caller) unless defined $package;
  385. Text::Template->fill_this_in($string, @_);
  386. }
  387. sub fill_in_file {
  388. my $fn = shift;
  389. my $templ = Text::Template->new(TYPE => 'FILE', SOURCE => $fn, @_) or return undef;
  390. $templ->compile or return undef;
  391. my $text = $templ->fill_in(@_);
  392. $text;
  393. }
  394. sub _default_broken {
  395. my %a = @_;
  396. my $prog_text = $a{text};
  397. my $err = $a{error};
  398. my $lineno = $a{lineno};
  399. chomp $err;
  400. # $err =~ s/\s+at .*//s;
  401. "Program fragment delivered error ``$err''";
  402. }
  403. sub _load_text {
  404. my $fn = shift;
  405. open my $fh, '<', $fn or do {
  406. $ERROR = "Couldn't open file $fn: $!";
  407. return undef;
  408. };
  409. local $/;
  410. <$fh>;
  411. }
  412. sub _is_clean {
  413. my $z;
  414. eval { ($z = join('', @_)), eval '#' . substr($z, 0, 0); 1 } # LOD
  415. }
  416. sub _unconditionally_untaint {
  417. for (@_) {
  418. ($_) = /(.*)/s;
  419. }
  420. }
  421. {
  422. my $seqno = 0;
  423. sub _gensym {
  424. __PACKAGE__ . '::GEN' . $seqno++;
  425. }
  426. sub _scrubpkg {
  427. my $s = shift;
  428. $s =~ s/^Text::Template:://;
  429. no strict 'refs';
  430. my $hash = $Text::Template::{ $s . "::" };
  431. foreach my $key (keys %$hash) {
  432. undef $hash->{$key};
  433. }
  434. %$hash = ();
  435. delete $Text::Template::{ $s . "::" };
  436. }
  437. }
  438. # Given a hashful of variables (or a list of such hashes)
  439. # install the variables into the specified package,
  440. # overwriting whatever variables were there before.
  441. sub _install_hash {
  442. my $hashlist = shift;
  443. my $dest = shift;
  444. if (UNIVERSAL::isa($hashlist, 'HASH')) {
  445. $hashlist = [$hashlist];
  446. }
  447. my @varlist;
  448. for my $hash (@$hashlist) {
  449. for my $name (keys %$hash) {
  450. my $val = $hash->{$name};
  451. no strict 'refs';
  452. no warnings 'redefine';
  453. local *SYM = *{"$ {dest}::$name"};
  454. if (!defined $val) {
  455. delete ${"$ {dest}::"}{$name};
  456. my $match = qr/^.\Q$name\E$/;
  457. @varlist = grep { $_ !~ $match } @varlist;
  458. }
  459. elsif (ref $val) {
  460. *SYM = $val;
  461. push @varlist, do {
  462. if (UNIVERSAL::isa($val, 'ARRAY')) { '@' }
  463. elsif (UNIVERSAL::isa($val, 'HASH')) { '%' }
  464. else { '$' }
  465. }
  466. . $name;
  467. }
  468. else {
  469. *SYM = \$val;
  470. push @varlist, '$' . $name;
  471. }
  472. }
  473. }
  474. @varlist;
  475. }
  476. sub TTerror { $ERROR }
  477. 1;
  478. __END__
  479. =pod
  480. =encoding UTF-8
  481. =head1 NAME
  482. Text::Template - Expand template text with embedded Perl
  483. =head1 VERSION
  484. version 1.59
  485. =head1 SYNOPSIS
  486. use Text::Template;
  487. $template = Text::Template->new(TYPE => 'FILE', SOURCE => 'filename.tmpl');
  488. $template = Text::Template->new(TYPE => 'ARRAY', SOURCE => [ ... ] );
  489. $template = Text::Template->new(TYPE => 'FILEHANDLE', SOURCE => $fh );
  490. $template = Text::Template->new(TYPE => 'STRING', SOURCE => '...' );
  491. $template = Text::Template->new(PREPEND => q{use strict;}, ...);
  492. # Use a different template file syntax:
  493. $template = Text::Template->new(DELIMITERS => [$open, $close], ...);
  494. $recipient = 'King';
  495. $text = $template->fill_in(); # Replaces `{$recipient}' with `King'
  496. print $text;
  497. $T::recipient = 'Josh';
  498. $text = $template->fill_in(PACKAGE => T);
  499. # Pass many variables explicitly
  500. $hash = { recipient => 'Abed-Nego',
  501. friends => [ 'me', 'you' ],
  502. enemies => { loathsome => 'Saruman',
  503. fearsome => 'Sauron' },
  504. };
  505. $text = $template->fill_in(HASH => $hash, ...);
  506. # $recipient is Abed-Nego,
  507. # @friends is ( 'me', 'you' ),
  508. # %enemies is ( loathsome => ..., fearsome => ... )
  509. # Call &callback in case of programming errors in template
  510. $text = $template->fill_in(BROKEN => \&callback, BROKEN_ARG => $ref, ...);
  511. # Evaluate program fragments in Safe compartment with restricted permissions
  512. $text = $template->fill_in(SAFE => $compartment, ...);
  513. # Print result text instead of returning it
  514. $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
  515. # Parse template with different template file syntax:
  516. $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
  517. # Note that this is *faster* than using the default delimiters
  518. # Prepend specified perl code to each fragment before evaluating:
  519. $text = $template->fill_in(PREPEND => q{use strict 'vars';}, ...);
  520. use Text::Template 'fill_in_string';
  521. $text = fill_in_string( <<'EOM', PACKAGE => 'T', ...);
  522. Dear {$recipient},
  523. Pay me at once.
  524. Love,
  525. G.V.
  526. EOM
  527. use Text::Template 'fill_in_file';
  528. $text = fill_in_file($filename, ...);
  529. # All templates will always have `use strict vars' attached to all fragments
  530. Text::Template->always_prepend(q{use strict 'vars';});
  531. =head1 DESCRIPTION
  532. This is a library for generating form letters, building HTML pages, or
  533. filling in templates generally. A `template' is a piece of text that
  534. has little Perl programs embedded in it here and there. When you
  535. `fill in' a template, you evaluate the little programs and replace
  536. them with their values.
  537. You can store a template in a file outside your program. People can
  538. modify the template without modifying the program. You can separate
  539. the formatting details from the main code, and put the formatting
  540. parts of the program into the template. That prevents code bloat and
  541. encourages functional separation.
  542. =head2 Example
  543. Here's an example of a template, which we'll suppose is stored in the
  544. file C<formletter.tmpl>:
  545. Dear {$title} {$lastname},
  546. It has come to our attention that you are delinquent in your
  547. {$monthname[$last_paid_month]} payment. Please remit
  548. ${sprintf("%.2f", $amount)} immediately, or your patellae may
  549. be needlessly endangered.
  550. Love,
  551. Mark "Vizopteryx" Dominus
  552. The result of filling in this template is a string, which might look
  553. something like this:
  554. Dear Mr. Smith,
  555. It has come to our attention that you are delinquent in your
  556. February payment. Please remit
  557. $392.12 immediately, or your patellae may
  558. be needlessly endangered.
  559. Love,
  560. Mark "Vizopteryx" Dominus
  561. Here is a complete program that transforms the example
  562. template into the example result, and prints it out:
  563. use Text::Template;
  564. my $template = Text::Template->new(SOURCE => 'formletter.tmpl')
  565. or die "Couldn't construct template: $Text::Template::ERROR";
  566. my @monthname = qw(January February March April May June
  567. July August September October November December);
  568. my %vars = (title => 'Mr.',
  569. firstname => 'John',
  570. lastname => 'Smith',
  571. last_paid_month => 1, # February
  572. amount => 392.12,
  573. monthname => \@monthname);
  574. my $result = $template->fill_in(HASH => \%vars);
  575. if (defined $result) { print $result }
  576. else { die "Couldn't fill in template: $Text::Template::ERROR" }
  577. =head2 Philosophy
  578. When people make a template module like this one, they almost always
  579. start by inventing a special syntax for substitutions. For example,
  580. they build it so that a string like C<%%VAR%%> is replaced with the
  581. value of C<$VAR>. Then they realize the need extra formatting, so
  582. they put in some special syntax for formatting. Then they need a
  583. loop, so they invent a loop syntax. Pretty soon they have a new
  584. little template language.
  585. This approach has two problems: First, their little language is
  586. crippled. If you need to do something the author hasn't thought of,
  587. you lose. Second: Who wants to learn another language? You already
  588. know Perl, so why not use it?
  589. C<Text::Template> templates are programmed in I<Perl>. You embed Perl
  590. code in your template, with C<{> at the beginning and C<}> at the end.
  591. If you want a variable interpolated, you write it the way you would in
  592. Perl. If you need to make a loop, you can use any of the Perl loop
  593. constructions. All the Perl built-in functions are available.
  594. =head1 Details
  595. =head2 Template Parsing
  596. The C<Text::Template> module scans the template source. An open brace
  597. C<{> begins a program fragment, which continues until the matching
  598. close brace C<}>. When the template is filled in, the program
  599. fragments are evaluated, and each one is replaced with the resulting
  600. value to yield the text that is returned.
  601. A backslash C<\> in front of a brace (or another backslash that is in
  602. front of a brace) escapes its special meaning. The result of filling
  603. out this template:
  604. \{ The sum of 1 and 2 is {1+2} \}
  605. is
  606. { The sum of 1 and 2 is 3 }
  607. If you have an unmatched brace, C<Text::Template> will return a
  608. failure code and a warning about where the problem is. Backslashes
  609. that do not precede a brace are passed through unchanged. If you have
  610. a template like this:
  611. { "String that ends in a newline.\n" }
  612. The backslash inside the string is passed through to Perl unchanged,
  613. so the C<\n> really does turn into a newline. See the note at the end
  614. for details about the way backslashes work. Backslash processing is
  615. I<not> done when you specify alternative delimiters with the
  616. C<DELIMITERS> option. (See L<"Alternative Delimiters">, below.)
  617. Each program fragment should be a sequence of Perl statements, which
  618. are evaluated the usual way. The result of the last statement
  619. executed will be evaluated in scalar context; the result of this
  620. statement is a string, which is interpolated into the template in
  621. place of the program fragment itself.
  622. The fragments are evaluated in order, and side effects from earlier
  623. fragments will persist into later fragments:
  624. {$x = @things; ''}The Lord High Chamberlain has gotten {$x}
  625. things for me this year.
  626. { $diff = $x - 17;
  627. $more = 'more'
  628. if ($diff == 0) {
  629. $diff = 'no';
  630. } elsif ($diff < 0) {
  631. $more = 'fewer';
  632. }
  633. '';
  634. }
  635. That is {$diff} {$more} than he gave me last year.
  636. The value of C<$x> set in the first line will persist into the next
  637. fragment that begins on the third line, and the values of C<$diff> and
  638. C<$more> set in the second fragment will persist and be interpolated
  639. into the last line. The output will look something like this:
  640. The Lord High Chamberlain has gotten 42
  641. things for me this year.
  642. That is 25 more than he gave me last year.
  643. That is all the syntax there is.
  644. =head2 The C<$OUT> variable
  645. There is one special trick you can play in a template. Here is the
  646. motivation for it: Suppose you are going to pass an array, C<@items>,
  647. into the template, and you want the template to generate a bulleted
  648. list with a header, like this:
  649. Here is a list of the things I have got for you since 1907:
  650. * Ivory
  651. * Apes
  652. * Peacocks
  653. * ...
  654. One way to do it is with a template like this:
  655. Here is a list of the things I have got for you since 1907:
  656. { my $blist = '';
  657. foreach $i (@items) {
  658. $blist .= qq{ * $i\n};
  659. }
  660. $blist;
  661. }
  662. Here we construct the list in a variable called C<$blist>, which we
  663. return at the end. This is a little cumbersome. There is a shortcut.
  664. Inside of templates, there is a special variable called C<$OUT>.
  665. Anything you append to this variable will appear in the output of the
  666. template. Also, if you use C<$OUT> in a program fragment, the normal
  667. behavior, of replacing the fragment with its return value, is
  668. disabled; instead the fragment is replaced with the value of C<$OUT>.
  669. This means that you can write the template above like this:
  670. Here is a list of the things I have got for you since 1907:
  671. { foreach $i (@items) {
  672. $OUT .= " * $i\n";
  673. }
  674. }
  675. C<$OUT> is reinitialized to the empty string at the start of each
  676. program fragment. It is private to C<Text::Template>, so
  677. you can't use a variable named C<$OUT> in your template without
  678. invoking the special behavior.
  679. =head2 General Remarks
  680. All C<Text::Template> functions return C<undef> on failure, and set the
  681. variable C<$Text::Template::ERROR> to contain an explanation of what
  682. went wrong. For example, if you try to create a template from a file
  683. that does not exist, C<$Text::Template::ERROR> will contain something like:
  684. Couldn't open file xyz.tmpl: No such file or directory
  685. =head2 C<new>
  686. $template = Text::Template->new( TYPE => ..., SOURCE => ... );
  687. This creates and returns a new template object. C<new> returns
  688. C<undef> and sets C<$Text::Template::ERROR> if it can't create the
  689. template object. C<SOURCE> says where the template source code will
  690. come from. C<TYPE> says what kind of object the source is.
  691. The most common type of source is a file:
  692. Text::Template->new( TYPE => 'FILE', SOURCE => $filename );
  693. This reads the template from the specified file. The filename is
  694. opened with the Perl C<open> command, so it can be a pipe or anything
  695. else that makes sense with C<open>.
  696. The C<TYPE> can also be C<STRING>, in which case the C<SOURCE> should
  697. be a string:
  698. Text::Template->new( TYPE => 'STRING',
  699. SOURCE => "This is the actual template!" );
  700. The C<TYPE> can be C<ARRAY>, in which case the source should be a
  701. reference to an array of strings. The concatenation of these strings
  702. is the template:
  703. Text::Template->new( TYPE => 'ARRAY',
  704. SOURCE => [ "This is ", "the actual",
  705. " template!",
  706. ]
  707. );
  708. The C<TYPE> can be FILEHANDLE, in which case the source should be an
  709. open filehandle (such as you got from the C<FileHandle> or C<IO::*>
  710. packages, or a glob, or a reference to a glob). In this case
  711. C<Text::Template> will read the text from the filehandle up to
  712. end-of-file, and that text is the template:
  713. # Read template source code from STDIN:
  714. Text::Template->new ( TYPE => 'FILEHANDLE',
  715. SOURCE => \*STDIN );
  716. If you omit the C<TYPE> attribute, it's taken to be C<FILE>.
  717. C<SOURCE> is required. If you omit it, the program will abort.
  718. The words C<TYPE> and C<SOURCE> can be spelled any of the following ways:
  719. TYPE SOURCE
  720. Type Source
  721. type source
  722. -TYPE -SOURCE
  723. -Type -Source
  724. -type -source
  725. Pick a style you like and stick with it.
  726. =over 4
  727. =item C<DELIMITERS>
  728. You may also add a C<DELIMITERS> option. If this option is present,
  729. its value should be a reference to an array of two strings. The first
  730. string is the string that signals the beginning of each program
  731. fragment, and the second string is the string that signals the end of
  732. each program fragment. See L<"Alternative Delimiters">, below.
  733. =item C<ENCODING>
  734. You may also add a C<ENCODING> option. If this option is present, and the
  735. C<SOURCE> is a C<FILE>, then the data will be decoded from the given encoding
  736. using the L<Encode> module. You can use any encoding that L<Encode> recognizes.
  737. E.g.:
  738. Text::Template->new(
  739. TYPE => 'FILE',
  740. ENCODING => 'UTF-8',
  741. SOURCE => 'xyz.tmpl');
  742. =item C<UNTAINT>
  743. If your program is running in taint mode, you may have problems if
  744. your templates are stored in files. Data read from files is
  745. considered 'untrustworthy', and taint mode will not allow you to
  746. evaluate the Perl code in the file. (It is afraid that a malicious
  747. person might have tampered with the file.)
  748. In some environments, however, local files are trustworthy. You can
  749. tell C<Text::Template> that a certain file is trustworthy by supplying
  750. C<UNTAINT =E<gt> 1> in the call to C<new>. This will tell
  751. C<Text::Template> to disable taint checks on template code that has
  752. come from a file, as long as the filename itself is considered
  753. trustworthy. It will also disable taint checks on template code that
  754. comes from a filehandle. When used with C<TYPE =E<gt> 'string'> or C<TYPE
  755. =E<gt> 'array'>, it has no effect.
  756. See L<perlsec> for more complete information about tainting.
  757. Thanks to Steve Palincsar, Gerard Vreeswijk, and Dr. Christoph Baehr
  758. for help with this feature.
  759. =item C<PREPEND>
  760. This option is passed along to the C<fill_in> call unless it is
  761. overridden in the arguments to C<fill_in>. See L<C<PREPEND> feature
  762. and using C<strict> in templates> below.
  763. =item C<BROKEN>
  764. This option is passed along to the C<fill_in> call unless it is
  765. overridden in the arguments to C<fill_in>. See L<C<BROKEN>> below.
  766. =back
  767. =head2 C<compile>
  768. $template->compile()
  769. Loads all the template text from the template's source, parses and
  770. compiles it. If successful, returns true; otherwise returns false and
  771. sets C<$Text::Template::ERROR>. If the template is already compiled,
  772. it returns true and does nothing.
  773. You don't usually need to invoke this function, because C<fill_in>
  774. (see below) compiles the template if it isn't compiled already.
  775. If there is an argument to this function, it must be a reference to an
  776. array containing alternative delimiter strings. See C<"Alternative
  777. Delimiters">, below.
  778. =head2 C<fill_in>
  779. $template->fill_in(OPTIONS);
  780. Fills in a template. Returns the resulting text if successful.
  781. Otherwise, returns C<undef> and sets C<$Text::Template::ERROR>.
  782. The I<OPTIONS> are a hash, or a list of key-value pairs. You can
  783. write the key names in any of the six usual styles as above; this
  784. means that where this manual says C<PACKAGE> (for example) you can
  785. actually use any of
  786. PACKAGE Package package -PACKAGE -Package -package
  787. Pick a style you like and stick with it. The all-lowercase versions
  788. may yield spurious warnings about
  789. Ambiguous use of package => resolved to "package"
  790. so you might like to avoid them and use the capitalized versions.
  791. At present, there are eight legal options: C<PACKAGE>, C<BROKEN>,
  792. C<BROKEN_ARG>, C<FILENAME>, C<SAFE>, C<HASH>, C<OUTPUT>, and C<DELIMITERS>.
  793. =over 4
  794. =item C<PACKAGE>
  795. C<PACKAGE> specifies the name of a package in which the program
  796. fragments should be evaluated. The default is to use the package from
  797. which C<fill_in> was called. For example, consider this template:
  798. The value of the variable x is {$x}.
  799. If you use C<$template-E<gt>fill_in(PACKAGE =E<gt> 'R')> , then the C<$x> in
  800. the template is actually replaced with the value of C<$R::x>. If you
  801. omit the C<PACKAGE> option, C<$x> will be replaced with the value of
  802. the C<$x> variable in the package that actually called C<fill_in>.
  803. You should almost always use C<PACKAGE>. If you don't, and your
  804. template makes changes to variables, those changes will be propagated
  805. back into the main program. Evaluating the template in a private
  806. package helps prevent this. The template can still modify variables
  807. in your program if it wants to, but it will have to do so explicitly.
  808. See the section at the end on `Security'.
  809. Here's an example of using C<PACKAGE>:
  810. Your Royal Highness,
  811. Enclosed please find a list of things I have gotten
  812. for you since 1907:
  813. { foreach $item (@items) {
  814. $item_no++;
  815. $OUT .= " $item_no. \u$item\n";
  816. }
  817. }
  818. Signed,
  819. Lord High Chamberlain
  820. We want to pass in an array which will be assigned to the array
  821. C<@items>. Here's how to do that:
  822. @items = ('ivory', 'apes', 'peacocks', );
  823. $template->fill_in();
  824. This is not very safe. The reason this isn't as safe is that if you
  825. had a variable named C<$item_no> in scope in your program at the point
  826. you called C<fill_in>, its value would be clobbered by the act of
  827. filling out the template. The problem is the same as if you had
  828. written a subroutine that used those variables in the same way that
  829. the template does. (C<$OUT> is special in templates and is always
  830. safe.)
  831. One solution to this is to make the C<$item_no> variable private to the
  832. template by declaring it with C<my>. If the template does this, you
  833. are safe.
  834. But if you use the C<PACKAGE> option, you will probably be safe even
  835. if the template does I<not> declare its variables with C<my>:
  836. @Q::items = ('ivory', 'apes', 'peacocks', );
  837. $template->fill_in(PACKAGE => 'Q');
  838. In this case the template will clobber the variable C<$Q::item_no>,
  839. which is not related to the one your program was using.
  840. Templates cannot affect variables in the main program that are
  841. declared with C<my>, unless you give the template references to those
  842. variables.
  843. =item C<HASH>
  844. You may not want to put the template variables into a package.
  845. Packages can be hard to manage: You can't copy them, for example.
  846. C<HASH> provides an alternative.
  847. The value for C<HASH> should be a reference to a hash that maps
  848. variable names to values. For example,
  849. $template->fill_in(
  850. HASH => {
  851. recipient => "The King",
  852. items => ['gold', 'frankincense', 'myrrh'],
  853. object => \$self,
  854. }
  855. );
  856. will fill out the template and use C<"The King"> as the value of
  857. C<$recipient> and the list of items as the value of C<@items>. Note
  858. that we pass an array reference, but inside the template it appears as
  859. an array. In general, anything other than a simple string or number
  860. should be passed by reference.
  861. We also want to pass an object, which is in C<$self>; note that we
  862. pass a reference to the object, C<\$self> instead. Since we've passed
  863. a reference to a scalar, inside the template the object appears as
  864. C<$object>.
  865. The full details of how it works are a little involved, so you might
  866. want to skip to the next section.
  867. Suppose the key in the hash is I<key> and the value is I<value>.
  868. =over 4
  869. =item *
  870. If the I<value> is C<undef>, then any variables named C<$key>,
  871. C<@key>, C<%key>, etc., are undefined.
  872. =item *
  873. If the I<value> is a string or a number, then C<$key> is set to that
  874. value in the template.
  875. =item *
  876. For anything else, you must pass a reference.
  877. If the I<value> is a reference to an array, then C<@key> is set to
  878. that array. If the I<value> is a reference to a hash, then C<%key> is
  879. set to that hash. Similarly if I<value> is any other kind of
  880. reference. This means that
  881. var => "foo"
  882. and
  883. var => \"foo"
  884. have almost exactly the same effect. (The difference is that in the
  885. former case, the value is copied, and in the latter case it is
  886. aliased.)
  887. =item *
  888. In particular, if you want the template to get an object or any kind,
  889. you must pass a reference to it:
  890. $template->fill_in(HASH => { database_handle => \$dbh, ... });
  891. If you do this, the template will have a variable C<$database_handle>
  892. which is the database handle object. If you leave out the C<\>, the
  893. template will have a hash C<%database_handle>, which exposes the
  894. internal structure of the database handle object; you don't want that.
  895. =back
  896. Normally, the way this works is by allocating a private package,
  897. loading all the variables into the package, and then filling out the
  898. template as if you had specified that package. A new package is
  899. allocated each time. However, if you I<also> use the C<PACKAGE>
  900. option, C<Text::Template> loads the variables into the package you
  901. specified, and they stay there after the call returns. Subsequent
  902. calls to C<fill_in> that use the same package will pick up the values
  903. you loaded in.
  904. If the argument of C<HASH> is a reference to an array instead of a
  905. reference to a hash, then the array should contain a list of hashes
  906. whose contents are loaded into the template package one after the
  907. other. You can use this feature if you want to combine several sets
  908. of variables. For example, one set of variables might be the defaults
  909. for a fill-in form, and the second set might be the user inputs, which
  910. override the defaults when they are present:
  911. $template->fill_in(HASH => [\%defaults, \%user_input]);
  912. You can also use this to set two variables with the same name:
  913. $template->fill_in(
  914. HASH => [
  915. { v => "The King" },
  916. { v => [1,2,3] }
  917. ]
  918. );
  919. This sets C<$v> to C<"The King"> and C<@v> to C<(1,2,3)>.
  920. =item C<BROKEN>
  921. If any of the program fragments fails to compile or aborts for any
  922. reason, and you have set the C<BROKEN> option to a function reference,
  923. C<Text::Template> will invoke the function. This function is called
  924. the I<C<BROKEN> function>. The C<BROKEN> function will tell
  925. C<Text::Template> what to do next.
  926. If the C<BROKEN> function returns C<undef>, C<Text::Template> will
  927. immediately abort processing the template and return the text that it
  928. has accumulated so far. If your function does this, it should set a
  929. flag that you can examine after C<fill_in> returns so that you can
  930. tell whether there was a premature return or not.
  931. If the C<BROKEN> function returns any other value, that value will be
  932. interpolated into the template as if that value had been the return
  933. value of the program fragment to begin with. For example, if the
  934. C<BROKEN> function returns an error string, the error string will be
  935. interpolated into the output of the template in place of the program
  936. fragment that cased the error.
  937. If you don't specify a C<BROKEN> function, C<Text::Template> supplies
  938. a default one that returns something like
  939. Program fragment delivered error ``Illegal division by 0 at
  940. template line 37''
  941. (Note that the format of this message has changed slightly since
  942. version 1.31.) The return value of the C<BROKEN> function is
  943. interpolated into the template at the place the error occurred, so
  944. that this template:
  945. (3+4)*5 = { 3+4)*5 }
  946. yields this result:
  947. (3+4)*5 = Program fragment delivered error ``syntax error at template line 1''
  948. If you specify a value for the C<BROKEN> attribute, it should be a
  949. reference to a function that C<fill_in> can call instead of the
  950. default function.
  951. C<fill_in> will pass a hash to the C<broken> function.
  952. The hash will have at least these three members:
  953. =over 4
  954. =item C<text>
  955. The source code of the program fragment that failed
  956. =item C<error>
  957. The text of the error message (C<$@>) generated by eval.
  958. The text has been modified to omit the trailing newline and to include
  959. the name of the template file (if there was one). The line number
  960. counts from the beginning of the template, not from the beginning of
  961. the failed program fragment.
  962. =item C<lineno>
  963. The line number of the template at which the program fragment began.
  964. =back
  965. There may also be an C<arg> member. See C<BROKEN_ARG>, below
  966. =item C<BROKEN_ARG>
  967. If you supply the C<BROKEN_ARG> option to C<fill_in>, the value of the
  968. option is passed to the C<BROKEN> function whenever it is called. The
  969. default C<BROKEN> function ignores the C<BROKEN_ARG>, but you can
  970. write a custom C<BROKEN> function that uses the C<BROKEN_ARG> to get
  971. more information about what went wrong.
  972. The C<BROKEN> function could also use the C<BROKEN_ARG> as a reference
  973. to store an error message or some other information that it wants to
  974. communicate back to the caller. For example:
  975. $error = '';
  976. sub my_broken {
  977. my %args = @_;
  978. my $err_ref = $args{arg};
  979. ...
  980. $$err_ref = "Some error message";
  981. return undef;
  982. }
  983. $template->fill_in(
  984. BROKEN => \&my_broken,
  985. BROKEN_ARG => \$error
  986. );
  987. if ($error) {
  988. die "It didn't work: $error";
  989. }
  990. If one of the program fragments in the template fails, it will call
  991. the C<BROKEN> function, C<my_broken>, and pass it the C<BROKEN_ARG>,
  992. which is a reference to C<$error>. C<my_broken> can store an error
  993. message into C<$error> this way. Then the function that called
  994. C<fill_in> can see if C<my_broken> has left an error message for it
  995. to find, and proceed accordingly.
  996. =item C<FILENAME>
  997. If you give C<fill_in> a C<FILENAME> option, then this is the file name that
  998. you loaded the template source from. This only affects the error message that
  999. is given for template errors. If you loaded the template from C<foo.txt> for
  1000. example, and pass C<foo.txt> as the C<FILENAME> parameter, errors will look
  1001. like C<... at foo.txt line N> rather than C<... at template line N>.
  1002. Note that this does NOT have anything to do with loading a template from the
  1003. given filename. See C<fill_in_file()> for that.
  1004. For example:
  1005. my $template = Text::Template->new(
  1006. TYPE => 'string',
  1007. SOURCE => 'The value is {1/0}');
  1008. $template->fill_in(FILENAME => 'foo.txt') or die $Text::Template::ERROR;
  1009. will die with an error that contains
  1010. Illegal division by zero at at foo.txt line 1
  1011. =item C<SAFE>
  1012. If you give C<fill_in> a C<SAFE> option, its value should be a safe
  1013. compartment object from the C<Safe> package. All evaluation of
  1014. program fragments will be performed in this compartment. See L<Safe>
  1015. for full details about such compartments and how to restrict the
  1016. operations that can be performed in them.
  1017. If you use the C<PACKAGE> option with C<SAFE>, the package you specify
  1018. will be placed into the safe compartment and evaluation will take
  1019. place in that package as usual.
  1020. If not, C<SAFE> operation is a little different from the default.
  1021. Usually, if you don't specify a package, evaluation of program
  1022. fragments occurs in the package from which the template was invoked.
  1023. But in C<SAFE> mode the evaluation occurs inside the safe compartment
  1024. and cannot affect the calling package. Normally, if you use C<HASH>
  1025. without C<PACKAGE>, the hash variables are imported into a private,
  1026. one-use-only package. But if you use C<HASH> and C<SAFE> together
  1027. without C<PACKAGE>, the hash variables will just be loaded into the
  1028. root namespace of the C<Safe> compartment.
  1029. =item C<OUTPUT>
  1030. If your template is going to generate a lot of text that you are just
  1031. going to print out again anyway, you can save memory by having
  1032. C<Text::Template> print out the text as it is generated instead of
  1033. making it into a big string and returning the string. If you supply
  1034. the C<OUTPUT> option to C<fill_in>, the value should be a filehandle.
  1035. The generated text will be printed to this filehandle as it is
  1036. constructed. For example:
  1037. $template->fill_in(OUTPUT => \*STDOUT, ...);
  1038. fills in the C<$template> as usual, but the results are immediately
  1039. printed to STDOUT. This may result in the output appearing more
  1040. quickly than it would have otherwise.
  1041. If you use C<OUTPUT>, the return value from C<fill_in> is still true on
  1042. success and false on failure, but the complete text is not returned to
  1043. the caller.
  1044. =item C<PREPEND>
  1045. You can have some Perl code prepended automatically to the beginning
  1046. of every program fragment. See L<C<PREPEND> feature and using
  1047. C<strict> in templates> below.
  1048. =item C<DELIMITERS>
  1049. If this option is present, its value should be a reference to a list
  1050. of two strings. The first string is the string that signals the
  1051. beginning of each program fragment, and the second string is the
  1052. string that signals the end of each program fragment. See
  1053. L<"Alternative Delimiters">, below.
  1054. If you specify C<DELIMITERS> in the call to C<fill_in>, they override
  1055. any delimiters you set when you created the template object with
  1056. C<new>.
  1057. =back
  1058. =head1 Convenience Functions
  1059. =head2 C<fill_this_in>
  1060. The basic way to fill in a template is to create a template object and
  1061. then call C<fill_in> on it. This is useful if you want to fill in
  1062. the same template more than once.
  1063. In some programs, this can be cumbersome. C<fill_this_in> accepts a
  1064. string, which contains the template, and a list of options, which are
  1065. passed to C<fill_in> as above. It constructs the template object for
  1066. you, fills it in as specified, and returns the results. It returns
  1067. C<undef> and sets C<$Text::Template::ERROR> if it couldn't generate
  1068. any results.
  1069. An example:
  1070. $Q::name = 'Donald';
  1071. $Q::amount = 141.61;
  1072. $Q::part = 'hyoid bone';
  1073. $text = Text::Template->fill_this_in( <<'EOM', PACKAGE => Q);
  1074. Dear {$name},
  1075. You owe me \\${sprintf('%.2f', $amount)}.
  1076. Pay or I will break your {$part}.
  1077. Love,
  1078. Grand Vizopteryx of Irkutsk.
  1079. EOM
  1080. Notice how we included the template in-line in the program by using a
  1081. `here document' with the C<E<lt>E<lt>> notation.
  1082. C<fill_this_in> is a deprecated feature. It is only here for
  1083. backwards compatibility, and may be removed in some far-future version
  1084. in C<Text::Template>. You should use C<fill_in_string> instead. It
  1085. is described in the next section.
  1086. =head2 C<fill_in_string>
  1087. It is stupid that C<fill_this_in> is a class method. It should have
  1088. been just an imported function, so that you could omit the
  1089. C<Text::Template-E<gt>> in the example above. But I made the mistake
  1090. four years ago and it is too late to change it.
  1091. C<fill_in_string> is exactly like C<fill_this_in> except that it is
  1092. not a method and you can omit the C<Text::Template-E<gt>> and just say
  1093. print fill_in_string(<<'EOM', ...);
  1094. Dear {$name},
  1095. ...
  1096. EOM
  1097. To use C<fill_in_string>, you need to say
  1098. use Text::Template 'fill_in_string';
  1099. at the top of your program. You should probably use
  1100. C<fill_in_string> instead of C<fill_this_in>.
  1101. =head2 C<fill_in_file>
  1102. If you import C<fill_in_file>, you can say
  1103. $text = fill_in_file(filename, ...);
  1104. The C<...> are passed to C<fill_in> as above. The filename is the
  1105. name of the file that contains the template you want to fill in. It
  1106. returns the result text. or C<undef>, as usual.
  1107. If you are going to fill in the same file more than once in the same
  1108. program you should use the longer C<new> / C<fill_in> sequence instead.
  1109. It will be a lot faster because it only has to read and parse the file
  1110. once.
  1111. =head2 Including files into templates
  1112. People always ask for this. ``Why don't you have an include
  1113. function?'' they want to know. The short answer is this is Perl, and
  1114. Perl already has an include function. If you want it, you can just put
  1115. {qx{cat filename}}
  1116. into your template. VoilE<agrave>.
  1117. If you don't want to use C<cat>, you can write a little four-line
  1118. function that opens a file and dumps out its contents, and call it
  1119. from the template. I wrote one for you. In the template, you can say
  1120. {Text::Template::_load_text(filename)}
  1121. If that is too verbose, here is a trick. Suppose the template package
  1122. that you are going to be mentioning in the C<fill_in> call is package
  1123. C<Q>. Then in the main program, write
  1124. *Q::include = \&Text::Template::_load_text;
  1125. This imports the C<_load_text> function into package C<Q> with the
  1126. name C<include>. From then on, any template that you fill in with
  1127. package C<Q> can say
  1128. {include(filename)}
  1129. to insert the text from the named file at that point. If you are
  1130. using the C<HASH> option instead, just put C<include =E<gt>
  1131. \&Text::Template::_load_text> into the hash instead of importing it
  1132. explicitly.
  1133. Suppose you don't want to insert a plain text file, but rather you
  1134. want to include one template within another? Just use C<fill_in_file>
  1135. in the template itself:
  1136. {Text::Template::fill_in_file(filename)}
  1137. You can do the same importing trick if this is too much to type.
  1138. =head1 Miscellaneous
  1139. =head2 C<my> variables
  1140. People are frequently surprised when this doesn't work:
  1141. my $recipient = 'The King';
  1142. my $text = fill_in_file('formletter.tmpl');
  1143. The text C<The King> doesn't get into the form letter. Why not?
  1144. Because C<$recipient> is a C<my> variable, and the whole point of
  1145. C<my> variables is that they're private and inaccessible except in the
  1146. scope in which they're declared. The template is not part of that
  1147. scope, so the template can't see C<$recipient>.
  1148. If that's not the behavior you want, don't use C<my>. C<my> means a
  1149. private variable, and in this case you don't want the variable to be
  1150. private. Put the variables into package variables in some other
  1151. package, and use the C<PACKAGE> option to C<fill_in>:
  1152. $Q::recipient = $recipient;
  1153. my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q');
  1154. or pass the names and values in a hash with the C<HASH> option:
  1155. my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient });
  1156. =head2 Security Matters
  1157. All variables are evaluated in the package you specify with the
  1158. C<PACKAGE> option of C<fill_in>. if you use this option, and if your
  1159. templates don't do anything egregiously stupid, you won't have to
  1160. worry that evaluation of the little programs will creep out into the
  1161. rest of your program and wreck something.
  1162. Nevertheless, there's really no way (except with C<Safe>) to protect
  1163. against a template that says
  1164. { $Important::Secret::Security::Enable = 0;
  1165. # Disable security checks in this program
  1166. }
  1167. or
  1168. { $/ = "ho ho ho"; # Sabotage future uses of <FH>.
  1169. # $/ is always a global variable
  1170. }
  1171. or even
  1172. { system("rm -rf /") }
  1173. so B<don't> go filling in templates unless you're sure you know what's
  1174. in them. If you're worried, or you can't trust the person who wrote
  1175. the template, use the C<SAFE> option.
  1176. A final warning: program fragments run a small risk of accidentally
  1177. clobbering local variables in the C<fill_in> function itself. These
  1178. variables all have names that begin with C<$fi_>, so if you stay away
  1179. from those names you'll be safe. (Of course, if you're a real wizard
  1180. you can tamper with them deliberately for exciting effects; this is
  1181. actually how C<$OUT> works.) I can fix this, but it will make the
  1182. package slower to do it, so I would prefer not to. If you are worried
  1183. about this, send me mail and I will show you what to do about it.
  1184. =head2 Alternative Delimiters
  1185. Lorenzo Valdettaro pointed out that if you are using C<Text::Template>
  1186. to generate TeX output, the choice of braces as the program fragment
  1187. delimiters makes you suffer suffer suffer. Starting in version 1.20,
  1188. you can change the choice of delimiters to something other than curly
  1189. braces.
  1190. In either the C<new()> call or the C<fill_in()> call, you can specify
  1191. an alternative set of delimiters with the C<DELIMITERS> option. For
  1192. example, if you would like code fragments to be delimited by C<[@-->
  1193. and C<--@]> instead of C<{> and C<}>, use
  1194. ... DELIMITERS => [ '[@--', '--@]' ], ...
  1195. Note that these delimiters are I<literal strings>, not regexes. (I
  1196. tried for regexes, but it complicates the lexical analysis too much.)
  1197. Note also that C<DELIMITERS> disables the special meaning of the
  1198. backslash, so if you want to include the delimiters in the literal
  1199. text of your template file, you are out of luck---it is up to you to
  1200. choose delimiters that do not conflict with what you are doing. The
  1201. delimiter strings may still appear inside of program fragments as long
  1202. as they nest properly. This means that if for some reason you
  1203. absolutely must have a program fragment that mentions one of the
  1204. delimiters, like this:
  1205. [@--
  1206. print "Oh no, a delimiter: --@]\n"
  1207. --@]
  1208. you may be able to make it work by doing this instead:
  1209. [@--
  1210. # Fake matching delimiter in a comment: [@--
  1211. print "Oh no, a delimiter: --@]\n"
  1212. --@]
  1213. It may be safer to choose delimiters that begin with a newline
  1214. character.
  1215. Because the parsing of templates is simplified by the absence of
  1216. backslash escapes, using alternative C<DELIMITERS> may speed up the
  1217. parsing process by 20-25%. This shows that my original choice of C<{>
  1218. and C<}> was very bad.
  1219. =head2 C<PREPEND> feature and using C<strict> in templates
  1220. Suppose you would like to use C<strict> in your templates to detect
  1221. undeclared variables and the like. But each code fragment is a
  1222. separate lexical scope, so you have to turn on C<strict> at the top of
  1223. each and every code fragment:
  1224. { use strict;
  1225. use vars '$foo';
  1226. $foo = 14;
  1227. ...
  1228. }
  1229. ...
  1230. { # we forgot to put `use strict' here
  1231. my $result = $boo + 12; # $boo is misspelled and should be $foo
  1232. # No error is raised on `$boo'
  1233. }
  1234. Because we didn't put C<use strict> at the top of the second fragment,
  1235. it was only active in the first fragment, and we didn't get any
  1236. C<strict> checking in the second fragment. Then we misspelled C<$foo>
  1237. and the error wasn't caught.
  1238. C<Text::Template> version 1.22 and higher has a new feature to make
  1239. this easier. You can specify that any text at all be automatically
  1240. added to the beginning of each program fragment.
  1241. When you make a call to C<fill_in>, you can specify a
  1242. PREPEND => 'some perl statements here'
  1243. option; the statements will be prepended to each program fragment for
  1244. that one call only. Suppose that the C<fill_in> call included a
  1245. PREPEND => 'use strict;'
  1246. option, and that the template looked like this:
  1247. { use vars '$foo';
  1248. $foo = 14;
  1249. ...
  1250. }
  1251. ...
  1252. { my $result = $boo + 12; # $boo is misspelled and should be $foo
  1253. ...
  1254. }
  1255. The code in the second fragment would fail, because C<$boo> has not
  1256. been declared. C<use strict> was implied, even though you did not
  1257. write it explicitly, because the C<PREPEND> option added it for you
  1258. automatically.
  1259. There are three other ways to do this. At the time you create the
  1260. template object with C<new>, you can also supply a C<PREPEND> option,
  1261. in which case the statements will be prepended each time you fill in
  1262. that template. If the C<fill_in> call has its own C<PREPEND> option,
  1263. this overrides the one specified at the time you created the
  1264. template. Finally, you can make the class method call
  1265. Text::Template->always_prepend('perl statements');
  1266. If you do this, then call calls to C<fill_in> for I<any> template will
  1267. attach the perl statements to the beginning of each program fragment,
  1268. except where overridden by C<PREPEND> options to C<new> or C<fill_in>.
  1269. An alternative to adding "use strict;" to the PREPEND option, you can
  1270. pass STRICT => 1 to fill_in when also passing the HASH option.
  1271. Suppose that the C<fill_in> call included both
  1272. HASH => {$foo => ''} and
  1273. STRICT => 1
  1274. options, and that the template looked like this:
  1275. {
  1276. $foo = 14;
  1277. ...
  1278. }
  1279. ...
  1280. { my $result = $boo + 12; # $boo is misspelled and should be $foo
  1281. ...
  1282. }
  1283. The code in the second fragment would fail, because C<$boo> has not
  1284. been declared. C<use strict> was implied, even though you did not
  1285. write it explicitly, because the C<STRICT> option added it for you
  1286. automatically. Any variable referenced in the template that is not in the
  1287. C<HASH> option will be an error.
  1288. =head2 Prepending in Derived Classes
  1289. This section is technical, and you should skip it on the first few
  1290. readings.
  1291. Normally there are three places that prepended text could come from.
  1292. It could come from the C<PREPEND> option in the C<fill_in> call, from
  1293. the C<PREPEND> option in the C<new> call that created the template
  1294. object, or from the argument of the C<always_prepend> call.
  1295. C<Text::Template> looks for these three things in order and takes the
  1296. first one that it finds.
  1297. In a subclass of C<Text::Template>, this last possibility is
  1298. ambiguous. Suppose C<S> is a subclass of C<Text::Template>. Should
  1299. Text::Template->always_prepend(...);
  1300. affect objects in class C<Derived>? The answer is that you can have it
  1301. either way.
  1302. The C<always_prepend> value for C<Text::Template> is normally stored
  1303. in a hash variable named C<%GLOBAL_PREPEND> under the key
  1304. C<Text::Template>. When C<Text::Template> looks to see what text to
  1305. prepend, it first looks in the template object itself, and if not, it
  1306. looks in C<$GLOBAL_PREPEND{I<class>}> where I<class> is the class to
  1307. which the template object belongs. If it doesn't find any value, it
  1308. looks in C<$GLOBAL_PREPEND{'Text::Template'}>. This means that
  1309. objects in class C<Derived> I<will> be affected by
  1310. Text::Template->always_prepend(...);
  1311. I<unless> there is also a call to
  1312. Derived->always_prepend(...);
  1313. So when you're designing your derived class, you can arrange to have
  1314. your objects ignore C<Text::Template::always_prepend> calls by simply
  1315. putting C<Derived-E<gt>always_prepend('')> at the top of your module.
  1316. Of course, there is also a final escape hatch: Templates support a
  1317. C<prepend_text> that is used to look up the appropriate text to be
  1318. prepended at C<fill_in> time. Your derived class can override this
  1319. method to get an arbitrary effect.
  1320. =head2 JavaScript
  1321. Jennifer D. St Clair asks:
  1322. > Most of my pages contain JavaScript and Stylesheets.
  1323. > How do I change the template identifier?
  1324. Jennifer is worried about the braces in the JavaScript being taken as
  1325. the delimiters of the Perl program fragments. Of course, disaster
  1326. will ensue when perl tries to evaluate these as if they were Perl
  1327. programs. The best choice is to find some unambiguous delimiter
  1328. strings that you can use in your template instead of curly braces, and
  1329. then use the C<DELIMITERS> option. However, if you can't do this for
  1330. some reason, there are two easy workarounds:
  1331. 1. You can put C<\> in front of C<{>, C<}>, or C<\> to remove its
  1332. special meaning. So, for example, instead of
  1333. if (br== "n3") {
  1334. // etc.
  1335. }
  1336. you can put
  1337. if (br== "n3") \{
  1338. // etc.
  1339. \}
  1340. and it'll come out of the template engine the way you want.
  1341. But here is another method that is probably better. To see how it
  1342. works, first consider what happens if you put this into a template:
  1343. { 'foo' }
  1344. Since it's in braces, it gets evaluated, and obviously, this is going
  1345. to turn into
  1346. foo
  1347. So now here's the trick: In Perl, C<q{...}> is the same as C<'...'>.
  1348. So if we wrote
  1349. {q{foo}}
  1350. it would turn into
  1351. foo
  1352. So for your JavaScript, just write
  1353. {q{if (br== "n3") {
  1354. // etc.
  1355. }}
  1356. }
  1357. and it'll come out as
  1358. if (br== "n3") {
  1359. // etc.
  1360. }
  1361. which is what you want.
  1362. head2 Shut Up!
  1363. People sometimes try to put an initialization section at the top of
  1364. their templates, like this:
  1365. { ...
  1366. $var = 17;
  1367. }
  1368. Then they complain because there is a C<17> at the top of the output
  1369. that they didn't want to have there.
  1370. Remember that a program fragment is replaced with its own return
  1371. value, and that in Perl the return value of a code block is the value
  1372. of the last expression that was evaluated, which in this case is 17.
  1373. If it didn't do that, you wouldn't be able to write C<{$recipient}>
  1374. and have the recipient filled in.
  1375. To prevent the 17 from appearing in the output is very simple:
  1376. { ...
  1377. $var = 17;
  1378. '';
  1379. }
  1380. Now the last expression evaluated yields the empty string, which is
  1381. invisible. If you don't like the way this looks, use
  1382. { ...
  1383. $var = 17;
  1384. ($SILENTLY);
  1385. }
  1386. instead. Presumably, C<$SILENTLY> has no value, so nothing will be
  1387. interpolated. This is what is known as a `trick'.
  1388. =head2 Compatibility
  1389. Every effort has been made to make this module compatible with older
  1390. versions. The only known exceptions follow:
  1391. The output format of the default C<BROKEN> subroutine has changed
  1392. twice, most recently between versions 1.31 and 1.40.
  1393. Starting in version 1.10, the C<$OUT> variable is arrogated for a
  1394. special meaning. If you had templates before version 1.10 that
  1395. happened to use a variable named C<$OUT>, you will have to change them
  1396. to use some other variable or all sorts of strangeness will result.
  1397. Between versions 0.1b and 1.00 the behavior of the \ metacharacter
  1398. changed. In 0.1b, \\ was special everywhere, and the template
  1399. processor always replaced it with a single backslash before passing
  1400. the code to Perl for evaluation. The rule now is more complicated but
  1401. probably more convenient. See the section on backslash processing,
  1402. below, for a full discussion.
  1403. =head2 Backslash Processing
  1404. In C<Text::Template> beta versions, the backslash was special whenever
  1405. it appeared before a brace or another backslash. That meant that
  1406. while C<{"\n"}> did indeed generate a newline, C<{"\\"}> did not
  1407. generate a backslash, because the code passed to Perl for evaluation
  1408. was C<"\"> which is a syntax error. If you wanted a backslash, you
  1409. would have had to write C<{"\\\\"}>.
  1410. In C<Text::Template> versions 1.00 through 1.10, there was a bug:
  1411. Backslash was special everywhere. In these versions, C<{"\n"}>
  1412. generated the letter C<n>.
  1413. The bug has been corrected in version 1.11, but I did not go back to
  1414. exactly the old rule, because I did not like the idea of having to
  1415. write C<{"\\\\"}> to get one backslash. The rule is now more
  1416. complicated to remember, but probably easier to use. The rule is now:
  1417. Backslashes are always passed to Perl unchanged I<unless> they occur
  1418. as part of a sequence like C<\\\\\\{> or C<\\\\\\}>. In these
  1419. contexts, they are special; C<\\> is replaced with C<\>, and C<\{> and
  1420. C<\}> signal a literal brace.
  1421. Examples:
  1422. \{ foo \}
  1423. is I<not> evaluated, because the C<\> before the braces signals that
  1424. they should be taken literally. The result in the output looks like this:
  1425. { foo }
  1426. This is a syntax error:
  1427. { "foo}" }
  1428. because C<Text::Template> thinks that the code ends at the first C<}>,
  1429. and then gets upset when it sees the second one. To make this work
  1430. correctly, use
  1431. { "foo\}" }
  1432. This passes C<"foo}"> to Perl for evaluation. Note there's no C<\> in
  1433. the evaluated code. If you really want a C<\> in the evaluated code,
  1434. use
  1435. { "foo\\\}" }
  1436. This passes C<"foo\}"> to Perl for evaluation.
  1437. Starting with C<Text::Template> version 1.20, backslash processing is
  1438. disabled if you use the C<DELIMITERS> option to specify alternative
  1439. delimiter strings.
  1440. =head2 A short note about C<$Text::Template::ERROR>
  1441. In the past some people have fretted about `violating the package
  1442. boundary' by examining a variable inside the C<Text::Template>
  1443. package. Don't feel this way. C<$Text::Template::ERROR> is part of
  1444. the published, official interface to this package. It is perfectly OK
  1445. to inspect this variable. The interface is not going to change.
  1446. If it really, really bothers you, you can import a function called
  1447. C<TTerror> that returns the current value of the C<$ERROR> variable.
  1448. So you can say:
  1449. use Text::Template 'TTerror';
  1450. my $template = Text::Template->new(SOURCE => $filename);
  1451. unless ($template) {
  1452. my $err = TTerror;
  1453. die "Couldn't make template: $err; aborting";
  1454. }
  1455. I don't see what benefit this has over just doing this:
  1456. use Text::Template;
  1457. my $template = Text::Template->new(SOURCE => $filename)
  1458. or die "Couldn't make template: $Text::Template::ERROR; aborting";
  1459. But if it makes you happy to do it that way, go ahead.
  1460. =head2 Sticky Widgets in Template Files
  1461. The C<CGI> module provides functions for `sticky widgets', which are
  1462. form input controls that retain their values from one page to the
  1463. next. Sometimes people want to know how to include these widgets
  1464. into their template output.
  1465. It's totally straightforward. Just call the C<CGI> functions from
  1466. inside the template:
  1467. { $q->checkbox_group(NAME => 'toppings',
  1468. LINEBREAK => true,
  1469. COLUMNS => 3,
  1470. VALUES => \@toppings,
  1471. );
  1472. }
  1473. =head2 Automatic preprocessing of program fragments
  1474. It may be useful to preprocess the program fragments before they are
  1475. evaluated. See C<Text::Template::Preprocess> for more details.
  1476. =head2 Automatic postprocessing of template hunks
  1477. It may be useful to process hunks of output before they are appended to
  1478. the result text. For this, subclass and replace the C<append_text_to_result>
  1479. method. It is passed a list of pairs with these entries:
  1480. handle - a filehandle to which to print the desired output
  1481. out - a ref to a string to which to append, to use if handle is not given
  1482. text - the text that will be appended
  1483. type - where the text came from: TEXT for literal text, PROG for code
  1484. =head1 HISTORY
  1485. Originally written by Mark Jason Dominus, Plover Systems (versions 0.01 - 1.46)
  1486. Maintainership transferred to Michael Schout E<lt>mschout@cpan.orgE<gt> in version
  1487. 1.47
  1488. =head1 THANKS
  1489. Many thanks to the following people for offering support,
  1490. encouragement, advice, bug reports, and all the other good stuff.
  1491. =over 4
  1492. =item *
  1493. Andrew G Wood
  1494. =item *
  1495. Andy Wardley
  1496. =item *
  1497. António Aragão
  1498. =item *
  1499. Archie Warnock
  1500. =item *
  1501. Bek Oberin
  1502. =item *
  1503. Bob Dougherty
  1504. =item *
  1505. Brian C. Shensky
  1506. =item *
  1507. Chris Nandor
  1508. =item *
  1509. Chris Wesley
  1510. =item *
  1511. Chris.Brezil
  1512. =item *
  1513. Daini Xie
  1514. =item *
  1515. Dan Franklin
  1516. =item *
  1517. Daniel LaLiberte
  1518. =item *
  1519. David H. Adler
  1520. =item *
  1521. David Marshall
  1522. =item *
  1523. Dennis Taylor
  1524. =item *
  1525. Donald L. Greer Jr.
  1526. =item *
  1527. Dr. Frank Bucolo
  1528. =item *
  1529. Fred Steinberg
  1530. =item *
  1531. Gene Damon
  1532. =item *
  1533. Hans Persson
  1534. =item *
  1535. Hans Stoop
  1536. =item *
  1537. Itamar Almeida de Carvalho
  1538. =item *
  1539. James H. Thompson
  1540. =item *
  1541. James Mastros
  1542. =item *
  1543. Jarko Hietaniemi
  1544. =item *
  1545. Jason Moore
  1546. =item *
  1547. Jennifer D. St Clair
  1548. =item *
  1549. Joel Appelbaum
  1550. =item *
  1551. Joel Meulenberg
  1552. =item *
  1553. Jonathan Roy
  1554. =item *
  1555. Joseph Cheek
  1556. =item *
  1557. Juan E. Camacho
  1558. =item *
  1559. Kevin Atteson
  1560. =item *
  1561. Kevin Madsen
  1562. =item *
  1563. Klaus Arnhold
  1564. =item *
  1565. Larry Virden
  1566. =item *
  1567. Lieven Tomme
  1568. =item *
  1569. Lorenzo Valdettaro
  1570. =item *
  1571. Marek Grac
  1572. =item *
  1573. Matt Womer
  1574. =item *
  1575. Matt X. Hunter
  1576. =item *
  1577. Michael G Schwern
  1578. =item *
  1579. Michael J. Suzio
  1580. =item *
  1581. Michaely Yeung
  1582. =item *
  1583. Michelangelo Grigni
  1584. =item *
  1585. Mike Brodhead
  1586. =item *
  1587. Niklas Skoglund
  1588. =item *
  1589. Randal L. Schwartz
  1590. =item *
  1591. Reuven M. Lerner
  1592. =item *
  1593. Robert M. Ioffe
  1594. =item *
  1595. Ron Pero
  1596. =item *
  1597. San Deng
  1598. =item *
  1599. Sean Roehnelt
  1600. =item *
  1601. Sergey Myasnikov
  1602. =item *
  1603. Shabbir J. Safdar
  1604. =item *
  1605. Shad Todd
  1606. =item *
  1607. Steve Palincsar
  1608. =item *
  1609. Tim Bunce
  1610. =item *
  1611. Todd A. Green
  1612. =item *
  1613. Tom Brown
  1614. =item *
  1615. Tom Henry
  1616. =item *
  1617. Tom Snee
  1618. =item *
  1619. Trip Lilley
  1620. =item *
  1621. Uwe Schneider
  1622. =item *
  1623. Val Luck
  1624. =item *
  1625. Yannis Livassof
  1626. =item *
  1627. Yonat Sharon
  1628. =item *
  1629. Zac Hansen
  1630. =item *
  1631. gary at dls.net
  1632. =back
  1633. Special thanks to:
  1634. =over 2
  1635. =item Jonathan Roy
  1636. for telling me how to do the C<Safe> support (I spent two years
  1637. worrying about it, and then Jonathan pointed out that it was trivial.)
  1638. =item Ranjit Bhatnagar
  1639. for demanding less verbose fragments like they have in ASP, for
  1640. helping me figure out the Right Thing, and, especially, for talking me
  1641. out of adding any new syntax. These discussions resulted in the
  1642. C<$OUT> feature.
  1643. =back
  1644. =head2 Bugs and Caveats
  1645. C<my> variables in C<fill_in> are still susceptible to being clobbered
  1646. by template evaluation. They all begin with C<fi_>, so avoid those
  1647. names in your templates.
  1648. The line number information will be wrong if the template's lines are
  1649. not terminated by C<"\n">. You should let me know if this is a
  1650. problem. If you do, I will fix it.
  1651. The C<$OUT> variable has a special meaning in templates, so you cannot
  1652. use it as if it were a regular variable.
  1653. There are not quite enough tests in the test suite.
  1654. =head1 SOURCE
  1655. The development version is on github at L<https://https://github.com/mschout/perl-text-template>
  1656. and may be cloned from L<git://https://github.com/mschout/perl-text-template.git>
  1657. =head1 BUGS
  1658. Please report any bugs or feature requests on the bugtracker website
  1659. L<https://github.com/mschout/perl-text-template/issues>
  1660. When submitting a bug or request, please include a test-file or a
  1661. patch to an existing test-file that illustrates the bug or desired
  1662. feature.
  1663. =head1 AUTHOR
  1664. Michael Schout <mschout@cpan.org>
  1665. =head1 COPYRIGHT AND LICENSE
  1666. This software is copyright (c) 2013 by Mark Jason Dominus <mjd@cpan.org>.
  1667. This is free software; you can redistribute it and/or modify it under
  1668. the same terms as the Perl 5 programming language system itself.
  1669. =cut