. Then in the main program, write *Q::include = \&Text::Template::_load_text; This imports the C<_load_text> function into package C with the name C. From then on, any template that you fill in with package C can say {include(filename)} to insert the text from the named file at that point. If you are using the C option instead, just put C \&Text::Template::_load_text> into the hash instead of importing it explicitly. Suppose you don't want to insert a plain text file, but rather you want to include one template within another? Just use C in the template itself: {Text::Template::fill_in_file(filename)} You can do the same importing trick if this is too much to type. =head1 Miscellaneous =head2 C variables People are frequently surprised when this doesn't work: my $recipient = 'The King'; my $text = fill_in_file('formletter.tmpl'); The text C doesn't get into the form letter. Why not? Because C<$recipient> is a C variable, and the whole point of C variables is that they're private and inaccessible except in the scope in which they're declared. The template is not part of that scope, so the template can't see C<$recipient>. If that's not the behavior you want, don't use C. C means a private variable, and in this case you don't want the variable to be private. Put the variables into package variables in some other package, and use the C option to C: $Q::recipient = $recipient; my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q'); or pass the names and values in a hash with the C option: my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient }); =head2 Security Matters All variables are evaluated in the package you specify with the C option of C. if you use this option, and if your templates don't do anything egregiously stupid, you won't have to worry that evaluation of the little programs will creep out into the rest of your program and wreck something. Nevertheless, there's really no way (except with C) to protect against a template that says { $Important::Secret::Security::Enable = 0; # Disable security checks in this program } or { $/ = "ho ho ho"; # Sabotage future uses of . # $/ is always a global variable } or even { system("rm -rf /") } so B go filling in templates unless you're sure you know what's in them. If you're worried, or you can't trust the person who wrote the template, use the C option. A final warning: program fragments run a small risk of accidentally clobbering local variables in the C function itself. These variables all have names that begin with C<$fi_>, so if you stay away from those names you'll be safe. (Of course, if you're a real wizard you can tamper with them deliberately for exciting effects; this is actually how C<$OUT> works.) I can fix this, but it will make the package slower to do it, so I would prefer not to. If you are worried about this, send me mail and I will show you what to do about it. =head2 Alternative Delimiters Lorenzo Valdettaro pointed out that if you are using C to generate TeX output, the choice of braces as the program fragment delimiters makes you suffer suffer suffer. Starting in version 1.20, you can change the choice of delimiters to something other than curly braces. In either the C call or the C call, you can specify an alternative set of delimiters with the C option. For example, if you would like code fragments to be delimited by C<[@--> and C<--@]> instead of C<{> and C<}>, use ... DELIMITERS => [ '[@--', '--@]' ], ... Note that these delimiters are I, not regexes. (I tried for regexes, but it complicates the lexical analysis too much.) Note also that C disables the special meaning of the backslash, so if you want to include the delimiters in the literal text of your template file, you are out of luck---it is up to you to choose delimiters that do not conflict with what you are doing. The delimiter strings may still appear inside of program fragments as long as they nest properly. This means that if for some reason you absolutely must have a program fragment that mentions one of the delimiters, like this: [@-- print "Oh no, a delimiter: --@]\n" --@] you may be able to make it work by doing this instead: [@-- # Fake matching delimiter in a comment: [@-- print "Oh no, a delimiter: --@]\n" --@] It may be safer to choose delimiters that begin with a newline character. Because the parsing of templates is simplified by the absence of backslash escapes, using alternative C may speed up the parsing process by 20-25%. This shows that my original choice of C<{> and C<}> was very bad. =head2 C feature and using C in templates Suppose you would like to use C in your templates to detect undeclared variables and the like. But each code fragment is a separate lexical scope, so you have to turn on C at the top of each and every code fragment: { use strict; use vars '$foo'; $foo = 14; ... } ... { # we forgot to put `use strict' here my $result = $boo + 12; # $boo is misspelled and should be $foo # No error is raised on `$boo' } Because we didn't put C at the top of the second fragment, it was only active in the first fragment, and we didn't get any C checking in the second fragment. Then we misspelled C<$foo> and the error wasn't caught. C version 1.22 and higher has a new feature to make this easier. You can specify that any text at all be automatically added to the beginning of each program fragment. When you make a call to C, you can specify a PREPEND => 'some perl statements here' option; the statements will be prepended to each program fragment for that one call only. Suppose that the C call included a PREPEND => 'use strict;' option, and that the template looked like this: { use vars '$foo'; $foo = 14; ... } ... { my $result = $boo + 12; # $boo is misspelled and should be $foo ... } The code in the second fragment would fail, because C<$boo> has not been declared. C was implied, even though you did not write it explicitly, because the C option added it for you automatically. There are three other ways to do this. At the time you create the template object with C, you can also supply a C option, in which case the statements will be prepended each time you fill in that template. If the C call has its own C option, this overrides the one specified at the time you created the template. Finally, you can make the class method call Text::Template->always_prepend('perl statements'); If you do this, then call calls to C for I template will attach the perl statements to the beginning of each program fragment, except where overridden by C options to C or C. An alternative to adding "use strict;" to the PREPEND option, you can pass STRICT => 1 to fill_in when also passing the HASH option. Suppose that the C call included both HASH => {$foo => ''} and STRICT => 1 options, and that the template looked like this: { $foo = 14; ... } ... { my $result = $boo + 12; # $boo is misspelled and should be $foo ... } The code in the second fragment would fail, because C<$boo> has not been declared. C was implied, even though you did not write it explicitly, because the C option added it for you automatically. Any variable referenced in the template that is not in the C
with the name C. From then on, any template that you fill in with package C can say {include(filename)} to insert the text from the named file at that point. If you are using the C option instead, just put C \&Text::Template::_load_text> into the hash instead of importing it explicitly. Suppose you don't want to insert a plain text file, but rather you want to include one template within another? Just use C in the template itself: {Text::Template::fill_in_file(filename)} You can do the same importing trick if this is too much to type. =head1 Miscellaneous =head2 C variables People are frequently surprised when this doesn't work: my $recipient = 'The King'; my $text = fill_in_file('formletter.tmpl'); The text C doesn't get into the form letter. Why not? Because C<$recipient> is a C variable, and the whole point of C variables is that they're private and inaccessible except in the scope in which they're declared. The template is not part of that scope, so the template can't see C<$recipient>. If that's not the behavior you want, don't use C. C means a private variable, and in this case you don't want the variable to be private. Put the variables into package variables in some other package, and use the C option to C: $Q::recipient = $recipient; my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q'); or pass the names and values in a hash with the C option: my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient }); =head2 Security Matters All variables are evaluated in the package you specify with the C option of C. if you use this option, and if your templates don't do anything egregiously stupid, you won't have to worry that evaluation of the little programs will creep out into the rest of your program and wreck something. Nevertheless, there's really no way (except with C) to protect against a template that says { $Important::Secret::Security::Enable = 0; # Disable security checks in this program } or { $/ = "ho ho ho"; # Sabotage future uses of . # $/ is always a global variable } or even { system("rm -rf /") } so B go filling in templates unless you're sure you know what's in them. If you're worried, or you can't trust the person who wrote the template, use the C option. A final warning: program fragments run a small risk of accidentally clobbering local variables in the C function itself. These variables all have names that begin with C<$fi_>, so if you stay away from those names you'll be safe. (Of course, if you're a real wizard you can tamper with them deliberately for exciting effects; this is actually how C<$OUT> works.) I can fix this, but it will make the package slower to do it, so I would prefer not to. If you are worried about this, send me mail and I will show you what to do about it. =head2 Alternative Delimiters Lorenzo Valdettaro pointed out that if you are using C to generate TeX output, the choice of braces as the program fragment delimiters makes you suffer suffer suffer. Starting in version 1.20, you can change the choice of delimiters to something other than curly braces. In either the C call or the C call, you can specify an alternative set of delimiters with the C option. For example, if you would like code fragments to be delimited by C<[@--> and C<--@]> instead of C<{> and C<}>, use ... DELIMITERS => [ '[@--', '--@]' ], ... Note that these delimiters are I, not regexes. (I tried for regexes, but it complicates the lexical analysis too much.) Note also that C disables the special meaning of the backslash, so if you want to include the delimiters in the literal text of your template file, you are out of luck---it is up to you to choose delimiters that do not conflict with what you are doing. The delimiter strings may still appear inside of program fragments as long as they nest properly. This means that if for some reason you absolutely must have a program fragment that mentions one of the delimiters, like this: [@-- print "Oh no, a delimiter: --@]\n" --@] you may be able to make it work by doing this instead: [@-- # Fake matching delimiter in a comment: [@-- print "Oh no, a delimiter: --@]\n" --@] It may be safer to choose delimiters that begin with a newline character. Because the parsing of templates is simplified by the absence of backslash escapes, using alternative C may speed up the parsing process by 20-25%. This shows that my original choice of C<{> and C<}> was very bad. =head2 C feature and using C in templates Suppose you would like to use C in your templates to detect undeclared variables and the like. But each code fragment is a separate lexical scope, so you have to turn on C at the top of each and every code fragment: { use strict; use vars '$foo'; $foo = 14; ... } ... { # we forgot to put `use strict' here my $result = $boo + 12; # $boo is misspelled and should be $foo # No error is raised on `$boo' } Because we didn't put C at the top of the second fragment, it was only active in the first fragment, and we didn't get any C checking in the second fragment. Then we misspelled C<$foo> and the error wasn't caught. C version 1.22 and higher has a new feature to make this easier. You can specify that any text at all be automatically added to the beginning of each program fragment. When you make a call to C, you can specify a PREPEND => 'some perl statements here' option; the statements will be prepended to each program fragment for that one call only. Suppose that the C call included a PREPEND => 'use strict;' option, and that the template looked like this: { use vars '$foo'; $foo = 14; ... } ... { my $result = $boo + 12; # $boo is misspelled and should be $foo ... } The code in the second fragment would fail, because C<$boo> has not been declared. C was implied, even though you did not write it explicitly, because the C option added it for you automatically. There are three other ways to do this. At the time you create the template object with C, you can also supply a C option, in which case the statements will be prepended each time you fill in that template. If the C call has its own C option, this overrides the one specified at the time you created the template. Finally, you can make the class method call Text::Template->always_prepend('perl statements'); If you do this, then call calls to C for I template will attach the perl statements to the beginning of each program fragment, except where overridden by C options to C or C. An alternative to adding "use strict;" to the PREPEND option, you can pass STRICT => 1 to fill_in when also passing the HASH option. Suppose that the C call included both HASH => {$foo => ''} and STRICT => 1 options, and that the template looked like this: { $foo = 14; ... } ... { my $result = $boo + 12; # $boo is misspelled and should be $foo ... } The code in the second fragment would fail, because C<$boo> has not been declared. C was implied, even though you did not write it explicitly, because the C option added it for you automatically. Any variable referenced in the template that is not in the C
can say {include(filename)} to insert the text from the named file at that point. If you are using the C option instead, just put C \&Text::Template::_load_text> into the hash instead of importing it explicitly. Suppose you don't want to insert a plain text file, but rather you want to include one template within another? Just use C in the template itself: {Text::Template::fill_in_file(filename)} You can do the same importing trick if this is too much to type. =head1 Miscellaneous =head2 C variables People are frequently surprised when this doesn't work: my $recipient = 'The King'; my $text = fill_in_file('formletter.tmpl'); The text C doesn't get into the form letter. Why not? Because C<$recipient> is a C variable, and the whole point of C variables is that they're private and inaccessible except in the scope in which they're declared. The template is not part of that scope, so the template can't see C<$recipient>. If that's not the behavior you want, don't use C. C means a private variable, and in this case you don't want the variable to be private. Put the variables into package variables in some other package, and use the C option to C: $Q::recipient = $recipient; my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q'); or pass the names and values in a hash with the C option: my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient }); =head2 Security Matters All variables are evaluated in the package you specify with the C option of C. if you use this option, and if your templates don't do anything egregiously stupid, you won't have to worry that evaluation of the little programs will creep out into the rest of your program and wreck something. Nevertheless, there's really no way (except with C) to protect against a template that says { $Important::Secret::Security::Enable = 0; # Disable security checks in this program } or { $/ = "ho ho ho"; # Sabotage future uses of . # $/ is always a global variable } or even { system("rm -rf /") } so B go filling in templates unless you're sure you know what's in them. If you're worried, or you can't trust the person who wrote the template, use the C option. A final warning: program fragments run a small risk of accidentally clobbering local variables in the C function itself. These variables all have names that begin with C<$fi_>, so if you stay away from those names you'll be safe. (Of course, if you're a real wizard you can tamper with them deliberately for exciting effects; this is actually how C<$OUT> works.) I can fix this, but it will make the package slower to do it, so I would prefer not to. If you are worried about this, send me mail and I will show you what to do about it. =head2 Alternative Delimiters Lorenzo Valdettaro pointed out that if you are using C to generate TeX output, the choice of braces as the program fragment delimiters makes you suffer suffer suffer. Starting in version 1.20, you can change the choice of delimiters to something other than curly braces. In either the C call or the C call, you can specify an alternative set of delimiters with the C option. For example, if you would like code fragments to be delimited by C<[@--> and C<--@]> instead of C<{> and C<}>, use ... DELIMITERS => [ '[@--', '--@]' ], ... Note that these delimiters are I, not regexes. (I tried for regexes, but it complicates the lexical analysis too much.) Note also that C disables the special meaning of the backslash, so if you want to include the delimiters in the literal text of your template file, you are out of luck---it is up to you to choose delimiters that do not conflict with what you are doing. The delimiter strings may still appear inside of program fragments as long as they nest properly. This means that if for some reason you absolutely must have a program fragment that mentions one of the delimiters, like this: [@-- print "Oh no, a delimiter: --@]\n" --@] you may be able to make it work by doing this instead: [@-- # Fake matching delimiter in a comment: [@-- print "Oh no, a delimiter: --@]\n" --@] It may be safer to choose delimiters that begin with a newline character. Because the parsing of templates is simplified by the absence of backslash escapes, using alternative C may speed up the parsing process by 20-25%. This shows that my original choice of C<{> and C<}> was very bad. =head2 C feature and using C in templates Suppose you would like to use C in your templates to detect undeclared variables and the like. But each code fragment is a separate lexical scope, so you have to turn on C at the top of each and every code fragment: { use strict; use vars '$foo'; $foo = 14; ... } ... { # we forgot to put `use strict' here my $result = $boo + 12; # $boo is misspelled and should be $foo # No error is raised on `$boo' } Because we didn't put C at the top of the second fragment, it was only active in the first fragment, and we didn't get any C checking in the second fragment. Then we misspelled C<$foo> and the error wasn't caught. C version 1.22 and higher has a new feature to make this easier. You can specify that any text at all be automatically added to the beginning of each program fragment. When you make a call to C, you can specify a PREPEND => 'some perl statements here' option; the statements will be prepended to each program fragment for that one call only. Suppose that the C call included a PREPEND => 'use strict;' option, and that the template looked like this: { use vars '$foo'; $foo = 14; ... } ... { my $result = $boo + 12; # $boo is misspelled and should be $foo ... } The code in the second fragment would fail, because C<$boo> has not been declared. C was implied, even though you did not write it explicitly, because the C option added it for you automatically. There are three other ways to do this. At the time you create the template object with C, you can also supply a C option, in which case the statements will be prepended each time you fill in that template. If the C call has its own C option, this overrides the one specified at the time you created the template. Finally, you can make the class method call Text::Template->always_prepend('perl statements'); If you do this, then call calls to C for I template will attach the perl statements to the beginning of each program fragment, except where overridden by C options to C or C. An alternative to adding "use strict;" to the PREPEND option, you can pass STRICT => 1 to fill_in when also passing the HASH option. Suppose that the C call included both HASH => {$foo => ''} and STRICT => 1 options, and that the template looked like this: { $foo = 14; ... } ... { my $result = $boo + 12; # $boo is misspelled and should be $foo ... } The code in the second fragment would fail, because C<$boo> has not been declared. C was implied, even though you did not write it explicitly, because the C option added it for you automatically. Any variable referenced in the template that is not in the C