提交 05c9c7b0 编写于 作者: R Richard Levitte

Update the bundled external perl module Text-Template to version 1.56

Fixes #9287
Reviewed-by: NMatt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/9828)
上级 7f4a2dff
......@@ -10,5 +10,5 @@ Here, we simply list those modules and where we got them from.
Downloaded and bundled Perl modules
-----------------------------------
Text::Template 1.46 was downloaded from
http://search.cpan.org/CPAN/authors/id/M/MJ/MJD/Text-Template-1.46.tar.gz
Text::Template 1.56 was downloaded from
https://cpan.metacpan.org/authors/id/M/MS/MSCHOUT/Text-Template-1.56.tar.gz
Text-Template-1.46/lib
Text-Template-1.56/lib
The "Artistic License"
Preamble
The intent of this document is to state the conditions under which a
Package may be copied, such that the Copyright Holder maintains some
semblance of artistic control over the development of the package,
while giving the users of the package the right to use and distribute
the Package in a more-or-less customary fashion, plus the right to make
reasonable modifications.
Definitions:
"Package" refers to the collection of files distributed by the
Copyright Holder, and derivatives of that collection of files
created through textual modification.
"Standard Version" refers to such a Package if it has not been
modified, or has been modified in accordance with the wishes
of the Copyright Holder as specified below.
"Copyright Holder" is whoever is named in the copyright or
copyrights for the package.
"You" is you, if you're thinking about copying or distributing
this Package.
"Reasonable copying fee" is whatever you can justify on the
basis of media cost, duplication charges, time of people involved,
and so on. (You will not be required to justify it to the
Copyright Holder, but only to the computing community at large
as a market that must bear the fee.)
"Freely Available" means that no fee is charged for the item
itself, though there may be fees involved in handling the item.
It also means that recipients of the item may redistribute it
under the same conditions they received it.
1. You may make and give away verbatim copies of the source form of the
Standard Version of this Package without restriction, provided that you
duplicate all of the original copyright notices and associated disclaimers.
2. You may apply bug fixes, portability fixes and other modifications
derived from the Public Domain or from the Copyright Holder. A Package
modified in such a way shall still be considered the Standard Version.
3. You may otherwise modify your copy of this Package in any way, provided
that you insert a prominent notice in each changed file stating how and
when you changed that file, and provided that you do at least ONE of the
following:
a) place your modifications in the Public Domain or otherwise make them
Freely Available, such as by posting said modifications to Usenet or
an equivalent medium, or placing the modifications on a major archive
site such as uunet.uu.net, or by allowing the Copyright Holder to include
your modifications in the Standard Version of the Package.
b) use the modified Package only within your corporation or organization.
c) rename any non-standard executables so the names do not conflict
with standard executables, which must also be provided, and provide
a separate manual page for each non-standard executable that clearly
documents how it differs from the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
4. You may distribute the programs of this Package in object code or
executable form, provided that you do at least ONE of the following:
a) distribute a Standard Version of the executables and library files,
together with instructions (in the manual page or equivalent) on where
to get the Standard Version.
b) accompany the distribution with the machine-readable source of
the Package with your modifications.
c) give non-standard executables non-standard names, and clearly
document the differences in manual pages (or equivalent), together
with instructions on where to get the Standard Version.
d) make other distribution arrangements with the Copyright Holder.
5. You may charge a reasonable copying fee for any distribution of this
Package. You may charge any fee you choose for support of this
Package. You may not charge a fee for this Package itself. However,
you may distribute this Package in aggregate with other (possibly
commercial) programs as part of a larger (possibly commercial) software
distribution provided that you do not advertise this Package as a
product of your own. You may embed this Package's interpreter within
an executable of yours (by linking); this shall be construed as a mere
form of aggregation, provided that the complete Standard Version of the
interpreter is so embedded.
6. The scripts and library files supplied as input to or produced as
output from the programs of this Package do not automatically fall
under the copyright of this Package, but belong to whoever generated
them, and may be sold commercially, and may be aggregated with this
Package. If such scripts or library files are aggregated with this
Package via the so-called "undump" or "unexec" methods of producing a
binary executable image, then distribution of such an image shall
neither be construed as a distribution of this Package nor shall it
fall under the restrictions of Paragraphs 3 and 4, provided that you do
not represent such an executable image as a Standard Version of this
Package.
7. C subroutines (or comparably compiled subroutines in other
languages) supplied by you and linked into this Package in order to
emulate subroutines and variables of the language defined by this
Package shall not be considered part of this Package, but are the
equivalent of input as in Paragraph 6, provided these subroutines do
not change the language in any way that would cause it to fail the
regression tests for the language.
8. Aggregation of this Package with a commercial distribution is always
permitted provided that the use of this Package is embedded; that is,
when no overt attempt is made to make this Package's interfaces visible
to the end user of the commercial distribution. Such use shall not be
construed as a distribution of this Package.
9. The name of the Copyright Holder may not be used to endorse or promote
products derived from this software without specific prior written permission.
10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
The End
MANIFEST
COPYING
Artistic
Makefile.PL
INSTALL
README
lib/Text/Template.pm
lib/Text/Template/Preprocess.pm
t/00-version.t
t/01-basic.t
t/02-hash.t
t/03-out.t
t/04-safe.t
t/05-safe2.t
t/06-ofh.t
t/07-safe3.t
t/08-exported.t
t/09-error.t
t/10-delimiters.t
t/11-prepend.t
t/12-preprocess.t
t/13-taint.t
t/14-broken.t
META.yml Module meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
{
"abstract" : "unknown",
"author" : [
"unknown"
],
"dynamic_config" : 1,
"generated_by" : "ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.120630",
"license" : [
"unknown"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : "2"
},
"name" : "Text-Template",
"no_index" : {
"directory" : [
"t",
"inc"
]
},
"prereqs" : {
"build" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0"
}
},
"runtime" : {
"requires" : {}
}
},
"release_status" : "stable",
"version" : "1.46"
}
---
abstract: unknown
author:
- unknown
build_requires:
ExtUtils::MakeMaker: 0
configure_requires:
ExtUtils::MakeMaker: 0
dynamic_config: 1
generated_by: 'ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.120630'
license: unknown
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: 1.4
name: Text-Template
no_index:
directory:
- t
- inc
requires: {}
version: 1.46
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'Text::Template',
VERSION_FROM => 'lib/Text/Template.pm',
# 'linkext' => {LINKTYPE => ''},
'dist' => {COMPRESS => 'gzip', SUFFIX => 'gz'},
);
Text::Template v1.46
This is a library for generating form letters, building HTML pages, or
filling in templates generally. A `template' is a piece of text that
has little Perl programs embedded in it here and there. When you
`fill in' a template, you evaluate the little programs and replace
them with their values.
Here's an example of a template:
Dear {$title} {$lastname},
It has come to our attention that you are delinquent in your
{$monthname[$last_paid_month]} payment. Please remit
${sprintf("%.2f", $amount)} immediately, or your patellae may
be needlessly endangered.
Love,
Mark "{nickname(rand 20)}" Dominus
The result of filling in this template is a string, which might look
something like this:
Dear Mr. Gates,
It has come to our attention that you are delinquent in your
February payment. Please remit
$392.12 immediately, or your patellae may
be needlessly endangered.
Love,
Mark "Vizopteryx" Dominus
You can store a template in a file outside your program. People can
modify the template without modifying the program. You can separate
the formatting details from the main code, and put the formatting
parts of the program into the template. That prevents code bloat and
encourages functional separation.
You can fill in the template in a `Safe' compartment. This means that
if you don't trust the person who wrote the code in the template, you
won't have to worry that they are tampering with your program when you
execute it.
----------------------------------------------------------------
Text::Template was originally released some time in late 1995 or early
1996. After three years of study and investigation, I rewrote it from
scratch in January 1999. The new version, 1.0, was much faster,
delivered better functionality and was almost 100% backward-compatible
with the previous beta versions.
I have added a number of useful features and conveniences since the
1.0 release, while still retaining backward compatibility. With one
merely cosmetic change, the current version of Text::Template passes
the test suite that the old beta versions passed.
Questions or comments should be addressed to
mjd-perl-template+@plover.com. This address goes directly to me, and
not to anyone else; it is not a mailing list address.
To receive occasional announcements of new versions of T::T, send an
empty note to mjd-perl-template-request@plover.com. This mailing list
is not for discussion; it is for announcements only. Therefore, there
is no address for sending messages to the list.
You can get the most recent version of Text::Template, news, comments,
and other collateral information from
<URL:http://www.plover.com/~mjd/perl/Template/>.
----------------------------------------------------------------
What's new in v1.46 since v1.44:
Thanks to Rik Signes, there is a new
Text::Template->append_text_to_output method, which
Text::Template always uses whenever it wants to emit output.
You can subclass this to get control over the output, for
example for postprocessing.
A spurious warning is no longer emitted when the TYPE
parameter to ->new is omitted.
----------------------------------------------------------------
What's new in v1.44 since v1.43:
This is a maintentance release. There are no feature changes.
_scrubpkg, which was responsible for eptying out temporary
packages after the module had done with them, wasn't always
working; the result was memory-leaks in long-running
applications. This should be fixed now, and there is a test
in the test suite for it.
Minor changes to the test suite to prevent spurious errors.
Minor documentation changes.
----------------------------------------------------------------
What's new in v1.43 since v1.42:
The ->new method now fails immediately and sets
$Text::Template::ERROR if the file that is named by a filename
argument does not exist or cannot be opened for some other
reason. Formerly, the constructor would succeed and the
->fill_in call would fail.
----------------------------------------------------------------
What's new in v1.42 since v1.41:
This is a maintentance release. There are no feature changes.
Fixed a bug relating to use of UNTAINT under perl 5.005_03 and
possibly other versions.
Taint-related tests are now more comprehensive.
----------------------------------------------------------------
What's new in v1.41 since v1.40:
This is a maintentance release. There are no feature changes.
Tests now work correctly on Windows systems and possibly on
other non-unix systems.
----------------------------------------------------------------
What's new in v1.40 since v1.31:
New UNTAINT option tells the module that it is safe to 'eval'
code even though it has come from a file or filehandle.
Code added to prevent memory leaks when filling many
templates. Thanks to Itamar Almeida de Carvalho.
Bug fix: $OUT was not correctly initialized when used in
conjunction with SAFE.
You may now use a glob ref when passing a filehandle to the
->new funcion. Formerly, a glob was reuqired.
New subclass: Text::Template::Preprocess. Just like
Text::Template, but you may supply a PREPROCESS option in the
constructor or the fill_in call; this is a function which
receives each code fragment prior to evaluation, and which may
modify and return the fragment; the modified fragment is what
is evaluated.
Error messages passed to BROKEN subroutines will now report
the correct line number of the template at which the error
occurred:
Illegal division by zero at template line 37.
If the template comes from a file, the filename will be
reported as well:
Illegal division by zero at catalog.tmpl line 37.
INCOMPATIBLE CHANGE:
The format of the default error message has changed. It used
to look like:
Program fragment at line 30 delivered error ``Illegal division by zero''
It now looks like:
Program fragment delivered error ``Illegal division by zero at catalog.tmpl line 37''
Note that the default message used to report the line number
at which the program fragment began; it now reports the line
number at which the error actually occurred.
----------------------------------------------------------------
What's new in v1.31 since v1.23:
Just bug fixes---fill_in_string was failing. Thanks to
Donald L. Greer Jr. for the test case.
----------------------------------------------------------------
What's new in v1.23 since v1.22:
Small bug fix: DELIMITER and other arguments were being
ignored in calls to fill_in_file and fill_this_in. (Thanks to
Jonathan Roy for reporting this.)
----------------------------------------------------------------
What's new in v1.22 since v1.20:
You can now specify that certain Perl statements be prepended
to the beginning of every program fragment in a template,
either per template, or for all templates, or for the duration
of only one call to fill_in. This is useful, for example, if
you want to enable `strict' checks in your templates but you
don't want to manually add `use strict' to the front of every
program fragment everywhere.
----------------------------------------------------------------
What's new in v1.20 since v1.12:
You can now specify that the program fragment delimiters are
strings other than { and }. This has three interesting
effects: First, it changes the delimiter strings. Second, it
disables the special meaning of \, so you have to be really,
really sure that the delimiters will not appear in your
templates. And third, because of the simplifications
introduced by the elimination of \ processing, template
parsing is 20-25% faster.
See the manual section on `Alternative Delimiters'.
Fixed bug having to do with undefined values in HASH options.
In particular, Text::Template no longer generates a warning if
you try to give a variable an undefined value.
----------------------------------------------------------------
What's new in v1.12 since v1.11:
I forgot to say that Text::Template ISA Exporter, so the
exported functions never got exported. Duhhh!
Template TYPEs are now case-insensitive. The `new' method now
diagnoses attempts to use an invalid TYPE.
More tests for these things.
----------------------------------------------------------------
What's new in v1.11 since v1.10:
Fixed a bug in the way backslashes were processed. The 1.10
behavior was incompatible with the beta versions and was also
inconvenient. (`\n' in templates was replaced with `n' before
it was given to Perl for evaluation.) The new behavior is
also incompatible with the beta versions, but it is only a
little bit incompatible, and it is probbaly better.
Documentation for the new behavior, and tests for the bug.
----------------------------------------------------------------
What's new in v1.10 since v1.03:
New OUTPUT option delivers template results directly to a
filehandle instead of making them into a string. Saves space
and time.
PACKAGE and HASH now work intelligently with SAFE.
Fragments may now output data directly to the template, rather
than having to arrange to return it as a return value at the
end. This means that where you used to have to write this:
{ my $blist = '';
foreach $i (@items) {
$blist .= qq{ * $i\n};
}
$blist;
}
You can now write this instead, because $OUT is special.
{ foreach $i (@items) {
$OUT.= " * $i\n";
}
}
(`A spoonful of sugar makes the medicine go down.')
Fixed some small bugs. Worked around a bug in Perl that does
the wrong thing with $x = <Y> when $x contains a glob.
More documentation. Errors fixed.
Lots more tests.
----------------------------------------------------------------
What's new in v1.03 since v1.0:
Code added to support HASH option to fill_in.
(Incl. `_gensym' function.)
Documentation for HASH.
New test file for HASH.
Note about failure of lexical variables to propagate into
templates. Why does this surprise people?
Bug fix: program fragments are evaluated in an environment with
`no strict' by default. Otherwise, you get a lot of `Global
symbol "$v" requires explicit package name' failures. Why didn't
the test program pick this up? Because the only variable the test
program ever used was `$a', which is exempt. Duhhhhh.
Fixed the test program.
Various minor documentation fixes.
----------------------------------------------------------------
Improvements of 1.0 over the old 0.1beta:
New features:
At least twice as fast
Better support for filling out the same template more than once
Now supports evaluation of program fragments in Safe
compartments. (Thanks, Jonathan!)
Better argument syntax
More convenience functions
The parser is much better and simpler.
Once a template is parsed, the parsed version is stored so that
it needn't be parsed again.
BROKEN function behavior is rationalized. You can now pass an
arbitrary argument to your BROKEN function, or return a value
from it to the main program.
Documentation overhauled.
#!perl
use Text::Template;
print "1..1\n";
if ($Text::Template::VERSION == 1.46) {
print "ok 1\n";
} else {
print "not ok 1\n";
}
#!perl
#
# Tests of basic, essential functionality
#
use Text::Template;
$X::v = $Y::v = 0; # Suppress `var used only once'
print "1..31\n";
$n=1;
$template_1 = <<EOM;
We will put value of \$v (which is "abc") here -> {\$v}
We will evaluate 1+1 here -> {1 + 1}
EOM
# (1) Construct temporary template file for testing
# file operations
$TEMPFILE = "tt$$";
open(TMP, "> $TEMPFILE") or print "not ok $n\n" && &abort("Couldn\'t write tempfile $TEMPFILE: $!");
print TMP $template_1;
close TMP;
print "ok $n\n"; $n++;
# (2) Build template from file
$template = new Text::Template ('type' => 'FILE', 'source' => $TEMPFILE);
if (defined($template)) {
print "ok $n\n";
} else {
print "not ok $n $Text::Template::ERROR\n";
}
$n++;
# (3) Fill in template from file
$X::v = "abc";
$resultX = <<EOM;
We will put value of \$v (which is "abc") here -> abc
We will evaluate 1+1 here -> 2
EOM
$Y::v = "ABC";
$resultY = <<EOM;
We will put value of \$v (which is "abc") here -> ABC
We will evaluate 1+1 here -> 2
EOM
$text = $template->fill_in('package' => X);
if ($text eq $resultX) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
# (4) Fill in same template again
$text = $template->fill_in('package' => Y);
if ($text eq $resultY) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
# (5) Simple test of `fill_this_in'
$text = Text::Template->fill_this_in( $template_1, 'package' => X);
if ($text eq $resultX) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
# (6) test creation of template from filehandle
if (open (TMPL, "< $TEMPFILE")) {
$template = new Text::Template ('type' => 'FILEHANDLE',
'source' => *TMPL);
if (defined($template)) {
print "ok $n\n";
} else {
print "not ok $n $Text::Template::ERROR\n";
}
$n++;
# (7) test filling in of template from filehandle
$text = $template->fill_in('package' => X);
if ($text eq $resultX) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
# (8) test second fill_in on same template object
$text = $template->fill_in('package' => Y);
if ($text eq $resultY) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
close TMPL;
} else {
print "not ok $n\n"; $n++;
print "not ok $n\n"; $n++;
print "not ok $n\n"; $n++;
}
# (9) test creation of template from array
$template = new Text::Template
('type' => 'ARRAY',
'source' => [
'We will put value of $v (which is "abc") here -> {$v}',
"\n",
'We will evaluate 1+1 here -> {1+1}',
"\n",
]);
if (defined($template)) {
print "ok $n\n";
} else {
print "not ok $n $Text::Template::ERROR\n";
}
$n++;
# (10) test filling in of template from array
$text = $template->fill_in('package' => X);
if ($text eq $resultX) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
# (11) test second fill_in on same array template object
$text = $template->fill_in('package' => Y);
if ($text eq $resultY) {
print "ok $n\n";
} else {
print "not ok $n\n";
print STDERR "$resultX\n---\n$text";
unless (!defined($text)) { print STDERR "ERROR: $Text::Template::ERROR\n"};
}
$n++;
# (12) Make sure \ is working properly
# Test added for version 1.11
my $tmpl = Text::Template->new(TYPE => 'STRING',
SOURCE => 'B{"\\}"}C{"\\{"}D',
);
# This should fail if the \ are not interpreted properly.
my $text = $tmpl->fill_in();
print +($text eq "B}C{D" ? '' : 'not '), "ok $n\n";
$n++;
# (13) Make sure \ is working properly
# Test added for version 1.11
$tmpl = Text::Template->new(TYPE => 'STRING',
SOURCE => qq{A{"\t"}B},
);
# Symptom of old problem: ALL \ were special in templates, so
# The lexer would return (A, PROGTEXT("t"), B), and the
# result text would be AtB instead of A(tab)B.
$text = $tmpl->fill_in();
print +($text eq "A\tB" ? '' : 'not '), "ok $n\n";
$n++;
# (14-27) Make sure \ is working properly
# Test added for version 1.11
# This is a sort of general test.
my @tests = ('{""}' => '', # (14)
'{"}"}' => undef, # (15)
'{"\\}"}' => '}', # One backslash
'{"\\\\}"}' => undef, # Two backslashes
'{"\\\\\\}"}' => '}', # Three backslashes
'{"\\\\\\\\}"}' => undef, # Four backslashes
'{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20)
'{"x20"}' => 'x20',
'{"\\x20"}' => ' ', # One backslash
'{"\\\\x20"}' => '\\x20', # Two backslashes
'{"\\\\\\x20"}' => '\\ ', # Three backslashes
'{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25)
'{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
'{"\\x20\\}"}' => ' }', # (27)
);
my $i;
for ($i=0; $i<@tests; $i+=2) {
my $tmpl = Text::Template->new(TYPE => 'STRING',
SOURCE => $tests[$i],
);
my $text = $tmpl->fill_in;
my $result = $tests[$i+1];
my $ok = (! defined $text && ! defined $result
|| $text eq $result);
unless ($ok) {
print STDERR "($n) expected .$result., got .$text.\n";
}
print +($ok ? '' : 'not '), "ok $n\n";
$n++;
}
# (28-30) I discovered that you can't pass a glob ref as your filehandle.
# MJD 20010827
# (28) test creation of template from filehandle
if (open (TMPL, "< $TEMPFILE")) {
$template = new Text::Template ('type' => 'FILEHANDLE',
'source' => \*TMPL);
if (defined($template)) {
print "ok $n\n";
} else {
print "not ok $n $Text::Template::ERROR\n";
}
$n++;
# (29) test filling in of template from filehandle
$text = $template->fill_in('package' => X);
if ($text eq $resultX) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
# (30) test second fill_in on same template object
$text = $template->fill_in('package' => Y);
if ($text eq $resultY) {
print "ok $n\n";
} else {
print "not ok $n\n";
}
$n++;
close TMPL;
} else {
print "not ok $n\n"; $n++;
print "not ok $n\n"; $n++;
print "not ok $n\n"; $n++;
}
# (31) Test _scrubpkg for leakiness
$Text::Template::GEN0::test = 1;
Text::Template::_scrubpkg('Text::Template::GEN0');
if ($Text::Template::GEN0::test) {
print "not ok $n\n";
} else {
print "ok $n\n";
}
$n++;
END {unlink $TEMPFILE;}
exit;
sub abort {
unlink $TEMPFILE;
die $_[0];
}
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use Text::Template;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..12\n";
$n=1;
$template = 'We will put value of $v (which is "good") here -> {$v}';
$v = 'oops (main)';
$Q::v = 'oops (Q)';
$vars = { 'v' => \'good' };
# (1) Build template from string
$template = new Text::Template ('type' => 'STRING', 'source' => $template);
print +($template ? '' : 'not '), "ok $n\n";
$n++;
# (2) Fill in template in anonymous package
$result2 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
print +($text eq $result2 ? '' : 'not '), "ok $n\n";
$n++;
# (3) Did we clobber the main variable?
print +($v eq 'oops (main)' ? '' : 'not '), "ok $n\n";
$n++;
# (4) Fill in same template again
$result4 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
print +($text eq $result4 ? '' : 'not '), "ok $n\n";
$n++;
# (5) Now with a package
$result5 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
print +($text eq $result5 ? '' : 'not '), "ok $n\n";
$n++;
# (6) We expect to have clobbered the Q variable.
print +($Q::v eq 'good' ? '' : 'not '), "ok $n\n";
$n++;
# (7) Now let's try it without a package
$result7 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
print +($text eq $result7 ? '' : 'not '), "ok $n\n";
$n++;
# (8-11) Now what does it do when we pass a hash with undefined values?
# Roy says it does something bad. (Added for 1.20.)
my $WARNINGS = 0;
{
local $SIG{__WARN__} = sub {$WARNINGS++};
local $^W = 1; # Make sure this is on for this test
$template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}';
$result8 = 'We will put value of $v (which is "good") here -> good';
my $template =
new Text::Template ('type' => 'STRING', 'source' => $template8);
my $text = $template->fill_in(HASH => {'v' => undef});
# (8) Did we generate a warning?
print +($WARNINGS == 0 ? '' : 'not '), "ok $n\n";
$n++;
# (9) Was the output correct?
print +($text eq $result8 ? '' : 'not '), "ok $n\n";
$n++;
# (10-11) Let's try that again, with a twist this time
$WARNINGS = 0;
$text = $template->fill_in(HASH => [{'v' => 17}, {'v' => undef}]);
# (10) Did we generate a warning?
print +($WARNINGS == 0 ? '' : 'not '), "ok $n\n";
$n++;
# (11) Was the output correct?
if ($] < 5.005) {
print "ok $n # skipped -- not supported before 5.005\n";
} else {
print +($text eq $result8 ? '' : 'not '), "ok $n\n";
}
$n++;
}
# (12) Now we'll test the multiple-hash option (Added for 1.20.)
$text = Text::Template::fill_in_string(q{$v: {$v}. @v: [{"@v"}].},
HASH => [{'v' => 17},
{'v' => ['a', 'b', 'c']},
{'v' => \23},
]);
$result = q{$v: 23. @v: [a b c].};
print +($text eq $result ? '' : 'not '), "ok $n\n";
$n++;
exit;
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use Text::Template;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..2\n";
$n=1;
$template = new Text::Template TYPE => STRING, SOURCE => q{My process ID is {$$}};
$of = "t$$";
END { unlink $of }
open O, "> $of" or die;
$text = $template->fill_in(OUTPUT => \*O);
# (1) No $text should have been constructed. Return value should be true.
print +($text eq '1' ? '' : 'not '), "ok $n\n";
$n++;
close O or die;
open I, "< $of" or die;
{ local $/; $t = <I> }
close I;
# (2) The text should have been printed to the file
print +($t eq "My process ID is $$" ? '' : 'not '), "ok $n\n";
$n++;
exit;
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use Text::Template 'fill_in_file', 'fill_in_string';
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..6\n";
$n=1;
$Q::n = $Q::n = 119;
# (1) Test fill_in_string
$out = fill_in_string('The value of $n is {$n}.', PACKAGE => 'Q' );
print +($out eq 'The value of $n is 119.' ? '' : 'not '), "ok $n\n";
$n++;
# (2) Test fill_in_file
$TEMPFILE = "tt$$";
open F, "> $TEMPFILE" or die "Couldn't open test file: $!; aborting";
print F 'The value of $n is {$n}.', "\n";
close F or die "Couldn't write test file: $!; aborting";
$R::n = $R::n = 8128;
$out = fill_in_file($TEMPFILE, PACKAGE => 'R');
print +($out eq "The value of \$n is 8128.\n" ? '' : 'not '), "ok $n\n";
$n++;
# (3) Jonathan Roy reported this bug:
open F, "> $TEMPFILE" or die "Couldn't open test file: $!; aborting";
print F "With a message here? [% \$var %]\n";
close F or die "Couldn't close test file: $!; aborting";
$out = fill_in_file($TEMPFILE, DELIMITERS => ['[%', '%]'],
HASH => { "var" => \"It is good!" });
print +($out eq "With a message here? It is good!\n" ? '' : 'not '), "ok $n\n";
$n++;
# (4) It probably occurs in fill_this_in also:
$out =
Text::Template->fill_this_in("With a message here? [% \$var %]\n",
DELIMITERS => ['[%', '%]'],
HASH => { "var" => \"It is good!" });
print +($out eq "With a message here? It is good!\n" ? '' : 'not '), "ok $n\n";
$n++;
# (5) This test failed in 1.25. It was supplied by Donald L. Greer Jr.
# Note that it's different from (1) in that there's no explicit
# package=> argument.
use vars qw($string $foo $r);
$string='Hello {$foo}';
$foo="Don";
$r = fill_in_string($string);
print (($r eq 'Hello Don' ? '' : 'not '), 'ok ', $n++, "\n");
# (6) This test failed in 1.25. It's a variation on (5)
package Q2;
use Text::Template 'fill_in_string';
use vars qw($string $foo $r);
$string='Hello {$foo}';
$foo="Don";
$r = fill_in_string($string);
print (($r eq 'Hello Don' ? '' : 'not '), 'ok ', $main::n++, "\n");
package main;
END { $TEMPFILE && unlink $TEMPFILE }
exit;
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use Text::Template;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..5\n";
$n = 1;
# (1-2) Missing source
eval {
Text::Template->new();
};
unless ($@ =~ /^\QUsage: Text::Template::new(TYPE => ..., SOURCE => ...)/) {
print STDERR $@;
print "not ";
}
print "ok $n\n";
$n++;
eval {
Text::Template->new(TYPE => 'FILE');
};
if ($@ =~ /^\QUsage: Text::Template::new(TYPE => ..., SOURCE => ...)/) {
print "ok $n\n";
} else {
print STDERR $@;
print "not ok $n\n";
}
$n++;
# (3) Invalid type
eval {
Text::Template->new(TYPE => 'wlunch', SOURCE => 'fish food');
};
if ($@ =~ /^\QIllegal value `WLUNCH' for TYPE parameter/) {
print "ok $n\n";
} else {
print STDERR $@;
print "not ok $n\n";
}
$n++;
# (4-5) File does not exist
my $o = Text::Template->new(TYPE => 'file',
SOURCE => 'this file does not exist');
print $o ? "not ok $n\n" : "ok $n\n";
$n++;
print defined($Text::Template::ERROR)
&& $Text::Template::ERROR =~ /^Couldn't open file/
? "ok $n\n" : "not ok $n\n";
$n++;
exit;
#!perl
#
# Tests for user-specified delimiter functions
# These tests first appeared in version 1.20.
use Text::Template;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..18\n";
$n = 1;
# (1) Try a simple delimiter: <<..>>
# First with the delimiters specified at object creation time
$V = $V = 119;
$template = q{The value of $V is <<$V>>.};
$result = q{The value of $V is 119.};
$template1 = Text::Template->new(TYPE => STRING,
SOURCE => $template,
DELIMITERS => ['<<', '>>']
)
or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
$text = $template1->fill_in();
print +($text eq $result ? '' : 'not '), "ok $n\n";
$n++;
# (2) Now with delimiter choice deferred until fill-in time.
$template1 = Text::Template->new(TYPE => STRING, SOURCE => $template);
$text = $template1->fill_in(DELIMITERS => ['<<', '>>']);
print +($text eq $result ? '' : 'not '), "ok $n\n";
$n++;
# (3) Now we'll try using regex metacharacters
# First with the delimiters specified at object creation time
$template = q{The value of $V is [$V].};
$template1 = Text::Template->new(TYPE => STRING,
SOURCE => $template,
DELIMITERS => ['[', ']']
)
or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
$text = $template1->fill_in();
print +($text eq $result ? '' : 'not '), "ok $n\n";
$n++;
# (4) Now with delimiter choice deferred until fill-in time.
$template1 = Text::Template->new(TYPE => STRING, SOURCE => $template);
$text = $template1->fill_in(DELIMITERS => ['[', ']']);
print +($text eq $result ? '' : 'not '), "ok $n\n";
$n++;
# (5-18) Make sure \ is working properly
# (That is to say, it is ignored.)
# These tests are similar to those in 01-basic.t.
my @tests = ('{""}' => '', # (5)
# Backslashes don't matter
'{"}"}' => undef,
'{"\\}"}' => undef, # One backslash
'{"\\\\}"}' => undef, # Two backslashes
'{"\\\\\\}"}' => undef, # Three backslashes
'{"\\\\\\\\}"}' => undef, # Four backslashes (10)
'{"\\\\\\\\\\}"}' => undef, # Five backslashes
# Backslashes are always passed directly to Perl
'{"x20"}' => 'x20',
'{"\\x20"}' => ' ', # One backslash
'{"\\\\x20"}' => '\\x20', # Two backslashes
'{"\\\\\\x20"}' => '\\ ', # Three backslashes (15)
'{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes
'{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
'{"\\x20\\}"}' => undef, # (18)
);
my $i;
for ($i=0; $i<@tests; $i+=2) {
my $tmpl = Text::Template->new(TYPE => 'STRING',
SOURCE => $tests[$i],
DELIMITERS => ['{', '}'],
);
my $text = $tmpl->fill_in;
my $result = $tests[$i+1];
my $ok = (! defined $text && ! defined $result
|| $text eq $result);
unless ($ok) {
print STDERR "($n) expected .$result., got .$text.\n";
}
print +($ok ? '' : 'not '), "ok $n\n";
$n++;
}
exit;
#!perl
#
# Tests for PREPEND features
# These tests first appeared in version 1.22.
use Text::Template;
die "This is the test program for Text::Template version 1.46
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
print "1..9\n";
my $n = 1;
@Emptyclass1::ISA = 'Text::Template';
@Emptyclass2::ISA = 'Text::Template';
my $tin = q{The value of $foo is: {$foo}};
Text::Template->always_prepend(q{$foo = "global"});
$tmpl1 = Text::Template->new(TYPE => 'STRING',
SOURCE => $tin,
);
$tmpl2 = Text::Template->new(TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"},
);
$tmpl1->compile;
$tmpl2->compile;
$t1 = $tmpl1->fill_in(PACKAGE => 'T1');
$t2 = $tmpl2->fill_in(PACKAGE => 'T2');
$t3 = $tmpl2->fill_in(PREPEND => q{$foo = "fillin"}, PACKAGE => 'T3');
($t1 eq 'The value of $foo is: global') or print "not ";
print "ok $n\n"; $n++;
($t2 eq 'The value of $foo is: template') or print "not ";
print "ok $n\n"; $n++;
($t3 eq 'The value of $foo is: fillin') or print "not ";
print "ok $n\n"; $n++;
Emptyclass1->always_prepend(q{$foo = 'Emptyclass global';});
$tmpl1 = Emptyclass1->new(TYPE => 'STRING',
SOURCE => $tin,
);
$tmpl2 = Emptyclass1->new(TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"},
);
$tmpl1->compile;
$tmpl2->compile;
$t1 = $tmpl1->fill_in(PACKAGE => 'T4');
$t2 = $tmpl2->fill_in(PACKAGE => 'T5');
$t3 = $tmpl2->fill_in(PREPEND => q{$foo = "fillin"}, PACKAGE => 'T6');
($t1 eq 'The value of $foo is: Emptyclass global') or print "not ";
print "ok $n\n"; $n++;
($t2 eq 'The value of $foo is: template') or print "not ";
print "ok $n\n"; $n++;
($t3 eq 'The value of $foo is: fillin') or print "not ";
print "ok $n\n"; $n++;
$tmpl1 = Emptyclass2->new(TYPE => 'STRING',
SOURCE => $tin,
);
$tmpl2 = Emptyclass2->new(TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"},
);
$tmpl1->compile;
$tmpl2->compile;
$t1 = $tmpl1->fill_in(PACKAGE => 'T4');
$t2 = $tmpl2->fill_in(PACKAGE => 'T5');
$t3 = $tmpl2->fill_in(PREPEND => q{$foo = "fillin"}, PACKAGE => 'T6');
($t1 eq 'The value of $foo is: global') or print "not ";
print "ok $n\n"; $n++;
($t2 eq 'The value of $foo is: template') or print "not ";
print "ok $n\n"; $n++;
($t3 eq 'The value of $foo is: fillin') or print "not ";
print "ok $n\n"; $n++;
#!perl
#
# Tests for PREPROCESSOR features
# These tests first appeared in version 1.25.
use Text::Template::Preprocess;
die "This is the test program for Text::Template::Preprocess version 1.46.
You are using version $Text::Template::Preprocess::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::Preprocess::VERSION == 1.46;
$TMPFILE = "tt$$";
print "1..8\n";
my $n = 1;
my $py = sub { tr/x/y/ };
my $pz = sub { tr/x/z/ };
my $t = 'xxx The value of $x is {$x}';
my $outx = 'xxx The value of $x is 119';
my $outy = 'yyy The value of $y is 23';
my $outz = 'zzz The value of $z is 5';
open TF, "> $TMPFILE" or die "Couldn't open test file: $!; aborting";
print TF $t;
close TF;
@result = ($outx, $outy, $outz, $outz);
for my $trial (1, 0) {
for my $test (0 .. 3) {
my $tmpl;
if ($trial == 0) {
$tmpl = new Text::Template::Preprocess
(TYPE => 'STRING', SOURCE => $t) or die;
} else {
open TF, "< $TMPFILE" or die "Couldn't open test file: $!; aborting";
$tmpl = new Text::Template::Preprocess
(TYPE => 'FILEHANDLE', SOURCE => \*TF) or die;
}
$tmpl->preprocessor($py) if ($test & 1) == 1;
my @args = ((($test & 2) == 2) ? (PREPROCESSOR => $pz) : ());
my $o = $tmpl->fill_in(@args,
HASH => {x => 119, 'y' => 23, z => 5});
# print STDERR "$o/$result[$test]\n";
print +(($o eq $result[$test]) ? '' : 'not '), "ok $n\n";
$n++;
}
}
unlink $TMPFILE;
#!perl
# test apparatus for Text::Template module
use Text::Template;
print "1..5\n";
$n=1;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
# (1) basic error delivery
{ my $r = Text::Template->new(TYPE => 'string',
SOURCE => '{1/0}',
)->fill_in();
if ($r eq q{Program fragment delivered error ``Illegal division by zero at template line 1.''}) {
print "ok $n\n";
} else {
print "not ok $n\n# $r\n";
}
$n++;
}
# (2) BROKEN sub called in ->new?
{ my $r = Text::Template->new(TYPE => 'string',
SOURCE => '{1/0}',
BROKEN => sub {'---'},
)->fill_in();
if ($r eq q{---}) {
print "ok $n\n";
} else {
print "not ok $n\n# $r\n";
}
$n++;
}
# (3) BROKEN sub called in ->fill_in?
{ my $r = Text::Template->new(TYPE => 'string',
SOURCE => '{1/0}',
)->fill_in(BROKEN => sub {'---'});
if ($r eq q{---}) {
print "ok $n\n";
} else {
print "not ok $n\n# $r\n";
}
$n++;
}
# (4) BROKEN sub passed correct args when called in ->new?
{ my $r = Text::Template->new(TYPE => 'string',
SOURCE => '{1/0}',
BROKEN => sub { my %a = @_;
qq{$a{lineno},$a{error},$a{text}}
},
)->fill_in();
if ($r eq qq{1,Illegal division by zero at template line 1.\n,1/0}) {
print "ok $n\n";
} else {
print "not ok $n\n# $r\n";
}
$n++;
}
# (5) BROKEN sub passed correct args when called in ->fill_in?
{ my $r = Text::Template->new(TYPE => 'string',
SOURCE => '{1/0}',
)->fill_in(BROKEN =>
sub { my %a = @_;
qq{$a{lineno},$a{error},$a{text}}
});
if ($r eq qq{1,Illegal division by zero at template line 1.\n,1/0}) {
print "ok $n\n";
} else {
print "not ok $n\n# $r\n";
}
$n++;
}
Revision history for Text::Template
1.56 2019-07-09
- Fix typos in Changes
1.55 2019-02-25
- Improve AppVeyor tests for older Perls (Thanks Roy Ivy)
- Check for Test::More 0.94 and skip tests if not installed where
done_testing() is used (Thanks Roy Ivy).
- Improve workaround for broken Win32 File::Temp taint failure (Thanks Roy Ivy).
- Skip/todo tests which fail under Devel::Cover (Thanks Roy Ivy)
- Add checks and skip_all checks for non-core test modules (Thanks Roy Ivy)
1.54 2019-01-13
- Fix tempfile creation during tests on Win32
1.53 2018-05-02
- Add support for decoding template files via ENCODING constructor arg
[github #11]
- Docs cleanup: replace indirect-object style examples and use class method
style constructor calls in the POD docs
- Docs cleanup: remove hard tabs from POD, replace dated, unfair synopsis
[github #5], convert "THANKS" section to a POD list
1.52 2018-03-19
- Fix possible 'Subroutine ... redefined' warning (Github #10)
1.51 2018-03-04
- Add test for nested tags breakage that happened in v1.46
- Turn off strict+warnings in sections where template code is eval'ed
[github #9]
1.50 2018-02-10
*** Revert support for identical start/end delimiters (e.g.: @@foo@@, XXfooXX)
due to breakage with nested tags (see
https://github.com/mschout/perl-text-template/issues/8). Will revisit
this in a future release.
1.49 2018-02-07
- Fix failing tests in v1.48 under perl < 5.8.9
1.48 2018-02-07
- remove COPYING and Artistic files from the dist. These are replaced by
the Dist::Zilla generated LICENSE file.
- use strict/warnings (thanks Mohammad S Anwar)
- remove $VERSION checks from tests. This makes it easier to run the test
with Dist::Zilla and avoids maintenance issue of updating the tests for
each release (Thanks Andrew Ruder).
- Allow precompiled templates to work with preprocessing [#29928] (Thanks
Nik LaBelle)
- Add "strict" option to fill_in(). This adds "use strict" and "use vars
(...)" to the prepend section, and only the keys of the HASH option are
allowed in the template. (Thanks Desmond Daignault, Kivanc Yazan, CJM)
[55696]
- Fix templates with inline comments without newline after comment for perl
< 5.18 [34292]
- Don't use bareword file handles
- use three arg form of open()
- Fix BROKEN behaviour so that it returns the text accumulated so far on
undef as documented [28974]
- Source code cleanups
- Minimum perl version is now 5.8.0
- Allow start/end delimiters to be identical (e.g.: @@foo@@, XXfooXX)
(Thanks mirod) [46639]
- Fix + document the FILENAME parameter to fill_in() (Thanks VDB) [106093]
- Test suite cleanups:
+ turn on strict/warnings for all tests
+ run tests through perltidy and formatting cleanup
+ remove number prefixes from test names
+ use Test::More instead of generating TAP by hand
+ use three-arg form of open()
+ don't use indirect object syntax
+ don't use bareword file handles
+ use File::Temp to generate temporary files
1.47 2017-02-27
- Fix longstanding memory leak in _scrubpkg() [#22031]
- Fix various spelling errors [#86872]
NOTE: Changes for versions prior to 1.47 have been imported from README
1.46 2013-02-11
- Thanks to Rik Signes, there is a new
Text::Template->append_text_to_output method, which Text::Template always
uses whenever it wants to emit output. You can subclass this to get
control over the output, for example for postprocessing.
- A spurious warning is no longer emitted when the TYPE parameter to ->new
is omitted.
1.45 2008-04-16
1.44 2003-04-29
- This is a maintenance release. There are no feature changes.
- _scrubpkg, which was responsible for eptying out temporary packages after
the module had done with them, wasn't always working; the result was
memory leaks in long-running applications. This should be fixed now, and
there is a test in the test suite for it.
- Minor changes to the test suite to prevent spurious errors.
- Minor documentation changes.
1.43 2002-03-25
- The ->new method now fails immediately and sets $Text::Template::ERROR if
the file that is named by a filename argument does not exist or cannot be
opened for some other reason. Formerly, the constructor would succeed
and the ->fill_in call would fail.
1.42 2001-11-05
- This is a maintenance release. There are no feature changes.
- Fixed a bug relating to use of UNTAINT under perl 5.005_03 and possibly
other versions.
- Taint-related tests are now more comprehensive.
1.41 2001-09-04
- This is a maintenance release. There are no feature changes.
- Tests now work correctly on Windows systems and possibly on other
non-unix systems.
1.40 2001-08-30
*** INCOMPATIBLE CHANGE ***
- The format of the default error message has changed. It used to look
like:
Program fragment at line 30 delivered error ``Illegal division by zero''
It now looks like:
Program fragment delivered error ``Illegal division by zero at catalog.tmpl line 37''
Note that the default message used to report the line number at which the
program fragment began; it now reports the line number at which the error
actually occurred.
*** INCOMPATIBLE CHANGE ***
- The format of the default error message has changed. It used to look like:
Program fragment at line 30 delivered error ``Illegal division by zero''
It now looks like:
Program fragment delivered error ``Illegal division by zero at catalog.tmpl line 37''
- Note that the default message used to report the line number at which the
program fragment began; it now reports the line number at which the error
actually occurred.
- New UNTAINT option tells the module that it is safe to 'eval' code even
though it has come from a file or filehandle.
- Code added to prevent memory leaks when filling many templates. Thanks
to Itamar Almeida de Carvalho.
- Bug fix: $OUT was not correctly initialized when used in conjunction
with SAFE.
- You may now use a glob ref when passing a filehandle to the ->new
function. Formerly, a glob was required.
- New subclass: Text::Template::Preprocess. Just like Text::Template, but
you may supply a PREPROCESS option in the constructor or the fill_in
call; this is a function which receives each code fragment prior to
evaluation, and which may modify and return the fragment; the modified
fragment is what is evaluated.
- Error messages passed to BROKEN subroutines will now report the correct
line number of the template at which the error occurred:
Illegal division by zero at template line 37.
- If the template comes from a file, the filename will be reported as well:
Illegal division by zero at catalog.tmpl line 37.
- New UNTAINT option tells the module that it is safe to eval template code
even if it has come from a file or filehandle, disabling taint checking
in these cases.
- Code added to prevent memory leaks when filling many templates. Thanks to
Itamar Almeida de Carvalho.
- Bug fix: $OUT was not always correctly initialized when used in
conjunction with SAFE.
- You may now use a glob ref when passing a filehandle to the new function.
Formerly, a glob was required.
- Error messages passed to BROKEN subroutines will now report the correct
line number of the template at which the error occurred:
Illegal division by zero at template line 37.
If the template comes from a file, the filename will be reported as well:
Illegal division by zero at catalog.tmpl line 37.
- New subclass: Text::Template::Preprocess. Just like Text::Template, but
you may supply a PREPROCESS option in the fill_in call; this is a
function which receives each code fragment prior to evaluation, and which
may modify and return the fragment; the modified fragment is what is
evaluated.
1.31 2001-02-05
- Maintenance and bug fix release
- fill_in_string was failing. Thanks to Donald L. Greer Jr. for the test case.
1.23 1999-12-21
- Small bug fix: DELIMITER and other arguments were being ignored in calls
to fill_in_file and fill_this_in. (Thanks to Jonathan Roy for reporting
this.)
1.22
- You can now specify that certain Perl statements be prepended to the
beginning of every program fragment in a template, either per template,
or for all templates, or for the duration of only one call to fill_in.
This is useful, for example, if you want to enable `strict' checks in
your templates but you don't want to manually add `use strict' to the
front of every program fragment everywhere.
1.20 1999-03-08
- You can now specify that the program fragment delimiters are strings
other than { and }. This has three interesting effects: First, it
changes the delimiter strings. Second, it disables the special meaning
of \, so you have to be really, really sure that the delimiters will not
appear in your templates. And third, because of the simplifications
introduced by the elimination of \ processing, template parsing is 20-25%
faster. See the manual section on `Alternative Delimiters'.
- Fixed bug having to do with undefined values in HASH options. In
particular, Text::Template no longer generates a warning if you try to
give a variable an undefined value.
1.12 1999-02-28
- I forgot to say that Text::Template ISA Exporter, so the exported
functions never got exported. Duhhh!
- Template TYPEs are now case-insensitive. The `new' method now diagnoses
attempts to use an invalid TYPE.
- More tests for these things.
1.11 1999-02-25
- Fixed a bug in the way backslashes were processed. The 1.10 behavior was
incompatible with the beta versions and was also inconvenient. (`\n' in
templates was replaced with `n' before it was given to Perl for
evaluation.) The new behavior is also incompatible with the beta
versions, but it is only a little bit incompatible, and it is probably
better.
- Documentation for the new behavior, and tests for the bug.
1.10 1999-02-13
- New OUTPUT option delivers template results directly to a filehandle
instead of making them into a string. Saves space and time.
- PACKAGE and HASH now work intelligently with SAFE.
- Fragments may now output data directly to the template, rather than
having to arrange to return it as a return value at the end. This means
that where you used to have to write this:
{ my $blist = '';
foreach $i (@items) {
$blist .= qq{ * $i\n};
}
$blist;
}
You can now write this instead, because $OUT is special.
{ foreach $i (@items) {
$OUT.= " * $i\n";
}
}
(`A spoonful of sugar makes the medicine go down.')
- Fixed some small bugs. Worked around a bug in Perl that does the wrong
thing with $x = <Y> when $x contains a glob.
- More documentation. Errors fixed.
- Lots more tests.
1.03 1999-02-06
- Code added to support HASH option to fill_in. (Incl. `_gensym'
function.)
- Documentation for HASH.
- New test file for HASH.
- Note about failure of lexical variables to propagate into templates. Why
does this surprise people?
- Bug fix: program fragments are evaluated in an environment with `no
strict' by default. Otherwise, you get a lot of `Global symbol "$v"
requires explicit package name' failures. Why didn't the test program
pick this up? Because the only variable the test program ever used was
`$a', which is exempt. Duhhhhh.
- Fixed the test program.
- Various minor documentation fixes.
1.00 1999-02-05
This is a complete rewrite. The new version delivers better functionality
but is only 2/3 as long, which I think is a good sign. It is supposed to be
100% backward-compatible with the previous versions. With one cosmetic
change, it passes the test suite that the previous versions passed. If you
have compatibility problems, please mail me immediately.
- At least twice as fast
- Better support for filling out the same template more than once
- Now supports evaluation of program fragments in Safe compartments.
(Thanks, Jonathan!)
- Better argument syntax
- More convenience functions
- The parser is much better and simpler
- Once a template is parsed, the parsed version is stored so that
it needn't be parsed again.
- BROKEN function behavior is rationalized. You can now pass an
arbitrary argument to your BROKEN function, or return a value
from it to the main program.
- Documentation overhauled.
Previous Versions
- Maintained by Mark Jason Dominus (MJD)
# This file was automatically generated by Dist::Zilla::Plugin::Manifest v6.012.
Changes
INSTALL
LICENSE
MANIFEST
META.json
META.yml
Makefile.PL
README
SIGNATURE
lib/Text/Template.pm
lib/Text/Template/Preprocess.pm
t/author-pod-syntax.t
t/author-signature.t
t/basic.t
t/broken.t
t/delimiters.t
t/error.t
t/exported.t
t/hash.t
t/inline-comment.t
t/nested-tags.t
t/ofh.t
t/out.t
t/prepend.t
t/preprocess.t
t/rt29928.t
t/safe.t
t/safe2.t
t/safe3.t
t/strict.t
t/taint.t
t/template-encoding.t
t/warnings.t
{
"abstract" : "Expand template text with embedded Perl",
"author" : [
"Michael Schout <mschout@cpan.org>"
],
"dynamic_config" : 0,
"generated_by" : "Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010",
"license" : [
"perl_5"
],
"meta-spec" : {
"url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
"version" : 2
},
"name" : "Text-Template",
"prereqs" : {
"configure" : {
"requires" : {
"ExtUtils::MakeMaker" : "0",
"perl" : "5.008"
}
},
"develop" : {
"requires" : {
"Dist::Zilla" : "5",
"Dist::Zilla::PluginBundle::MSCHOUT" : "0",
"Software::License::Perl_5" : "0",
"Test::Pod" : "1.41",
"Test::Signature" : "0"
}
},
"runtime" : {
"requires" : {
"Carp" : "0",
"Encode" : "0",
"Exporter" : "0",
"base" : "0",
"perl" : "5.008",
"strict" : "0",
"warnings" : "0"
}
},
"test" : {
"requires" : {
"File::Temp" : "0",
"Safe" : "0",
"Test::More" : "0",
"Test::More::UTF8" : "0",
"Test::Warnings" : "0",
"lib" : "0",
"perl" : "5.008",
"utf8" : "0",
"vars" : "0"
}
}
},
"provides" : {
"Text::Template" : {
"file" : "lib/Text/Template.pm",
"version" : "1.56"
},
"Text::Template::Preprocess" : {
"file" : "lib/Text/Template/Preprocess.pm",
"version" : "1.56"
}
},
"release_status" : "stable",
"resources" : {
"bugtracker" : {
"web" : "https://github.com/mschout/perl-text-template/issues"
},
"homepage" : "https://github.com/mschout/perl-text-template",
"repository" : {
"type" : "git",
"url" : "https://github.com/mschout/perl-text-template.git",
"web" : "https://github.com/mschout/perl-text-template"
}
},
"version" : "1.56",
"x_generated_by_perl" : "v5.26.2",
"x_serialization_backend" : "Cpanel::JSON::XS version 4.04"
}
---
abstract: 'Expand template text with embedded Perl'
author:
- 'Michael Schout <mschout@cpan.org>'
build_requires:
File::Temp: '0'
Safe: '0'
Test::More: '0'
Test::More::UTF8: '0'
Test::Warnings: '0'
lib: '0'
perl: '5.008'
utf8: '0'
vars: '0'
configure_requires:
ExtUtils::MakeMaker: '0'
perl: '5.008'
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.012, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: Text-Template
provides:
Text::Template:
file: lib/Text/Template.pm
version: '1.56'
Text::Template::Preprocess:
file: lib/Text/Template/Preprocess.pm
version: '1.56'
requires:
Carp: '0'
Encode: '0'
Exporter: '0'
base: '0'
perl: '5.008'
strict: '0'
warnings: '0'
resources:
bugtracker: https://github.com/mschout/perl-text-template/issues
homepage: https://github.com/mschout/perl-text-template
repository: https://github.com/mschout/perl-text-template.git
version: '1.56'
x_generated_by_perl: v5.26.2
x_serialization_backend: 'YAML::Tiny version 1.73'
# This file was automatically generated by Dist::Zilla::Plugin::MakeMaker v6.012.
use strict;
use warnings;
use 5.008;
use ExtUtils::MakeMaker;
my %WriteMakefileArgs = (
"ABSTRACT" => "Expand template text with embedded Perl",
"AUTHOR" => "Michael Schout <mschout\@cpan.org>",
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => 0
},
"DISTNAME" => "Text-Template",
"LICENSE" => "perl",
"MIN_PERL_VERSION" => "5.008",
"NAME" => "Text::Template",
"PREREQ_PM" => {
"Carp" => 0,
"Encode" => 0,
"Exporter" => 0,
"base" => 0,
"strict" => 0,
"warnings" => 0
},
"TEST_REQUIRES" => {
"File::Temp" => 0,
"Safe" => 0,
"Test::More" => 0,
"Test::More::UTF8" => 0,
"Test::Warnings" => 0,
"lib" => 0,
"utf8" => 0,
"vars" => 0
},
"VERSION" => "1.56",
"test" => {
"TESTS" => "t/*.t"
}
);
my %FallbackPrereqs = (
"Carp" => 0,
"Encode" => 0,
"Exporter" => 0,
"File::Temp" => 0,
"Safe" => 0,
"Test::More" => 0,
"Test::More::UTF8" => 0,
"Test::Warnings" => 0,
"base" => 0,
"lib" => 0,
"strict" => 0,
"utf8" => 0,
"vars" => 0,
"warnings" => 0
);
unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) {
delete $WriteMakefileArgs{TEST_REQUIRES};
delete $WriteMakefileArgs{BUILD_REQUIRES};
$WriteMakefileArgs{PREREQ_PM} = \%FallbackPrereqs;
}
delete $WriteMakefileArgs{CONFIGURE_REQUIRES}
unless eval { ExtUtils::MakeMaker->VERSION(6.52) };
WriteMakefile(%WriteMakefileArgs);
Text::Template v1.46
This is a library for generating form letters, building HTML pages, or
filling in templates generally. A `template' is a piece of text that
has little Perl programs embedded in it here and there. When you
`fill in' a template, you evaluate the little programs and replace
them with their values.
Here's an example of a template:
Dear {$title} {$lastname},
It has come to our attention that you are delinquent in your
{$monthname[$last_paid_month]} payment. Please remit
${sprintf("%.2f", $amount)} immediately, or your patellae may
be needlessly endangered.
Love,
Mark "{nickname(rand 20)}" Dominus
The result of filling in this template is a string, which might look
something like this:
Dear Mr. Gates,
It has come to our attention that you are delinquent in your
February payment. Please remit
$392.12 immediately, or your patellae may
be needlessly endangered.
Love,
Mark "Vizopteryx" Dominus
You can store a template in a file outside your program. People can
modify the template without modifying the program. You can separate
the formatting details from the main code, and put the formatting
parts of the program into the template. That prevents code bloat and
encourages functional separation.
You can fill in the template in a `Safe' compartment. This means that
if you don't trust the person who wrote the code in the template, you
won't have to worry that they are tampering with your program when you
execute it.
----------------------------------------------------------------
Text::Template was originally released some time in late 1995 or early
1996. After three years of study and investigation, I rewrote it from
scratch in January 1999. The new version, 1.0, was much faster,
delivered better functionality and was almost 100% backward-compatible
with the previous beta versions.
I have added a number of useful features and conveniences since the
1.0 release, while still retaining backward compatibility. With one
merely cosmetic change, the current version of Text::Template passes
the test suite that the old beta versions passed.
This file contains message digests of all files listed in MANIFEST,
signed via the Module::Signature module, version 0.81.
To verify the content in this distribution, first make sure you have
Module::Signature installed, then type:
% cpansign -v
It will check each file's integrity, as well as the signature's
validity. If "==> Signature verified OK! <==" is not displayed,
the distribution may already have been compromised, and you should
not run its Makefile.PL or Build.PL.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
SHA1 8ba1381d3fc8d81457c35e5aff52b8e55d57be7c Changes
SHA1 b457bd56a70b838ccc55d183ab09de64b6996958 INSTALL
SHA1 f12894289cb0f379f24b8d63e2e761dbcba1b216 LICENSE
SHA1 2c21cb13f53da41c4b30011aca9014db2de46862 MANIFEST
SHA1 ea82a70dfcffe05202868dfe02826aaf1f6e0229 META.json
SHA1 9ad7419fb6209e81652da42967995c8fb8f1826b META.yml
SHA1 f7634b46dde2cf8c6f31fe46327d15151d654a2c Makefile.PL
SHA1 b94aaad0a0bf2c323061bfefb9cf1fd532f14e7b README
SHA1 090d77972c087a8905fa85522854afbf4ccc999b lib/Text/Template.pm
SHA1 ca5251a021e46b60603f10e757d689e52fde1feb lib/Text/Template/Preprocess.pm
SHA1 8efad25309730a4d501fb40fc03eda4697303372 t/author-pod-syntax.t
SHA1 19cc343f8a85c6805bbeb02580487483a6283887 t/author-signature.t
SHA1 ae085010c9f08576ef8584f224e38e6a98c1c178 t/basic.t
SHA1 006feb1a0b1e5780db52aa79bd38933664a8339a t/broken.t
SHA1 dee8cef1fcd43ce5de462018f8539d4a0fbc460f t/delimiters.t
SHA1 304955c4280159ec3a4c0f2717dcff9c887bb487 t/error.t
SHA1 c862dfc08e00e76b3f2aee953583d3cc8e5524a2 t/exported.t
SHA1 50ef92bda3b6b5cbd5a9307e6f17ce49ee8f245c t/hash.t
SHA1 d5dc210684aec8bb2c4817af96597c86047169c1 t/inline-comment.t
SHA1 31ff85f423178f2d6638d35edf859d73f63dd5c7 t/nested-tags.t
SHA1 62ae0720aa86146bccfa23d2c903fa142cb86d50 t/ofh.t
SHA1 68093417d49a2afdfcd4642bacea04466039b734 t/out.t
SHA1 a8b21fdca0f1d243775a00758105e0fcc58022aa t/prepend.t
SHA1 4e7e00eccede7c3231e93ef0f3cb011423be4eb5 t/preprocess.t
SHA1 a52d61ef92e6a88d694db0be4893b88417a72f9c t/rt29928.t
SHA1 5186ff459c6042af11bca92decd271887c7b2eae t/safe.t
SHA1 aa0c9ff96d66c1f74fc7ac73ce173c9f741f552e t/safe2.t
SHA1 b50a51577c0f2c13c9a48113dc7f061385a02219 t/safe3.t
SHA1 16d3abf7588da4c0056c6c6b7818470c8601577c t/strict.t
SHA1 f325ebf739e2aec3ae62427aef0c4e86de58ad29 t/taint.t
SHA1 4dac28585388482f1719f404cc357991af77e345 t/template-encoding.t
SHA1 ce1da9bf88d6ea62d7c756f0d730dfb3c5888b6e t/warnings.t
-----BEGIN PGP SIGNATURE-----
iQJFBAEBCAAvFiEE2EtuRfhGgngE8PsARAzvLrlUzY4FAl0kljYRHG1zY2hvdXRA
Y3Bhbi5vcmcACgkQRAzvLrlUzY6BdRAAlu/LI+71ax6OJfn6O6SslZCKdIfef1+P
UQ3eQCzbUOK1hP9p6TNOMFv0xPew7a1jSsM/wLjryXKLqGIclonBopRisZjCSaFa
DSH+5k4A9TWKo7n08C3nydVZPTfHXLEO6tHIH2umDHjMyC1gDkApjvSqH53OCtoV
vUTqQn9c6MxmuIWus1gjVynTvXWtBmaqrvTuQENFUXsGH5gVVbEr0VNIyt6Ip3p5
HnRe8UdykFJZvfhEhH1pbt2j513m9Bqce0eSb/eshraFugx7coD3gk2EXqp8eVzK
84vPEcNC8Moii8+qg48VDh9wBq1u/cba2U7PcVjj3lRk/pAni31XnlXLPWJ0dZLF
fR4glE86ESTGSyI2wGDxyEdev+e64sk7geYmL388kqtpqkKWvALmQkKUJg0T0ppp
LYv31DrsP4QM1duv46y3wVbqUPeQlKBOU81vfyAonrD02tRZjwRpCD67CZ9WSgZu
el85yPneIIijYEOzV8GYHKNiiZKpu2uidKCe+MFGxBtXe9hyi9x95tuyi9/pu2FB
YjbGJ8Wb4xhS7zdDWZb/axfwbcGMVml6a6xA98jVkwjHhPvKsf3qWVZZPtWneeN0
pGNpMta73wQxrp5/W1GfvBfPVlsker6SAARg2NMwS9Wmp+ZuD6gdJ+aN0/PPtNUd
HQJJFjr/xo4=
=nx9E
-----END PGP SIGNATURE-----
package Text::Template::Preprocess;
$Text::Template::Preprocess::VERSION = '1.56';
# ABSTRACT: Expand template text with embedded Perl
use strict;
use warnings;
use Text::Template;
@ISA = qw(Text::Template);
$Text::Template::Preprocess::VERSION = 1.46;
our @ISA = qw(Text::Template);
sub fill_in {
my $self = shift;
my (%args) = @_;
my $pp = $args{PREPROCESSOR} || $self->{PREPROCESSOR} ;
if ($pp) {
local $_ = $self->source();
# print "# fill_in: before <$_>\n";
&$pp;
# print "# fill_in: after <$_>\n";
$self->set_source_data($_);
}
$self->SUPER::fill_in(@_);
my $self = shift;
my (%args) = @_;
my $pp = $args{PREPROCESSOR} || $self->{PREPROCESSOR};
if ($pp) {
local $_ = $self->source();
my $type = $self->{TYPE};
# print "# fill_in: before <$_>\n";
&$pp;
# print "# fill_in: after <$_>\n";
$self->set_source_data($_, $type);
}
$self->SUPER::fill_in(@_);
}
sub preprocessor {
my ($self, $pp) = @_;
my $old_pp = $self->{PREPROCESSOR};
$self->{PREPROCESSOR} = $pp if @_ > 1; # OK to pass $pp=undef
$old_pp;
my ($self, $pp) = @_;
my $old_pp = $self->{PREPROCESSOR};
$self->{PREPROCESSOR} = $pp if @_ > 1; # OK to pass $pp=undef
$old_pp;
}
1;
__END__
=head1 NAME
=pod
=encoding UTF-8
=head1 NAME
Text::Template::Preprocess - Expand template text with embedded Perl
=head1 VERSION
This file documents C<Text::Template::Preprocess> version B<1.46>
version 1.56
=head1 SYNOPSIS
......@@ -78,7 +97,6 @@ C<Text::Template>.
One possible purpose: If your files contain a lot of JavaScript, like
this:
Plain text here...
{ perl code }
<script language=JavaScript>
......@@ -98,14 +116,25 @@ JavaScript program with executable Perl code. One strategy:
Then use C<PREPROCESSOR =E<gt> \&quote_scripts>. This will transform
=head1 SEE ALSO
L<Text::Template>
=head1 AUTHOR
=head1 SOURCE
The development version is on github at L<https://https://github.com/mschout/perl-text-template>
and may be cloned from L<git://https://github.com/mschout/perl-text-template.git>
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
L<https://github.com/mschout/perl-text-template/issues>
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 AUTHOR
Mark Jason Dominus, Plover Systems
......@@ -118,27 +147,11 @@ C<mjd-perl-template-request@plover.com> to join.
For updates, visit C<http://www.plover.com/~mjd/perl/Template/>.
=head1 LICENSE
Text::Template::Preprocess version 1.46
Copyright 2013 Mark Jason Dominus
=head1 COPYRIGHT AND LICENSE
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. You may also can
redistribute it and/or modify it under the terms of the Perl
Artistic License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received copies of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
This software is copyright (c) 2013 by Mark Jason Dominus <mjd@cpan.org>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
#!perl
BEGIN {
unless ($ENV{AUTHOR_TESTING}) {
print qq{1..0 # SKIP these tests are for testing by the author\n};
exit
}
}
# This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests.
use strict; use warnings;
use Test::More;
use Test::Pod 1.41;
all_pod_files_ok();
#!perl -w
BEGIN {
unless ($ENV{AUTHOR_TESTING}) {
print qq{1..0 # SKIP these tests are for testing by the author\n};
exit
}
}
# This file was automatically generated by Dist::Zilla::Plugin::AuthorSignatureTest
use strict;
use warnings;
use Test::More;
unless (eval { require Test::Signature; 1 }) {
plan skip_all => 'Test::Signature is required for this test';
}
Test::Signature::signature_ok();
done_testing;
#!perl
#
# Tests of basic, essential functionality
#
use strict;
use warnings;
use Test::More tests => 34;
use File::Temp;
my $tmpfile = File::Temp->new;
use_ok 'Text::Template' or exit 1;
$X::v = $Y::v = 0; # Suppress `var used only once'
my $template_1 = <<EOM;
We will put value of \$v (which is "abc") here -> {\$v}
We will evaluate 1+1 here -> {1 + 1}
EOM
# (1) Construct temporary template file for testing
# file operations
my $TEMPFILE = $tmpfile->filename;
eval {
open my $tmp, '>', $TEMPFILE
or die "Couldn't write tempfile $TEMPFILE: $!";
print $tmp $template_1;
close $tmp;
pass;
};
if ($@) {
fail $@;
}
# (2) Build template from file
my $template = Text::Template->new('type' => 'FILE', 'source' => $TEMPFILE);
ok(defined $template) or diag $Text::Template::ERROR;
# (3) Fill in template from file
$X::v = "abc";
my $resultX = <<EOM;
We will put value of \$v (which is "abc") here -> abc
We will evaluate 1+1 here -> 2
EOM
$Y::v = "ABC";
my $resultY = <<EOM;
We will put value of \$v (which is "abc") here -> ABC
We will evaluate 1+1 here -> 2
EOM
my $text = $template->fill_in('package' => 'X');
is $text, $resultX;
# (4) Fill in same template again
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;
# (5) Simple test of `fill_this_in'
$text = Text::Template->fill_this_in($template_1, 'package' => 'X');
is $text, $resultX;
# (6) test creation of template from filehandle
open my $tmpl, '<', $TEMPFILE or die "failed to open $TEMPFILE: $!";
$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
ok defined $template or diag $Text::Template::ERROR;
# (7) test filling in of template from filehandle
$text = $template->fill_in('package' => 'X');
is $text, $resultX;
# (8) test second fill_in on same template object
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;
close $tmpl;
# (9) test creation of template from array
$template = Text::Template->new(
type => 'ARRAY',
source => [
'We will put value of $v (which is "abc") here -> {$v}', "\n",
'We will evaluate 1+1 here -> {1+1}', "\n"
]
);
ok defined $template; # or diag $Text::Template::ERROR;
# (10) test filling in of template from array
$text = $template->fill_in('package' => 'X');
is $text, $resultX;
# (11) test second fill_in on same array template object
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;
# (12) Make sure \ is working properly
# Test added for version 1.11
$tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => 'B{"\\}"}C{"\\{"}D');
# This should fail if the \ are not interpreted properly.
$text = $tmpl->fill_in();
is $text, 'B}C{D';
# (13) Make sure \ is working properly
# Test added for version 1.11
$tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => qq{A{"\t"}B});
# Symptom of old problem: ALL \ were special in templates, so
# The lexer would return (A, PROGTEXT("t"), B), and the
# result text would be AtB instead of A(tab)B.
$text = $tmpl->fill_in();
is $text, "A\tB";
# (14-27) Make sure \ is working properly
# Test added for version 1.11
# This is a sort of general test.
my @tests = (
'{""}' => '', # (14)
'{"}"}' => undef, # (15)
'{"\\}"}' => '}', # One backslash
'{"\\\\}"}' => undef, # Two backslashes
'{"\\\\\\}"}' => '}', # Three backslashes
'{"\\\\\\\\}"}' => undef, # Four backslashes
'{"\\\\\\\\\\}"}' => '\}', # Five backslashes (20)
'{"x20"}' => 'x20',
'{"\\x20"}' => ' ', # One backslash
'{"\\\\x20"}' => '\\x20', # Two backslashes
'{"\\\\\\x20"}' => '\\ ', # Three backslashes
'{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes (25)
'{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
'{"\\x20\\}"}' => ' }', # (27)
);
while (my ($test, $result) = splice @tests, 0, 2) {
my $tmpl = Text::Template->new(TYPE => 'STRING', SOURCE => $test);
my $text = $tmpl->fill_in;
ok(!defined $text && !defined $result || $text eq $result)
or diag "expected .$result. got .$text.";
}
# (28-30) I discovered that you can't pass a glob ref as your filehandle.
# MJD 20010827
# (28) test creation of template from filehandle
$tmpl = undef;
ok(open $tmpl, '<', $TEMPFILE) or diag "Couldn't open $TEMPFILE: $!";
$template = Text::Template->new(type => 'FILEHANDLE', source => $tmpl);
ok(defined $template) or diag $Text::Template::ERROR;
# (29) test filling in of template from filehandle
$text = $template->fill_in('package' => 'X');
is $text, $resultX;
# (30) test second fill_in on same template object
$text = $template->fill_in('package' => 'Y');
is $text, $resultY;
close $tmpl;
# (31) Test _scrubpkg for leakiness
$Text::Template::GEN0::test = 1;
Text::Template::_scrubpkg('Text::Template::GEN0');
ok !($Text::Template::GEN0::test
|| exists $Text::Template::GEN0::{test}
|| exists $Text::Template::{'GEN0::'});
# that filename parameter works. we use BROKEN to verify this
$text = Text::Template->new(
TYPE => 'string',
SOURCE => 'Hello {1/0}'
)->fill_in(FILENAME => 'foo.txt');
like $text, qr/division by zero at foo\.txt line 1/;
#!perl
# test apparatus for Text::Template module
use strict;
use warnings;
use Test::More tests => 7;
use_ok 'Text::Template' or exit 1;
# (1) basic error delivery
{
my $r = Text::Template->new(
TYPE => 'string',
SOURCE => '{1/0}',)->fill_in();
is $r, q{Program fragment delivered error ``Illegal division by zero at template line 1.''};
}
# (2) BROKEN sub called in ->new?
{
my $r = Text::Template->new(
TYPE => 'string',
SOURCE => '{1/0}',
BROKEN => sub { '---' },)->fill_in();
is $r, q{---};
}
# (3) BROKEN sub called in ->fill_in?
{
my $r = Text::Template->new(
TYPE => 'string',
SOURCE => '{1/0}',)->fill_in(BROKEN => sub { '---' });
is $r, q{---};
}
# (4) BROKEN sub passed correct args when called in ->new?
{
my $r = Text::Template->new(
TYPE => 'string',
SOURCE => '{1/0}',
BROKEN => sub {
my %a = @_;
qq{$a{lineno},$a{error},$a{text}};
},)->fill_in();
is $r, qq{1,Illegal division by zero at template line 1.\n,1/0};
}
# (5) BROKEN sub passed correct args when called in ->fill_in?
{
my $r = Text::Template->new(
TYPE => 'string',
SOURCE => '{1/0}',
)->fill_in(
BROKEN => sub {
my %a = @_;
qq{$a{lineno},$a{error},$a{text}};
});
is $r, qq{1,Illegal division by zero at template line 1.\n,1/0};
}
# BROKEN sub handles undef
{
my $r = Text::Template->new(TYPE => 'string', SOURCE => 'abc{1/0}defg')
->fill_in(BROKEN => sub { undef });
is $r, 'abc';
}
#!perl
#
# Tests for user-specified delimiter functions
# These tests first appeared in version 1.20.
use strict;
use warnings;
use Test::More tests => 19;
use_ok 'Text::Template' or exit 1;
# (1) Try a simple delimiter: <<..>>
# First with the delimiters specified at object creation time
our $V = $V = 119;
my $template = q{The value of $V is <<$V>>.};
my $result = q{The value of $V is 119.};
my $template1 = Text::Template->new(
TYPE => 'STRING',
SOURCE => $template,
DELIMITERS => [ '<<', '>>' ])
or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
my $text = $template1->fill_in();
is $text, $result;
# (2) Now with delimiter choice deferred until fill-in time.
$template1 = Text::Template->new(TYPE => 'STRING', SOURCE => $template);
$text = $template1->fill_in(DELIMITERS => [ '<<', '>>' ]);
is $text, $result;
# (3) Now we'll try using regex metacharacters
# First with the delimiters specified at object creation time
$template = q{The value of $V is [$V].};
$template1 = Text::Template->new(
TYPE => 'STRING',
SOURCE => $template,
DELIMITERS => [ '[', ']' ])
or die "Couldn't construct template object: $Text::Template::ERROR; aborting";
$text = $template1->fill_in();
is $text, $result;
# (4) Now with delimiter choice deferred until fill-in time.
$template1 = Text::Template->new(TYPE => 'STRING', SOURCE => $template);
$text = $template1->fill_in(DELIMITERS => [ '[', ']' ]);
is $text, $result;
# (5-18) Make sure \ is working properly
# (That is to say, it is ignored.)
# These tests are similar to those in 01-basic.t.
my @tests = (
'{""}' => '', # (5)
# Backslashes don't matter
'{"}"}' => undef,
'{"\\}"}' => undef, # One backslash
'{"\\\\}"}' => undef, # Two backslashes
'{"\\\\\\}"}' => undef, # Three backslashes
'{"\\\\\\\\}"}' => undef, # Four backslashes (10)
'{"\\\\\\\\\\}"}' => undef, # Five backslashes
# Backslashes are always passed directly to Perl
'{"x20"}' => 'x20',
'{"\\x20"}' => ' ', # One backslash
'{"\\\\x20"}' => '\\x20', # Two backslashes
'{"\\\\\\x20"}' => '\\ ', # Three backslashes (15)
'{"\\\\\\\\x20"}' => '\\\\x20', # Four backslashes
'{"\\\\\\\\\\x20"}' => '\\\\ ', # Five backslashes
'{"\\x20\\}"}' => undef, # (18)
);
while (my ($test, $result) = splice @tests, 0, 2) {
my $tmpl = Text::Template->new(
TYPE => 'STRING',
SOURCE => $test,
DELIMITERS => [ '{', '}' ]);
my $text = $tmpl->fill_in;
my $ok = (!defined $text && !defined $result || $text eq $result);
ok($ok) or diag "expected .$result., got .$text.";
}
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use strict;
use warnings;
use Test::More tests => 6;
use_ok 'Text::Template' or exit 1;
# (1-2) Missing source
eval {
Text::Template->new();
pass;
};
like $@, qr/^\QUsage: Text::Template::new(TYPE => ..., SOURCE => ...)/;
eval { Text::Template->new(TYPE => 'FILE'); };
like $@, qr/^\QUsage: Text::Template::new(TYPE => ..., SOURCE => ...)/;
# (3) Invalid type
eval { Text::Template->new(TYPE => 'wlunch', SOURCE => 'fish food'); };
like $@, qr/^\QIllegal value `WLUNCH' for TYPE parameter/;
# (4-5) File does not exist
my $o = Text::Template->new(
TYPE => 'file',
SOURCE => 'this file does not exist');
ok !defined $o;
ok defined($Text::Template::ERROR)
&& $Text::Template::ERROR =~ /^Couldn't open file/;
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use strict;
use warnings;
use Test::More tests => 7;
use File::Temp;
use_ok 'Text::Template' or exit 1;
my $tfh = File::Temp->new;
Text::Template->import('fill_in_file', 'fill_in_string');
$Q::n = $Q::n = 119;
# (1) Test fill_in_string
my $out = fill_in_string('The value of $n is {$n}.', PACKAGE => 'Q');
is $out, 'The value of $n is 119.';
# (2) Test fill_in_file
my $TEMPFILE = $tfh->filename;
print $tfh 'The value of $n is {$n}.', "\n";
close $tfh or die "Couldn't write test file: $!; aborting";
$R::n = $R::n = 8128;
$out = fill_in_file($TEMPFILE, PACKAGE => 'R');
is $out, "The value of \$n is 8128.\n";
# (3) Jonathan Roy reported this bug:
open my $ofh, '>', $TEMPFILE or die "Couldn't open test file: $!; aborting";
print $ofh "With a message here? [% \$var %]\n";
close $ofh or die "Couldn't close test file: $!; aborting";
$out = fill_in_file($TEMPFILE,
DELIMITERS => [ '[%', '%]' ],
HASH => { "var" => \"It is good!" });
is $out, "With a message here? It is good!\n";
# (4) It probably occurs in fill_this_in also:
$out = Text::Template->fill_this_in("With a message here? [% \$var %]\n",
DELIMITERS => [ '[%', '%]' ],
HASH => { "var" => \"It is good!" });
is $out, "With a message here? It is good!\n";
# (5) This test failed in 1.25. It was supplied by Donald L. Greer Jr.
# Note that it's different from (1) in that there's no explicit
# package=> argument.
use vars qw($string $foo $r);
$string = 'Hello {$foo}';
$foo = "Don";
$r = fill_in_string($string);
is $r, 'Hello Don';
# (6) This test failed in 1.25. It's a variation on (5)
package Q2;
use Text::Template 'fill_in_string';
use vars qw($string $foo $r);
$string = 'Hello {$foo}';
$foo = "Don";
$r = fill_in_string($string);
package main;
is $Q2::r, 'Hello Don';
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use strict;
use warnings;
use Test::More tests => 13;
use_ok 'Text::Template' or exit 1;
my $template = 'We will put value of $v (which is "good") here -> {$v}';
my $v = 'oops (main)';
$Q::v = 'oops (Q)';
my $vars = { 'v' => \'good' };
# (1) Build template from string
$template = Text::Template->new('type' => 'STRING', 'source' => $template);
isa_ok $template, 'Text::Template';
# (2) Fill in template in anonymous package
my $result2 = 'We will put value of $v (which is "good") here -> good';
my $text = $template->fill_in(HASH => $vars);
is $text, $result2;
# (3) Did we clobber the main variable?
is $v, 'oops (main)';
# (4) Fill in same template again
my $result4 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
is $text, $result4;
# (5) Now with a package
my $result5 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars, PACKAGE => 'Q');
is $text, $result5;
# (6) We expect to have clobbered the Q variable.
is $Q::v, 'good';
# (7) Now let's try it without a package
my $result7 = 'We will put value of $v (which is "good") here -> good';
$text = $template->fill_in(HASH => $vars);
is $text, $result7;
# (8-11) Now what does it do when we pass a hash with undefined values?
# Roy says it does something bad. (Added for 1.20.)
my $WARNINGS = 0;
{
local $SIG{__WARN__} = sub { $WARNINGS++ };
local $^W = 1; # Make sure this is on for this test
my $template8 = 'We will put value of $v (which is "good") here -> {defined $v ? "bad" : "good"}';
my $result8 = 'We will put value of $v (which is "good") here -> good';
my $template = Text::Template->new('type' => 'STRING', 'source' => $template8);
my $text = $template->fill_in(HASH => { 'v' => undef });
# (8) Did we generate a warning?
cmp_ok $WARNINGS, '==', 0;
# (9) Was the output correct?
is $text, $result8;
# (10-11) Let's try that again, with a twist this time
$WARNINGS = 0;
$text = $template->fill_in(HASH => [ { 'v' => 17 }, { 'v' => undef } ]);
# (10) Did we generate a warning?
cmp_ok $WARNINGS, '==', 0;
# (11) Was the output correct?
SKIP: {
skip 'not supported before 5.005', 1 unless $] >= 5.005;
is $text, $result8;
}
}
# (12) Now we'll test the multiple-hash option (Added for 1.20.)
$text = Text::Template::fill_in_string(q{$v: {$v}. @v: [{"@v"}].},
HASH => [
{ 'v' => 17 },
{ 'v' => [ 'a', 'b', 'c' ] },
{ 'v' => \23 }
]
);
my $result = q{$v: 23. @v: [a b c].};
is $text, $result;
#!perl
#
# Test for comments within an inline code block
use strict;
use warnings;
use Test::More tests => 2;
use_ok 'Text::Template' or exit 1;
my $tmpl = Text::Template->new(
TYPE => 'STRING',
SOURCE => "Hello {\$name#comment}");
my $vars = { name => 'Bob' };
is $tmpl->fill_in(HASH => $vars), 'Hello Bob';
#!perl
#
# Test for breakage of Dist::Milla in v1.46
#
use strict;
use warnings;
use Text::Template;
BEGIN {
# Minimum Test::More version; 0.94+ is required for `done_testing`
unless (eval { require Test::More; "$Test::More::VERSION" >= 0.94; }) {
Test::More::plan(skip_all => '[ Test::More v0.94+ ] is required for testing');
}
Test::More->import;
}
my $tmpl = Text::Template->new(
TYPE => 'STRING',
SOURCE => q| {{ '{{$NEXT}}' }} |,
DELIMITERS => [ '{{', '}}' ]);
is $tmpl->fill_in, ' {{$NEXT}} ';
done_testing;
#!perl
#
# test apparatus for Text::Template module
# still incomplete.
use strict;
use warnings;
use Test::More tests => 3;
use File::Temp;
use_ok 'Text::Template' or exit 1;
my $template = Text::Template->new(
TYPE => 'STRING',
SOURCE => q{My process ID is {$$}});
my $of = File::Temp->new;
my $text = $template->fill_in(OUTPUT => $of);
# (1) No $text should have been constructed. Return value should be true.
is $text, '1';
close $of or die "close(): $!";
open my $ifh, '<', $of->filename or die "open($of): $!";
my $t;
{ local $/; $t = <$ifh> }
close $ifh;
# (2) The text should have been printed to the file
is $t, "My process ID is $$";
......@@ -4,26 +4,20 @@
# still incomplete.
#
use Text::Template;
use strict;
use warnings;
use Test::More tests => 4;
die "This is the test program for Text::Template version 1.46
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
use_ok 'Text::Template' or exit 1;
print "1..1\n";
$n=1;
$template = q{
my $templateIN = q{
This line should have a 3: {1+2}
This line should have several numbers:
{ $t = ''; foreach $n (1 .. 20) { $t .= $n . ' ' } $t }
};
$templateOUT = q{
my $templateOUT = q{
This line should have a 3: { $OUT = 1+2 }
This line should have several numbers:
......@@ -31,26 +25,22 @@ This line should have several numbers:
};
# Build templates from string
$template = new Text::Template ('type' => 'STRING', 'source' => $template)
or die;
$templateOUT = new Text::Template ('type' => 'STRING', 'source' => $templateOUT)
or die;
my $template = Text::Template->new('type' => 'STRING', 'source' => $templateIN);
isa_ok $template, 'Text::Template';
$templateOUT = Text::Template->new('type' => 'STRING', 'source' => $templateOUT);
isa_ok $templateOUT, 'Text::Template';
# Fill in templates
$text = $template->fill_in()
or die;
$textOUT = $templateOUT->fill_in()
or die;
my $text = $template->fill_in();
my $textOUT = $templateOUT->fill_in();
# (1) They should be the same
print +($text eq $textOUT ? '' : 'not '), "ok $n\n";
$n++;
is $text, $textOUT;
# Missing: Test this feature in Safe compartments;
# Missing: Test this feature in Safe compartments;
# it's a totally different code path.
# Decision: Put that into safe.t, because that file should
# be skipped when Safe.pm is unavailable.
exit;
#!perl
#
# Tests for PREPEND features
# These tests first appeared in version 1.22.
use strict;
use warnings;
use Test::More tests => 10;
use_ok 'Text::Template' or exit 1;
@Emptyclass1::ISA = 'Text::Template';
@Emptyclass2::ISA = 'Text::Template';
my $tin = q{The value of $foo is: {$foo}};
Text::Template->always_prepend(q{$foo = "global"});
my $tmpl1 = Text::Template->new(
TYPE => 'STRING',
SOURCE => $tin);
my $tmpl2 = Text::Template->new(
TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"});
$tmpl1->compile;
$tmpl2->compile;
my $t1 = $tmpl1->fill_in(PACKAGE => 'T1');
my $t2 = $tmpl2->fill_in(PACKAGE => 'T2');
my $t3 = $tmpl2->fill_in(PREPEND => q{$foo = "fillin"}, PACKAGE => 'T3');
is $t1, 'The value of $foo is: global';
is $t2, 'The value of $foo is: template';
is $t3, 'The value of $foo is: fillin';
Emptyclass1->always_prepend(q{$foo = 'Emptyclass global';});
$tmpl1 = Emptyclass1->new(
TYPE => 'STRING',
SOURCE => $tin);
$tmpl2 = Emptyclass1->new(
TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"});
$tmpl1->compile;
$tmpl2->compile;
$t1 = $tmpl1->fill_in(PACKAGE => 'T4');
$t2 = $tmpl2->fill_in(PACKAGE => 'T5');
$t3 = $tmpl2->fill_in(PREPEND => q{$foo = "fillin"}, PACKAGE => 'T6');
is $t1, 'The value of $foo is: Emptyclass global';
is $t2, 'The value of $foo is: template';
is $t3, 'The value of $foo is: fillin';
$tmpl1 = Emptyclass2->new(
TYPE => 'STRING',
SOURCE => $tin);
$tmpl2 = Emptyclass2->new(
TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"});
$tmpl1->compile;
$tmpl2->compile;
$t1 = $tmpl1->fill_in(PACKAGE => 'T4');
$t2 = $tmpl2->fill_in(PACKAGE => 'T5');
$t3 = $tmpl2->fill_in(PREPEND => q{$foo = "fillin"}, PACKAGE => 'T6');
is $t1, 'The value of $foo is: global';
is $t2, 'The value of $foo is: template';
is $t3, 'The value of $foo is: fillin';
#!perl
#
# Tests for PREPROCESSOR features
# These tests first appeared in version 1.25.
use strict;
use warnings;
use Test::More tests => 9;
use File::Temp;
use_ok 'Text::Template::Preprocess' or exit 1;
my $tmpfile = File::Temp->new;
my $TMPFILE = $tmpfile->filename;
my $py = sub { tr/x/y/ };
my $pz = sub { tr/x/z/ };
my $t = 'xxx The value of $x is {$x}';
my $outx = 'xxx The value of $x is 119';
my $outy = 'yyy The value of $y is 23';
my $outz = 'zzz The value of $z is 5';
open my $tfh, '>', $TMPFILE or die "Couldn't open test file: $!; aborting";
print $tfh $t;
close $tfh;
my @result = ($outx, $outy, $outz, $outz);
for my $trial (1, 0) {
for my $test (0 .. 3) {
my $tmpl;
if ($trial == 0) {
$tmpl = Text::Template::Preprocess->new(TYPE => 'STRING', SOURCE => $t) or die;
}
else {
open $tfh, '<', $TMPFILE or die "Couldn't open test file: $!; aborting";
$tmpl = Text::Template::Preprocess->new(TYPE => 'FILEHANDLE', SOURCE => $tfh) or die;
}
$tmpl->preprocessor($py) if ($test & 1) == 1;
my @args = ((($test & 2) == 2) ? (PREPROCESSOR => $pz) : ());
my $o = $tmpl->fill_in(@args, HASH => { x => 119, 'y' => 23, z => 5 });
is $o, $result[$test];
}
}
#!perl
#
# Test for RT Bug 29928 fix
# https://rt.cpan.org/Public/Bug/Display.html?id=29928
use strict;
use warnings;
use Test::More tests => 2;
use_ok 'Text::Template::Preprocess' or exit 1;
my $tin = q{The value of $foo is: {$foo}.};
sub tester {
1; # dummy preprocessor to cause the bug described.
}
my $tmpl1 = Text::Template::Preprocess->new(TYPE => 'STRING', SOURCE => $tin);
$tmpl1->compile;
my $t1 = $tmpl1->fill_in(
HASH => { foo => 'things' },
PREPROCESSOR => \&tester);
is $t1, 'The value of $foo is: things.';
......@@ -3,58 +3,48 @@
# test apparatus for Text::Template module
# still incomplete.
use Text::Template;
BEGIN {
eval "use Safe";
if ($@) {
print "1..0\n";
exit 0;
}
}
use strict;
use warnings;
use Test::More;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
unless (eval { require Safe; 1 }) {
plan skip_all => 'Safe.pm is required for this test';
}
else {
plan tests => 20;
}
print "1..16\n";
use_ok 'Text::Template' or exit 1;
my ($BADOP, $FAILURE);
if ($^O eq 'MacOS') {
$BADOP = qq{};
$FAILURE = q{};
} else {
$BADOP = qq{kill 0};
$FAILURE = q{Program fragment at line 1 delivered error ``kill trapped by operation mask''};
$BADOP = qq{};
$FAILURE = q{};
}
else {
$BADOP = qq{kill 0};
$FAILURE = q{Program fragment at line 1 delivered error ``kill trapped by operation mask''};
}
$n=1;
$v = $v = 119;
our $v = 119;
$c = new Safe or die;
my $c = Safe->new or die;
$goodtemplate = q{This should succeed: { $v }};
$goodoutput = q{This should succeed: 119};
my $goodtemplate = q{This should succeed: { $v }};
my $goodoutput = q{This should succeed: 119};
$template1 = new Text::Template ('type' => 'STRING', 'source' => $goodtemplate)
or die;
$template2 = new Text::Template ('type' => 'STRING', 'source' => $goodtemplate)
or die;
my $template1 = Text::Template->new(type => 'STRING', source => $goodtemplate);
my $template2 = Text::Template->new(type => 'STRING', source => $goodtemplate);
$text1 = $template1->fill_in();
$text2 = $template1->fill_in(SAFE => $c);
$ERR2 = $@;
$text3 = $template2->fill_in(SAFE => $c);
$ERR3 = $@;
my $text1 = $template1->fill_in();
ok defined $text1;
my $text2 = $template1->fill_in(SAFE => $c);
ok defined $text2;
# (1)(2)(3) None of these should have failed.
print +(defined $text1 ? '' : 'not '), "ok $n\n";
$n++;
print +(defined $text2 ? '' : 'not '), "ok $n\n";
$n++;
print +(defined $text3 ? '' : 'not '), "ok $n\n";
$n++;
my $text3 = $template2->fill_in(SAFE => $c);
ok defined $text3;
# (4) Safe and non-safe fills of different template objects with the
# same template text should yield the same result.
......@@ -64,98 +54,82 @@ $n++;
# We could alias the secret safe package to be identical to main,
# but that wouldn't be safe. If you want the aliasing, you have to
# request it explicitly with `PACKAGE'.
print "ok $n\n";
$n++;
# (5) Safe and non-safe fills of the same template object
# should yield the same result.
# (5) voided this test for the same reason as #4.
# print +($text1 eq $text2 ? '' : 'not '), "ok $n\n";
print "ok $n\n";
$n++;
# (6) Make sure the output was actually correct
print +($text1 eq $goodoutput ? '' : 'not '), "ok $n\n";
$n++;
is $text1, $goodoutput;
my $badtemplate = qq{This should fail: { $BADOP; 'NOFAIL' }};
my $badnosafeoutput = q{This should fail: NOFAIL};
my $badsafeoutput =
q{This should fail: Program fragment delivered error ``kill trapped by operation mask at template line 1.''};
$badtemplate = qq{This should fail: { $BADOP; 'NOFAIL' }};
$badnosafeoutput = q{This should fail: NOFAIL};
$badsafeoutput = q{This should fail: Program fragment delivered error ``kill trapped by operation mask at template line 1.''};
$template1 = Text::Template->new('type' => 'STRING', 'source' => $badtemplate);
isa_ok $template1, 'Text::Template';
$template1 = new Text::Template ('type' => 'STRING', 'source' => $badtemplate)
or die;
$template2 = new Text::Template ('type' => 'STRING', 'source' => $badtemplate)
or die;
$template2 = Text::Template->new('type' => 'STRING', 'source' => $badtemplate);
isa_ok $template2, 'Text::Template';
# none of these should fail
$text1 = $template1->fill_in();
ok defined $text1;
$text2 = $template1->fill_in(SAFE => $c);
$ERR2 = $@;
ok defined $text2;
$text3 = $template2->fill_in(SAFE => $c);
$ERR3 = $@;
$text4 = $template1->fill_in();
# (7)(8)(9)(10) None of these should have failed.
print +(defined $text1 ? '' : 'not '), "ok $n\n";
$n++;
print +(defined $text2 ? '' : 'not '), "ok $n\n";
$n++;
print +(defined $text3 ? '' : 'not '), "ok $n\n";
$n++;
print +(defined $text4 ? '' : 'not '), "ok $n\n";
$n++;
ok defined $text3;
my $text4 = $template1->fill_in();
ok defined $text4;
# (11) text1 and text4 should be the same (using safe in between
# didn't change anything.)
print +($text1 eq $text4 ? '' : 'not '), "ok $n\n";
$n++;
is $text1, $text4;
# (12) text2 and text3 should be the same (same template text in different
# objects
print +($text2 eq $text3 ? '' : 'not '), "ok $n\n";
$n++;
is $text2, $text3;
# (13) text1 should yield badnosafeoutput
print +($text1 eq $badnosafeoutput ? '' : 'not '), "ok $n\n";
$n++;
is $text1, $badnosafeoutput;
# (14) text2 should yield badsafeoutput
$text2 =~ s/'kill'/kill/; # 5.8.1 added quote marks around the op name
print "# expected: <$badsafeoutput>\n# got : <$text2>\n";
print +($text2 eq $badsafeoutput ? '' : 'not '), "ok $n\n";
$n++;
$text2 =~ s/'kill'/kill/; # 5.8.1 added quote marks around the op name
is $text2, $badsafeoutput;
my $template = q{{$x=1}{$x+1}};
$template = q{{$x=1}{$x+1}};
$template1 = Text::Template->new('type' => 'STRING', 'source' => $template);
isa_ok $template1, 'Text::Template';
$template1 = new Text::Template ('type' => 'STRING', 'source' => $template)
or die;
$template2 = new Text::Template ('type' => 'STRING', 'source' => $template)
or die;
$template2 = Text::Template->new('type' => 'STRING', 'source' => $template);
isa_ok $template2, 'Text::Template';
$text1 = $template1->fill_in();
$text2 = $template1->fill_in(SAFE => new Safe);
$text2 = $template1->fill_in(SAFE => Safe->new);
# (15) Do effects persist in safe compartments?
print +($text1 eq $text2 ? '' : 'not '), "ok $n\n";
$n++;
is $text1, $text2;
# (16) Try the BROKEN routine in safe compartments
sub my_broken {
my %a = @_; $a{error} =~ s/ at.*//s;
"OK! text:$a{text} error:$a{error} lineno:$a{lineno} arg:$a{arg}" ;
sub my_broken {
my %a = @_;
$a{error} =~ s/ at.*//s;
"OK! text:$a{text} error:$a{error} lineno:$a{lineno} arg:$a{arg}";
}
$templateB = new Text::Template (TYPE => 'STRING', SOURCE => '{die}')
or die;
$text1 = $templateB->fill_in(BROKEN => \&my_broken,
BROKEN_ARG => 'barg',
SAFE => new Safe,
);
$result1 = qq{OK! text:die error:Died lineno:1 arg:barg};
print +($text1 eq $result1 ? '' : 'not '), "ok $n\n";
$n++;
my $templateB = Text::Template->new(TYPE => 'STRING', SOURCE => '{die}');
isa_ok $templateB, 'Text::Template';
exit;
$text1 = $templateB->fill_in(
BROKEN => \&my_broken,
BROKEN_ARG => 'barg',
SAFE => Safe->new);
my $result1 = qq{OK! text:die error:Died lineno:1 arg:barg};
is $text1, $result1;
......@@ -3,103 +3,92 @@
# test apparatus for Text::Template module
# still incomplete.
use Text::Template;
BEGIN {
eval "use Safe";
if ($@) {
print "1..0\n";
exit 0;
}
}
use strict;
use warnings;
use Test::More;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
unless (eval { require Safe; 1 }) {
plan skip_all => 'Safe.pm is required for this test';
}
else {
plan tests => 12;
}
print "1..12\n";
$n = 1;
use_ok 'Text::Template' or exit 1;
$c = new Safe or die;
my $c = Safe->new or die;
# Test handling of packages and importing.
$c->reval('$P = "safe root"');
$P = $P = 'main';
our $P = 'main';
$Q::P = $Q::P = 'Q';
# How to effectively test the gensymming?
$t = new Text::Template TYPE => 'STRING', SOURCE => 'package is {$P}'
or die;
my $t = Text::Template->new(
TYPE => 'STRING',
SOURCE => 'package is {$P}') or die;
# (1) Default behavior: Inherit from calling package, `main' in this case.
$text = $t->fill_in();
print +($text eq 'package is main' ? '' : 'not '), "ok $n\n";
$n++;
my $text = $t->fill_in();
is $text, 'package is main';
# (2) When a package is specified, we should use that package instead.
$text = $t->fill_in(PACKAGE => 'Q');
print +($text eq 'package is Q' ? '' : 'not '), "ok $n\n";
$n++;
is $text, 'package is Q';
# (3) When no package is specified in safe mode, we should use the
# default safe root.
$text = $t->fill_in(SAFE => $c);
print +($text eq 'package is safe root' ? '' : 'not '), "ok $n\n";
$n++;
is $text, 'package is safe root';
# (4) When a package is specified in safe mode, we should use the
# default safe root, after aliasing to the specified package
$text = $t->fill_in(SAFE => $c, PACKAGE => Q);
print +($text eq 'package is Q' ? '' : 'not '), "ok $n\n";
$n++;
TODO: {
local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
$text = $t->fill_in(SAFE => $c, PACKAGE => 'Q');
is $text, 'package is Q';
}
# Now let's see if hash vars are installed properly into safe templates
$t = new Text::Template TYPE => 'STRING', SOURCE => 'hash is {$H}'
or die;
$t = Text::Template->new(
TYPE => 'STRING',
SOURCE => 'hash is {$H}') or die;
# (5) First in default mode
$text = $t->fill_in(HASH => {H => 'good5'} );
print +($text eq 'hash is good5' ? '' : 'not '), "ok $n\n";
$n++;
$text = $t->fill_in(HASH => { H => 'good5' });
is $text, 'hash is good5';
# suppress "once" warnings
$Q::H = $Q2::H = undef;
# (6) Now in packages
$text = $t->fill_in(HASH => {H => 'good6'}, PACKAGE => 'Q' );
print +($text eq 'hash is good6' ? '' : 'not '), "ok $n\n";
$n++;
$text = $t->fill_in(HASH => { H => 'good6' }, PACKAGE => 'Q');
is $text, 'hash is good6';
# (7) Now in the default root of the safe compartment
$text = $t->fill_in(HASH => {H => 'good7'}, SAFE => $c );
print +($text eq 'hash is good7' ? '' : 'not '), "ok $n\n";
$n++;
TODO: {
local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
$text = $t->fill_in(HASH => { H => 'good7' }, SAFE => $c);
is $text, 'hash is good7';
}
# (8) Now in the default root after aliasing to a package that
# got the hash stuffed in
$text = $t->fill_in(HASH => {H => 'good8'}, SAFE => $c, PACKAGE => 'Q2' );
print +($text eq 'hash is good8' ? '' : 'not '), "ok $n\n";
$n++;
our $H;
TODO: {
local $TODO = "test fails when tested with TAP/Devel::Cover" if defined $Devel::Cover::VERSION;
$text = $t->fill_in(HASH => { H => 'good8' }, SAFE => $c, PACKAGE => 'Q2');
is $text, 'hash is good8';
}
# Now let's make sure that none of the packages leaked on each other.
# (9) This var should NOT have been installed into the main package
print +(defined $H ? 'not ' : ''), "ok $n\n";
$H=$H;
$n++;
# (10) good6 was overwritten in test 7, so there's nothing to test for here.
print "ok $n\n";
$n++;
ok !defined $H;
$H = $H;
# (11) this value overwrote the one from test 6.
print +($Q::H eq 'good7' ? '' : 'not '), "ok $n\n";
$Q::H = $Q::H;
$n++;
# (12)
print +($Q2::H eq 'good8' ? '' : 'not '), "ok $n\n";
$Q2::H = $Q2::H;
$n++;
is $Q::H, 'good7';
# (12)
is $Q2::H, 'good8';
......@@ -2,61 +2,56 @@
#
# test apparatus for Text::Template module
use Text::Template;
BEGIN {
eval "use Safe";
if ($@) {
print "1..0\n";
exit 0;
}
}
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
use strict;
use warnings;
use Test::More;
print "1..3\n";
unless (eval { require Safe; 1 }) {
plan skip_all => 'Safe.pm is required for this test';
}
else {
plan tests => 4;
}
$n=1;
use_ok 'Text::Template' or exit 1;
# Test the OUT feature with safe compartments
$template = q{
my $template = q{
This line should have a 3: {1+2}
This line should have several numbers:
{ $t = ''; foreach $n (1 .. 20) { $t .= $n . ' ' } $t }
};
$templateOUT = q{
my $templateOUT = q{
This line should have a 3: { $OUT = 1+2 }
This line should have several numbers:
{ foreach $n (1 .. 20) { $OUT .= $n . ' ' } }
};
$c = new Safe;
my $c = Safe->new;
# Build templates from string
$template = new Text::Template ('type' => 'STRING', 'source' => $template,
SAFE => $c)
or die;
$templateOUT = new Text::Template ('type' => 'STRING', 'source' => $templateOUT,
SAFE => $c)
or die;
$template = Text::Template->new(
type => 'STRING',
source => $template,
SAFE => $c) or die;
$templateOUT = Text::Template->new(
type => 'STRING',
source => $templateOUT,
SAFE => $c) or die;
# Fill in templates
$text = $template->fill_in()
or die;
$textOUT = $templateOUT->fill_in()
or die;
my $text = $template->fill_in()
or die;
my $textOUT = $templateOUT->fill_in()
or die;
# (1) They should be the same
print +($text eq $textOUT ? '' : 'not '), "ok $n\n";
$n++;
is $text, $textOUT;
# (2-3) "Joel Appelbaum" <joel@orbz.com> <000701c0ac2c$aed1d6e0$0201a8c0@prime>
# "Contrary to the documentation the $OUT variable is not always
......@@ -70,22 +65,16 @@ $n++;
# Also reported by Daini Xie.
{
my $template = q{{$OUT = 'x'}y{$OUT .= 'z'}};
my $expected = "xyz";
my $s = Safe->new;
my $o = Text::Template->new(type => 'string',
source => $template,
);
for (1..2) {
my $r = $o->fill_in(SAFE => $s);
if ($r ne $expected) {
print "not ok $n # <$r>\n";
} else {
print "ok $n\n";
}
$n++;
}
}
my $template = q{{$OUT = 'x'}y{$OUT .= 'z'}};
my $expected = "xyz";
my $s = Safe->new;
my $o = Text::Template->new(
type => 'string',
source => $template);
exit;
for (1 .. 2) {
my $r = $o->fill_in(SAFE => $s);
is $r, $expected;
}
}
#!perl
#
# Tests for STRICT features
# These tests first appeared in version 1.48.
use strict;
use warnings;
use Test::More tests => 4;
use_ok 'Text::Template' or exit 1;
@Emptyclass1::ISA = 'Text::Template';
@Emptyclass2::ISA = 'Text::Template';
my $tin = q{The value of $foo is: {$foo}};
Text::Template->always_prepend(q{$foo = "global"});
my $tmpl1 = Text::Template->new(
TYPE => 'STRING',
SOURCE => $tin);
my $tmpl2 = Text::Template->new(
TYPE => 'STRING',
SOURCE => $tin,
PREPEND => q{$foo = "template"});
$tmpl1->compile;
$tmpl2->compile;
# strict should cause t1 to contain an error message if wrong variable is used in template
my $t1 = $tmpl1->fill_in(PACKAGE => 'T1', STRICT => 1, HASH => { bar => 'baz' });
# non-strict still works
my $t2 = $tmpl2->fill_in(PACKAGE => 'T2', HASH => { bar => 'baz' });
# prepend overrides the hash values
my $t3 = $tmpl2->fill_in(
PREPEND => q{$foo = "fillin"},
PACKAGE => 'T3',
STRICT => 1,
HASH => { foo => 'hashval2' });
like $t1, qr/Global symbol "\$foo" requires explicit package/;
is $t2, 'The value of $foo is: template', "non-strict hash still works";
is $t3, "The value of \$foo is: fillin", "hash values with prepend, prepend wins, even under strict.";
#!perl -T
# Tests for taint-mode features
use strict;
use warnings;
use lib 'blib/lib';
use Text::Template;
use Test::More tests => 21;
use File::Temp;
die "This is the test program for Text::Template version 1.46.
You are using version $Text::Template::VERSION instead.
That does not make sense.\n
Aborting"
unless $Text::Template::VERSION == 1.46;
use_ok 'Text::Template' or exit 1;
my $r = int(rand(10000));
my $file = "tt$r";
if ($^O eq 'MSWin32') {
# File::Temp (for all versions up to at least 0.2308) is currently bugged under MSWin32/taint mode [as of 2018-09]
# ... fails unless "/tmp" on the current windows drive is a writable directory OR either $ENV{TMP} or $ENV{TEMP} are untainted and point to a writable directory
# ref: [File-Temp: Fails under -T, Windows 7, Strawberry Perl 5.12.1](https://rt.cpan.org/Public/Bug/Display.html?id=60340)
($ENV{TEMP}) = $ENV{TEMP} =~ m/^.*$/gmsx; # untaint $ENV{TEMP}
($ENV{TMP}) = $ENV{TMP} =~ m/^.*$/gmsx; # untaint $ENV{TMP}
}
my $tmpfile = File::Temp->new;
my $file = $tmpfile->filename;
# makes its arguments tainted
sub taint {
for (@_) {
$_ .= substr($0,0,0); # LOD
}
for (@_) {
$_ .= substr($0, 0, 0); # LOD
}
}
print "1..21\n";
my $n =1;
print "ok ", $n++, "\n";
my $template = 'The value of $n is {$n}.';
open T, "> $file" or die "Couldn't write temporary file $file: $!";
print T $template, "\n";
close T or die "Couldn't finish temporary file $file: $!";
open my $fh, '>', $file or die "Couldn't write temporary file $file: $!";
print $fh $template, "\n";
close $fh or die "Couldn't finish temporary file $file: $!";
sub should_fail {
my $obj = Text::Template->new(@_);
eval {$obj->fill_in()};
if ($@) {
print "ok $n # $@\n";
} else {
print "not ok $n # (didn't fail)\n";
}
$n++;
my $obj = Text::Template->new(@_);
eval { $obj->fill_in() };
if ($@) {
pass $@;
}
else {
fail q[didn't fail];
}
}
sub should_work {
my $obj = Text::Template->new(@_);
eval {$obj->fill_in()};
if ($@) {
print "not ok $n # $@\n";
} else {
print "ok $n\n";
}
$n++;
my $obj = Text::Template->new(@_);
eval { $obj->fill_in() };
if ($@) {
fail $@;
}
else {
pass;
}
}
sub should_be_tainted {
if (Text::Template::_is_clean($_[0])) {
print "not ok $n\n"; $n++; return;
}
print "ok $n\n"; $n++; return;
ok !Text::Template::_is_clean($_[0]);
}
sub should_be_clean {
unless (Text::Template::_is_clean($_[0])) {
print "not ok $n\n"; $n++; return;
}
print "ok $n\n"; $n++; return;
ok Text::Template::_is_clean($_[0]);
}
# Tainted filename should die with and without UNTAINT option
......@@ -84,12 +79,13 @@ should_fail TYPE => 'file', SOURCE => $file;
should_work TYPE => 'file', SOURCE => $file, UNTAINT => 1;
# (8-9)
open H, "< $file" or die "Couldn't open $file for reading: $!; aborting";
should_fail TYPE => 'filehandle', SOURCE => \*H;
close H;
open H, "< $file" or die "Couldn't open $file for reading: $!; aborting";
should_work TYPE => 'filehandle', SOURCE => \*H, UNTAINT => 1;
close H;
open $fh, '<', $file or die "Couldn't open $file for reading: $!; aborting";
should_fail TYPE => 'filehandle', SOURCE => $fh;
close $fh;
open $fh, '<', $file or die "Couldn't open $file for reading: $!; aborting";
should_work TYPE => 'filehandle', SOURCE => $fh, UNTAINT => 1;
close $fh;
# (10-15)
my $ttemplate = $template;
......@@ -102,8 +98,8 @@ should_work TYPE => 'string', SOURCE => $template;
should_work TYPE => 'string', SOURCE => $template, UNTAINT => 1;
# (16-19)
my $array = [ $template ];
my $tarray = [ $ttemplate ];
my $array = [$template];
my $tarray = [$ttemplate];
should_fail TYPE => 'array', SOURCE => $tarray;
should_fail TYPE => 'array', SOURCE => $tarray, UNTAINT => 1;
should_work TYPE => 'array', SOURCE => $array;
......@@ -114,6 +110,3 @@ Text::Template::_unconditionally_untaint($ttemplate);
should_be_clean($ttemplate);
Text::Template::_unconditionally_untaint($tfile);
should_be_clean($tfile);
END { unlink $file }
#!perl
use utf8;
use strict;
use warnings;
use Test::More;
use Encode;
use File::Temp;
# Non-CORE module(s)
unless (eval { require Test::More::UTF8; 1; } ) {
plan skip_all => '[ Test::More::UTF8 ] is required for testing';
}
plan tests => 3;
use_ok 'Text::Template' or exit 1;
my $tmp_fh = File::Temp->new;
print $tmp_fh encode('UTF-8', "\x{4f60}\x{597d} {{\$name}}");
$tmp_fh->flush;
# UTF-8 encoded template file
my $str = Text::Template->new(
TYPE => 'FILE',
SOURCE => $tmp_fh->filename,
ENCODING => 'UTF-8'
)->fill_in(HASH => { name => 'World' });
is $str, "\x{4f60}\x{597d} World";
$tmp_fh = File::Temp->new;
print $tmp_fh encode('iso-8859-1', "Ol\x{e1} {{\$name}}");
$tmp_fh->flush;
# ISO-8859-1 encoded template file
$str = Text::Template->new(
TYPE => 'FILE',
SOURCE => $tmp_fh->filename,
ENCODING => 'iso-8859-1'
)->fill_in(HASH => { name => 'World' });
is $str, "Ol\x{e1} World";
#!perl
use strict;
use warnings;
use Text::Template;
# Minimum Test::More version; 0.94+ is required for `done_testing`
BEGIN {
unless (eval { require Test::More; "$Test::More::VERSION" >= 0.94; }) {
Test::More::plan(skip_all => '[ Test::More v0.94+ ] is required for testing');
}
Test::More->import;
# Non-CORE module(s)
unless (eval { require Test::Warnings; 1; }) {
plan(skip_all => '[ Test::Warnings ] is required for testing');
}
Test::Warnings->import;
}
my $template = <<'EOT';
{{
if ($good =~ /good/) {
'This template should not produce warnings.'.$bad;
}
}}
EOT
$template = Text::Template->new(type => 'STRING', source => $template);
isa_ok $template, 'Text::Template';
my $result = $template->fill_in(HASH => { good => 'good' });
$result =~ s/(?:^\s+)|(?:\s+$)//gs;
is $result, 'This template should not produce warnings.';
# see https://github.com/mschout/perl-text-template/issues/10
$template = Text::Template->new(type => 'STRING', package => 'MY', source => '');
$template->fill_in(package => 'MY', hash => { include => sub { 'XX' } });
$template = Text::Template->new(type => 'STRING', package => 'MY', source => '');
$template->fill_in(package => 'MY', hash => { include => sub { 'XX' } });
done_testing;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册