Changes

From SME Server
Jump to navigationJump to search
2,119 bytes removed ,  11:18, 20 December 2013
no edit summary
Line 1: Line 1: −
   
+
  {{level|developer}}
NAME
+
== NAME ==
      Text::Template - Expand template text with embedded Perl
+
Text::Template - Expand template text with embedded Perl
   −
VERSION
+
== VERSION ==
      This file documents "Text::Template" version 1.45
     −
SYNOPSIS
+
This file documents "Text::Template" version 1.45
 +
 
 +
== SYNOPSIS ==
 
         use Text::Template;
 
         use Text::Template;
   Line 45: Line 46:  
         $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
 
         $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
   −
# Parse template with different template file syntax:
+
Parse template with different template file syntax:
 
         $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
 
         $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
 
         # Note that this is *faster* than using the default delimiters
 
         # Note that this is *faster* than using the default delimiters
Line 58: Line 59:  
               Love,
 
               Love,
 
                 G.V.
 
                 G.V.
        EOM
+
EOM
    
         use Text::Template ’fill_in_file’;
 
         use Text::Template ’fill_in_file’;
Line 66: Line 67:  
         Text::Template->always_prepend(q{use strict ’vars’;});
 
         Text::Template->always_prepend(q{use strict ’vars’;});
   −
DESCRIPTION
+
== DESCRIPTION ==
      This is a library for generating form letters, building HTML pages, or
+
This is a library for generating form letters, building HTML pages, or filling in templates generally.  A ‘template’ is a piece of text that has little Perl programs embedded in it here and there.  When you ‘fill in’ a template, you evaluate the little programs and replace them with their values.
      filling in templates generally.  A ‘template’ is a piece of text that
  −
      has little Perl programs embedded in it here and there.  When you ‘fill
  −
      in’ a template, you evaluate the little programs and replace them with
  −
      their values.
     −
      You can store a template in a file outside your program.  People can
+
You can store a template in a file outside your program.  People can modify the template without modifying the program.  You can separate the formatting details from the main code, and put the formatting parts of the program into the template.  That prevents code bloat and encourages functional separation.
      modify the template without modifying the program.  You can separate
  −
      the formatting details from the main code, and put the formatting parts
  −
      of the program into the template.  That prevents code bloat and encour-
  −
      ages functional separation.
     −
      Example
+
=== Example ===
   −
      Here’s an example of a template, which we’ll suppose is stored in the
+
Here’s an example of a template, which we’ll suppose is stored in the file "formletter.tmpl":
      file "formletter.tmpl":
      
               Dear {$title} {$lastname},
 
               Dear {$title} {$lastname},
Line 95: Line 87:  
                               Mark "Vizopteryx" Dominus
 
                               Mark "Vizopteryx" Dominus
   −
      The result of filling in this template is a string, which might look
+
The result of filling in this template is a string, which might look something like this:
 
  −
something like this:
      
               Dear Mr. Gates,
 
               Dear Mr. Gates,
Line 110: Line 100:  
                               Mark "Vizopteryx" Dominus
 
                               Mark "Vizopteryx" Dominus
   −
      Here is a complete program that transforms the example template into
+
Here is a complete program that transforms the example template into the example result, and prints it out:
      the example result, and prints it out:
      
               use Text::Template;
 
               use Text::Template;
Line 133: Line 122:  
               else { die "Couldn’t fill in template: $Text::Template::ERROR" }
 
               else { die "Couldn’t fill in template: $Text::Template::ERROR" }
   −
      Philosophy
+
=== Philosophy ===
   −
      When people make a template module like this one, they almost always
+
When people make a template module like this one, they almost always start by inventing a special syntax for substitutions.  For example, they build it so that a string like "%%VAR%%" is replaced with the value of $VAR.  Then they realize the need extra formatting, so they put in some special syntax for formatting.  Then they need a loop, so they invent a loop syntax.  Pretty soon they have a new little template language.
      start by inventing a special syntax for substitutions.  For example,
+
This approach has two problems: First, their little language is crippled. If you need to do something the author hasn’t thought of, you lose.  Second: Who wants to learn another language?  You already know Perl, so why not use it?
      they build it so that a string like "%%VAR%%" is replaced with the
  −
      value of $VAR.  Then they realize the need extra formatting, so they
  −
      put in some special syntax for formatting.  Then they need a loop, so
  −
      they invent a loop syntax.  Pretty soon they have a new little template
  −
      language.
  −
This approach has two problems: First, their little language is crip-
  −
      pled. If you need to do something the author hasn’t thought of, you
  −
      lose.  Second: Who wants to learn another language?  You already know
  −
      Perl, so why not use it?
     −
      "Text::Template" templates are programmed in Perl.  You embed Perl code
+
"Text::Template" templates are programmed in Perl.  You embed Perl code in your template, with "{" at the beginning and "}" at the end.  If you want a variable interpolated, you write it the way you would in Perl. If you need to make a loop, you can use any of the Perl loop constructions.  All the Perl built-in functions are available.
      in your template, with "{" at the beginning and "}" at the end.  If you
  −
      want a variable interpolated, you write it the way you would in Perl.
  −
      If you need to make a loop, you can use any of the Perl loop construc-
  −
      tions.  All the Perl built-in functions are available.
     −
Details
+
=== Details ===
      Template Parsing
+
==== Template Parsing ====
   −
      The "Text::Template" module scans the template source.  An open brace
+
The "Text::Template" module scans the template source.  An open brace "{" begins a program fragment, which continues until the matching close brace "}".  When the template is filled in, the program fragments are evaluated, and each one is replaced with the resulting value to yield the text that is returned.
      "{" begins a program fragment, which continues until the matching close
  −
      brace "}".  When the template is filled in, the program fragments are
  −
      evaluated, and each one is replaced with the resulting value to yield
  −
      the text that is returned.
     −
      A backslash "\" in front of a brace (or another backslash that is in
+
A backslash "\" in front of a brace (or another backslash that is in front of a brace) escapes its special meaning.  The result of filling out this template:
      front of a brace) escapes its special meaning.  The result of filling
  −
      out this template:
      
               \{ The sum of 1 and 2 is {1+2}  \}
 
               \{ The sum of 1 and 2 is {1+2}  \}
   −
      is
+
is
    
               { The sum of 1 and 2 is 3  }
 
               { The sum of 1 and 2 is 3  }
   −
      If you have an unmatched brace, "Text::Template" will return a failure
+
If you have an unmatched brace, "Text::Template" will return a failure code and a warning about where the problem is.  Backslashes that do not precede a brace are passed through unchanged.  If you have a template like this:
      code and a warning about where the problem is.  Backslashes that do not
  −
      precede a brace are passed through unchanged.  If you have a template
  −
      like this:
      
               { "String that ends in a newline.\n" }
 
               { "String that ends in a newline.\n" }
 +
The backslash inside the string is passed through to Perl unchanged, so the "\n" really does turn into a newline.  See the note at the end for details about the way backslashes work.  Backslash processing is not done when you specify alternative delimiters with the "DELIMITERS" option.  (See "Alternative Delimiters", below.)
   −
      The backslash inside the string is passed through to Perl unchanged, so
+
Each program fragment should be a sequence of Perl statements, which are evaluated the usual way.  The result of the last statement executed will be evaluated in scalar context; the result of this statement is a string, which is interpolated into the template in place of the program fragment itself.
      the "\n" really does turn into a newline.  See the note at the end for
  −
      details about the way backslashes work.  Backslash processing is not
  −
      done when you specify alternative delimiters with the "DELIMITERS"
  −
      option.  (See "Alternative Delimiters", below.)
  −
Each program fragment should be a sequence of Perl statements, which
  −
      are evaluated the usual way.  The result of the last statement executed
  −
      will be evaluted in scalar context; the result of this statement is a
  −
      string, which is interpolated into the template in place of the program
  −
      fragment itself.
     −
      The fragments are evaluated in order, and side effects from earlier
+
The fragments are evaluated in order, and side effects from earlier fragments will persist into later fragments:
      fragments will persist into later fragments:
      
               {$x = @things; ’’}The Lord High Chamberlain has gotten {$x}
 
               {$x = @things; ’’}The Lord High Chamberlain has gotten {$x}
Line 206: Line 164:  
               That is {$diff} {$more} than he gave me last year.
 
               That is {$diff} {$more} than he gave me last year.
   −
      The value of $x set in the first line will persist into the next frag-
+
The value of $x set in the first line will persist into the next fragment that begins on the third line, and the values of $diff and $more set in the second fragment will persist and be interpolated into the last line.  The output will look something like this:
      ment that begins on the third line, and the values of $diff and $more
  −
      set in the second fragment will persist and be interpolated into the
  −
      last line.  The output will look something like this:
      
               The Lord High Chamberlain has gotten 42
 
               The Lord High Chamberlain has gotten 42
Line 216: Line 171:  
               That is 25 more than he gave me last year.
 
               That is 25 more than he gave me last year.
   −
      That is all the syntax there is.
+
That is all the syntax there is.
   −
      The $OUT variable
+
==== The $OUT variable ====
    There is one special trick you can play in a template.  Here is the
+
There is one special trick you can play in a template.  Here is the motivation for it:  Suppose you are going to pass an array, @items, into the template, and you want the template to generate a bulleted list with a header, like this:
      motivation for it:  Suppose you are going to pass an array, @items,
  −
      into the template, and you want the template to generate a bulleted
  −
      list with a header, like this:
      
               Here is a list of the things I have got for you since 1907:
 
               Here is a list of the things I have got for you since 1907:
Line 230: Line 182:  
                 * ...
 
                 * ...
   −
      One way to do it is with a template like this:
+
One way to do it is with a template like this:
    
               Here is a list of the things I have got for you since 1907:
 
               Here is a list of the things I have got for you since 1907:
Line 240: Line 192:  
               }
 
               }
   −
      Here we construct the list in a variable called $blist, which we return
+
Here we construct the list in a variable called $blist, which we return at the end.  This is a little cumbersome.  There is a shortcut.
      at the end.  This is a little cumbersome.  There is a shortcut.
     −
      Inside of templates, there is a special variable called $OUT.  Anything
+
Inside of templates, there is a special variable called $OUT.  Anything you append to this variable will appear in the output of the template. Also, if you use $OUT in a program fragment, the normal behavior, of replacing the fragment with its return value, is disabled; instead the fragment is replaced with the value of $OUT.  This means that you can write the template above like this:
      you append to this variable will appear in the output of the template.
  −
      Also, if you use $OUT in a program fragment, the normal behavior, of
  −
      replacing the fragment with its return value, is disabled; instead the
  −
      fragment is replaced with the value of $OUT.  This means that you can
  −
      write the template above like this:
      
               Here is a list of the things I have got for you since 1907:
 
               Here is a list of the things I have got for you since 1907:
Line 256: Line 202:  
               }
 
               }
   −
      $OUT is reinitialized to the empty string at the start of each program
+
$OUT is reinitialized to the empty string at the start of each program fragment.  It is private to "Text::Template", so you can’t use a variable named $OUT in your template without invoking the special behavior.
      fragment.  It is private to "Text::Template", so you can’t use a
+
==== General Remarks ====
      variable named $OUT in your template without invoking the special
  −
      behavior.
  −
General Remarks
     −
      All "Text::Template" functions return "undef" on failure, and set the
+
All "Text::Template" functions return "undef" on failure, and set the variable $Text::Template::ERROR to contain an explanation of what went wrong.  For example, if you try to create a template from a file that does not exist, $Text::Template::ERROR will contain something like:
      variable $Text::Template::ERROR to contain an explanation of what went
  −
      wrong.  For example, if you try to create a template from a file that
  −
      does not exist, $Text::Template::ERROR will contain something like:
      
               Couldn’t open file xyz.tmpl: No such file or directory
 
               Couldn’t open file xyz.tmpl: No such file or directory
   −
      "new"
+
===== "new" =====
    
               $template = new Text::Template ( TYPE => ..., SOURCE => ... );
 
               $template = new Text::Template ( TYPE => ..., SOURCE => ... );
   −
      This creates and returns a new template object.  "new" returns "undef"
+
This creates and returns a new template object.  "new" returns "undef" and sets $Text::Template::ERROR if it can’t create the template object. "SOURCE" says where the template source code will come from.  "TYPE" says what kind of object the source is.
      and sets $Text::Template::ERROR if it can’t create the template object.
  −
      "SOURCE" says where the template source code will come from.  "TYPE"
  −
      says what kind of object the source is.
     −
      The most common type of source is a file:
+
The most common type of source is a file:
    
               new Text::Template ( TYPE => ’FILE’, SOURCE => $filename );
 
               new Text::Template ( TYPE => ’FILE’, SOURCE => $filename );
   −
      This reads the template from the specified file.  The filename is
+
This reads the template from the specified file.  The filename is opened with the Perl "open" command, so it can be a pipe or anything else that makes sense with "open". The "TYPE" can also be "STRING", in which case the "SOURCE" should be a
      opened with the Perl "open" command, so it can be a pipe or anything
  −
      else that makes sense with "open".
  −
 
  −
      The "TYPE" can also be "STRING", in which case the "SOURCE" should be a
   
       string:
 
       string:
   Line 292: Line 225:  
                                     SOURCE => "This is the actual template!" );
 
                                     SOURCE => "This is the actual template!" );
   −
      The "TYPE" can be "ARRAY", in which case the source should be a refer-
+
The "TYPE" can be "ARRAY", in which case the source should be a reference to an array of strings.  The concatenation of these strings is the template:
      ence to an array of strings.  The concatenation of these strings is the
  −
      template:
      
               new Text::Template ( TYPE => ’ARRAY’,
 
               new Text::Template ( TYPE => ’ARRAY’,
Line 302: Line 233:  
                                   );
 
                                   );
   −
      The "TYPE" can be FILEHANDLE, in which case the source should be an
+
The "TYPE" can be FILEHANDLE, in which case the source should be an open filehandle (such as you got from the "FileHandle" or "IO::*" packages, or a glob, or a reference to a glob).  In this case "Text::Template" will read the text from the filehandle up to end-of-file, and that text is the template:
      open filehandle (such as you got from the "FileHandle" or "IO::*" pack-
  −
      ages, or a glob, or a reference to a glob).  In this case "Text::Tem-
  −
      plate" will read the text from the filehandle up to end-of-file, and
  −
      that text is the template:
   
           # Read template source code from STDIN:
 
           # Read template source code from STDIN:
 
               new Text::Template ( TYPE => ’FILEHANDLE’,
 
               new Text::Template ( TYPE => ’FILEHANDLE’,
 
                                     SOURCE => \*STDIN  );
 
                                     SOURCE => \*STDIN  );
   −
      If you omit the "TYPE" attribute, it’s taken to be "FILE".  "SOURCE" is
+
If you omit the "TYPE" attribute, it’s taken to be "FILE".  "SOURCE" is required.  If you omit it, the program will abort.
      required.  If you omit it, the program will abort.
     −
      The words "TYPE" and "SOURCE" can be spelled any of the following ways:
+
The words "TYPE" and "SOURCE" can be spelled any of the following ways:
    
               TYPE    SOURCE
 
               TYPE    SOURCE
Line 323: Line 249:  
               -type  -source
 
               -type  -source
   −
      Pick a style you like and stick with it.
+
Pick a style you like and stick with it.
   −
      "DELIMITERS"
+
===== "DELIMITERS" =====
          You may also add a "DELIMITERS" option.  If this option is present,
+
You may also add a "DELIMITERS" option.  If this option is present, its value should be a reference to an array of two strings.  The first string is the string that signals the beginning of each program fragment, and the second string is the string that signals the end of each program fragment.  See "Alternative Delimiters", below.
          its value should be a reference to an array of two strings.  The
+
 
          first string is the string that signals the beginning of each pro-
+
===== "UNTAINT" =====
          gram fragment, and the second string is the string that signals the
+
If your program is running in taint mode, you may have problems if your templates are stored in files.  Data read from files is considered ’untrustworthy’, and taint mode will not allow you to evaluate the Perl code in the file.  (It is afraid that a malicious person might have tampered with the file.)
          end of each program fragment.  See "Alternative Delimiters", below.
     −
      "UNTAINT"
+
In some environments, however, local files are trustworthy.  You can tell "Text::Template" that a certain file is trustworthy by supplying "UNTAINT => 1" in the call to "new"This will tell "Text::Template" to disable taint checks on template code that has come from a file, as long as the filename itself is considered trustworthy.  It will also disable taint checks on template code that comes from a filehandle.  When used with "TYPE => 'string'" or "TYPE => 'array'", it has no effect.
          If your program is running in taint mode, you may have problems if
  −
          your templates are stored in filesData read from files is con-
  −
          sidered ’untrustworthy’, and taint mode will not allow you to eval-
  −
          uate the Perl code in the file(It is afraid that a malicious
  −
          person might have tampered with the file.)
     −
          In some environments, however, local files are trustworthy.  You
+
See perlsec for more complete information about tainting.
          can tell "Text::Template" that a certain file is trustworthy by
  −
          supplying "UNTAINT => 1" in the call to "new".  This will tell
  −
          "Text::Template" to disable taint checks on template code that has
  −
          come from a file, as long as the filename itself is considered
  −
          trustworthy.  It will also disable taint checks on template code
  −
          that comes from a filehandle.  When used with "TYPE => 'string'" or
  −
          "TYPE => 'array'", it has no effect.
     −
          See perlsec for more complete information about tainting.
+
Thanks to Steve Palincsar, Gerard Vreeswijk, and Dr. Christoph Baehr for help with this feature.
   −
          Thanks to Steve Palincsar, Gerard Vreeswijk, and Dr. Christoph
+
===== "PREPEND" =====
          Baehr for help with this feature.
+
This option is passed along to the "fill_in" call unless it is overridden in the arguments to "fill_in".  See ""PREPEND" feature and using "strict" in templates" below.
"PREPEND"
  −
          This option is passed along to the "fill_in" call unless it is
  −
          overridden in the arguments to "fill_in".  See ""PREPEND" feature
  −
          and using "strict" in templates" below.
     −
      "BROKEN"
+
===== "BROKEN" =====
          This option is passed along to the "fill_in" call unless it is
+
This option is passed along to the "fill_in" call unless it is overridden in the arguments to "fill_in".  See "BROKEN" below.
          overridden in the arguments to "fill_in".  See "BROKEN" below.
     −
      "compile"
+
===== "compile" =====
    
               $template->compile()
 
               $template->compile()
   −
      Loads all the template text from the template’s source, parses and com-
+
Loads all the template text from the template’s source, parses and compiles it.  If successful, returns true; otherwise returns false and sets $Text::Template::ERROR.  If the template is already compiled, it returns true and does nothing.
      piles it.  If successful, returns true; otherwise returns false and
  −
      sets $Text::Template::ERROR.  If the template is already compiled, it
  −
      returns true and does nothing.
     −
      You don’t usually need to invoke this function, because "fill_in" (see
+
You don’t usually need to invoke this function, because "fill_in" (see below) compiles the template if it isn’t compiled already.
      below) compiles the template if it isn’t compiled already.
     −
      If there is an argument to this function, it must be a reference to an
+
If there is an argument to this function, it must be a reference to an array containing alternative delimiter strings.  See ""Alternative Delimiters"", below.
      array containing alternative delimiter strings.  See ""Alternative
  −
      Delimiters"", below.
     −
      "fill_in"
+
===== "fill_in" =====
    
               $template->fill_in(OPTIONS);
 
               $template->fill_in(OPTIONS);
   −
      Fills in a template.  Returns the resulting text if successful.  Other-
+
Fills in a template.  Returns the resulting text if successful.  Otherwise, returns "undef"  and sets $Text::Template::ERROR.
      wise, returns "undef"  and sets $Text::Template::ERROR.
     −
      The OPTIONS are a hash, or a list of key-value pairs.  You can write
+
The OPTIONS are a hash, or a list of key-value pairs.  You can write the key names in any of the six usual styles as above; this means that where this manual says "PACKAGE" (for example) you can actually use any of PACKAGE Package package -PACKAGE -Package -package
      the key names in any of the six usual styles as above; this means that
  −
      where this manual says "PACKAGE" (for example) you can actually use any
  −
      of
     −
              PACKAGE Package package -PACKAGE -Package -package
+
Pick a style you like and stick with it.  The all-lowercase versions may yield spurious warnings about Ambiguous use of package => resolved to "package" so you might like to avoid them and use the capitalized versions.
   −
      Pick a style you like and stick with it. The all-lowercase versions
+
At present, there are eight legal options:  "PACKAGE", "BROKEN", "BROKEN_ARG", "SAFE", "HASH", "OUTPUT", and "DELIMITERS".
      may yield spurious warnings about
     −
              Ambiguous use of package => resolved to "package"
+
===== "PACKAGE" =====
 
+
"PACKAGE" specifies the name of a package in which the program fragments should be evaluated.  The default is to use the package from which "fill_in" was called.  For example, consider this template:
      so you might like to avoid them and use the capitalized versions.
  −
 
  −
      At present, there are eight legal options:  "PACKAGE", "BROKEN", "BRO-
  −
      KEN_ARG", "SAFE", "HASH", "OUTPUT", and "DELIMITERS".
  −
 
  −
      "PACKAGE"
  −
        "PACKAGE" specifies the name of a package in which the program
  −
          fragments should be evaluated.  The default is to use the package
  −
          from which "fill_in" was called.  For example, consider this tem-
  −
          plate:
      
                   The value of the variable x is {$x}.
 
                   The value of the variable x is {$x}.
   −
          If you use "$template->fill_in(PACKAGE => 'R')" , then the $x in
+
If you use "$template->fill_in(PACKAGE => 'R')" , then the $x in the template is actually replaced with the value of $R::x.  If you omit the "PACKAGE" option, $x will be replaced with the value of the $x variable in the package that actually called "fill_in".
          the template is actually replaced with the value of $R::x.  If you
  −
          omit the "PACKAGE" option, $x will be replaced with the value of
  −
          the $x variable in the package that actually called "fill_in".
     −
          You should almost always use "PACKAGE".  If you don’t, and your
+
You should almost always use "PACKAGE".  If you don’t, and your template makes changes to variables, those changes will be propagated back into the main program.  Evaluating the template in a private package helps prevent this.  The template can still modify variables in your program if it wants to, but it will have to do so explicitly.  See the section at the end on ‘Security’.
          template makes changes to variables, those changes will be propa-
  −
          gated back into the main program.  Evaluating the template in a
  −
          private package helps prevent this.  The template can still modify
  −
          variables in your program if it wants to, but it will have to do so
  −
          explicitly.  See the section at the end on ‘Security’.
     −
          Here’s an example of using "PACKAGE":
+
Here’s an example of using "PACKAGE":
    
                   Your Royal Highness,
 
                   Your Royal Highness,
Line 437: Line 316:  
                   Lord High Chamberlain
 
                   Lord High Chamberlain
   −
          We want to pass in an array which will be assigned to the array
+
We want to pass in an array which will be assigned to the array @items.  Here’s how to do that:
          @items.  Here’s how to do that:
      
                   @items = (’ivory’, ’apes’, ’peacocks’, );
 
                   @items = (’ivory’, ’apes’, ’peacocks’, );
 
                   $template->fill_in();
 
                   $template->fill_in();
   −
          This is not very safe.  The reason this isn’t as safe is that if
+
This is not very safe.  The reason this isn’t as safe is that if you had a variable named $item_no in scope in your program at the point you called "fill_in", its value would be clobbered by the act of filling out the template.  The problem is the same as if you had written a subroutine that used those variables in the same way that the template does.  ($OUT is special in templates and is always safe.)
          you had a variable named $item_no in scope in your program at the
+
One solution to this is to make the $item_no variable private to the template by declaring it with "my".  If the template does this, you are safe.
          point you called "fill_in", its value would be clobbered by the act
  −
          of filling out the template.  The problem is the same as if you had
  −
          written a subroutine that used those variables in the same way that
  −
          the template does.  ($OUT is special in templates and is always
  −
          safe.)
  −
          One solution to this is to make the $item_no variable private to
  −
          the template by declaring it with "my".  If the template does this,
  −
          you are safe.
     −
          But if you use the "PACKAGE" option, you will probably be safe even
+
But if you use the "PACKAGE" option, you will probably be safe even if the template does not declare its variables with "my":
          if the template does not declare its variables with "my":
      
                   @Q::items = (’ivory’, ’apes’, ’peacocks’, );
 
                   @Q::items = (’ivory’, ’apes’, ’peacocks’, );
 
                   $template->fill_in(PACKAGE => ’Q’);
 
                   $template->fill_in(PACKAGE => ’Q’);
   −
          In this case the template will clobber the variable $Q::item_no,
+
In this case the template will clobber the variable $Q::item_no, which is not related to the one your program was using.
          which is not related to the one your program was using.
     −
          Templates cannot affect variables in the main program that are
+
Templates cannot affect variables in the main program that are declared with "my", unless you give the template references to those variables.
          declared with "my", unless you give the template references to
  −
          those variables.
     −
      "HASH"
+
===== "HASH" =====
          You may not want to put the template variables into a package.
+
You may not want to put the template variables into a package. Packages can be hard to manage:  You can’t copy them, for example. "HASH" provides an alternative.
          Packages can be hard to manage:  You can’t copy them, for example.
  −
          "HASH" provides an alternative.
     −
          The value for "HASH" should be a reference to a hash that maps
+
The value for "HASH" should be a reference to a hash that maps variable names to values.  For example,
          variable names to values.  For example,
      
                   $template->fill_in(HASH => { recipient => "The King",
 
                   $template->fill_in(HASH => { recipient => "The King",
Line 480: Line 343:  
                                               });
 
                                               });
   −
          will fill out the template and use "The King" as the value of
+
will fill out the template and use "The King" as the value of $recipient and the list of items as the value of @items.  Note that we pass an array reference, but inside the template it appears as an array.  In general, anything other than a simple string or number should be passed by reference.
          $recipient and the list of items as the value of @items.  Note that
  −
          we pass an array reference, but inside the template it appears as
  −
          an array.  In general, anything other than a simple string or num-
  −
          ber should be passed by reference.
  −
 
  −
          We also want to pass an object, which is in $self; note that we
  −
          pass a reference to the object, "\$self" instead.  Since we’ve
  −
          passed a reference to a scalar, inside the template the object
  −
          appears as $object.
     −
          The full details of how it works are a little involved, so you
+
We also want to pass an object, which is in $self; note that we pass a reference to the object, "\$self" instead.  Since we’ve passed a reference to a scalar, inside the template the object appears as $object.
          might want to skip to the next section.
     −
          Suppose the key in the hash is key and the value is value.
+
The full details of how it works are a little involved, so you might want to skip to the next section.
      *  If the value is "undef", then any variables named $key, @key,
  −
              %key, etc., are undefined.
     −
          *   If the value is a string or a number, then $key is set to that
+
Suppose the key in the hash is key and the value is value.
              value in the template.
+
* If the value is "undef", then any variables named $key, @key, %key, etc., are undefined.
   −
          *   For anything else, you must pass a reference.
+
* If the value is a string or a number, then $key is set to that value in the template.
   −
              If the value is a reference to an array, then @key is set to
+
* For anything else, you must pass a reference.
              that array.  If the value is a reference to a hash, then %key
+
If the value is a reference to an array, then @key is set to that array.  If the value is a reference to a hash, then %key is set to that hash.  Similarly if value is any other kind of reference.  This means that
              is set to that hash.  Similarly if value is any other kind of
  −
              reference.  This means that
      
                       var => "foo"
 
                       var => "foo"
   −
              and
+
and
    
                       var => \"foo"
 
                       var => \"foo"
   −
              have almost exactly the same effect.  (The difference is that
+
have almost exactly the same effect.  (The difference is that in the former case, the value is copied, and in the latter case it is aliased.)
              in the former case, the value is copied, and in the latter case
  −
              it is aliased.)
     −
          *   In particular, if you want the template to get an object or any
+
* In particular, if you want the template to get an object or any kind, you must pass a reference to it:
              kind, you must pass a reference to it:
      
                       $template->fill_in(HASH => { database_handle => \$dbh, ... });
 
                       $template->fill_in(HASH => { database_handle => \$dbh, ... });
   −
              If you do this, the template will have a variable
+
If you do this, the template will have a variable $database_handle which is the database handle object. If you leave out the "\", the template will have a hash %database_handle, which exposes the internal structure of the database handle object; you don’t want that.
              $database_handle which is the database handle object. If you
  −
              leave out the "\", the template will have a hash %database_han-
  −
              dle, which exposes the internal structure of the database han-
  −
              dle object; you don’t want that.
     −
          Normally, the way this works is by allocating a private package,
+
Normally, the way this works is by allocating a private package, loading all the variables into the package, and then filling out the template as if you had specified that package.  A new package is allocated each time.  However, if you also use the "PACKAGE" option, "Text::Template" loads the variables into the package you specified, and they stay there after the call returns.  Subsequent calls to "fill_in" that use the same package will pick up the values you loaded in.
          loading all the variables into the package, and then filling out
+
If the argument of "HASH" is a reference to an array instead of a reference to a hash, then the array should contain a list of hashes whose contents are loaded into the template package one after the other.  You can use this feature if you want to combine several sets of variables.  For example, one set of variables might be the defaults for a fill-in form, and the second set might be the user inputs, which override the defaults when they are present:
          the template as if you had specified that package.  A new package
  −
          is allocated each time.  However, if you also use the "PACKAGE"
  −
          option, "Text::Template" loads the variables into the package you
  −
          specified, and they stay there after the call returns.  Subsequent
  −
          calls to "fill_in" that use the same package will pick up the val-
  −
          ues you loaded in.
  −
          If the argument of "HASH" is a reference to an array instead of a
  −
          reference to a hash, then the array should contain a list of hashes
  −
          whose contents are loaded into the template package one after the
  −
          other.  You can use this feature if you want to combine several
  −
          sets of variables.  For example, one set of variables might be the
  −
          defaults for a fill-in form, and the second set might be the user
  −
          inputs, which override the defaults when they are present:
      
                   $template->fill_in(HASH => [\%defaults, \%user_input]);
 
                   $template->fill_in(HASH => [\%defaults, \%user_input]);
   −
          You can also use this to set two variables with the same name:
+
You can also use this to set two variables with the same name:
    
                   $template->fill_in(HASH => [{ v => "The King" },
 
                   $template->fill_in(HASH => [{ v => "The King" },
Line 554: Line 383:  
                                     );
 
                                     );
   −
          This sets $v to "The King" and @v to "(1,2,3)".
+
This sets $v to "The King" and @v to "(1,2,3)".
   −
      "BROKEN"
+
===== "BROKEN" =====
 
           If any of the program fragments fails to compile or aborts for any
 
           If any of the program fragments fails to compile or aborts for any
 
           reason, and you have set the "BROKEN" option to a function refer-
 
           reason, and you have set the "BROKEN" option to a function refer-
Line 1,279: Line 1,108:  
       ing list.  The mailing list address is a secret.)
 
       ing list.  The mailing list address is a secret.)
   −
LICENSE
+
=== LICENSE ===
 
           Text::Template version 1.45
 
           Text::Template version 1.45
 
           Copyright (C) 2008 Mark Jason Dominus
 
           Copyright (C) 2008 Mark Jason Dominus
Line 1,299: Line 1,128:  
           Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
           Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   −
THANKS
+
=== THANKS ===
 
       Many thanks to the following people for offering support, encourage-
 
       Many thanks to the following people for offering support, encourage-
 
       ment, advice, bug reports, and all the other good stuff.
 
       ment, advice, bug reports, and all the other good stuff.

Navigation menu