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