safe3.t 1.9 KB

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