07-safe3.t 2.1 KB

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