1#!perl
2#
3# test apparatus for Text::Template module
4
5use strict;
6use warnings;
7use Test::More;
8
9unless (eval { require Safe; 1 }) {
10    plan skip_all => 'Safe.pm is required for this test';
11}
12else {
13    plan tests => 4;
14}
15
16use_ok 'Text::Template' or exit 1;
17
18# Test the OUT feature with safe compartments
19
20my $template = q{
21This line should have a 3: {1+2}
22
23This line should have several numbers:
24{ $t = ''; foreach $n (1 .. 20) { $t .= $n . ' ' } $t }
25};
26
27my $templateOUT = q{
28This line should have a 3: { $OUT = 1+2 }
29
30This line should have several numbers:
31{ foreach $n (1 .. 20) { $OUT .= $n . ' ' } }
32};
33
34my $c = Safe->new;
35
36# Build templates from string
37$template = Text::Template->new(
38    type   => 'STRING',
39    source => $template,
40    SAFE   => $c) or die;
41
42$templateOUT = Text::Template->new(
43    type   => 'STRING',
44    source => $templateOUT,
45    SAFE    => $c) or die;
46
47# Fill in templates
48my $text = $template->fill_in()
49    or die;
50my $textOUT = $templateOUT->fill_in()
51    or die;
52
53# (1) They should be the same
54is $text, $textOUT;
55
56# (2-3)  "Joel Appelbaum" <joel@orbz.com> <000701c0ac2c$aed1d6e0$0201a8c0@prime>
57# "Contrary to the documentation the $OUT variable is not always
58# undefined at the start of each program fragment.  The $OUT variable
59# is never undefined after it is used once if you are using the SAFE
60# option.  The result is that every fragment after the fragment that
61# $OUT was used in is replaced by the old $OUT value instead of the
62# result of the fragment.  This holds true even after the
63# Text::Template object goes out of scope and a new one is created!"
64#
65# Also reported by Daini Xie.
66
67{
68    my $template = q{{$OUT = 'x'}y{$OUT .= 'z'}};
69    my $expected = "xyz";
70    my $s        = Safe->new;
71    my $o        = Text::Template->new(
72        type   => 'string',
73        source => $template);
74
75    for (1 .. 2) {
76        my $r = $o->fill_in(SAFE => $s);
77
78        is $r, $expected;
79    }
80}
81