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