Brian Duggan > String-Template > String::Template

Download:
String-Template-0.11.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.11   Source  

NAME ^

String::Template - Fills in string templates from hash of fields

SYNOPSIS ^

  use String::Template;

  my %fields = ( num => 2, str => 'this', date => 'Feb 27, 2008' );

  my $template = "...<num%04d>...<str>...<date:%Y/%m/%d>...\n";

  print expand_string($template, \%fields);

  prints: "...0002...this...2008/02/27..."

DESCRIPTION ^

$str = expand_string($template, \%fields, $undef_flag).

Fills in a simple template with values from a hash, replacing tokens like "<fieldname>" with the value from the hash $fields->{fieldname}.

Some special characters can be used to impose formatting on the fields:

 % - treat like a sprintf() format
     e.g.  <int%02d>

 : - treat like a L<POSIX::strftime()> format
     e.g. <date:%Y-%m-%d>

 ! - Just like ':', but with gmtime instead of localtime
     e.g. <gmdate!%Y-%m-%d %H:%M>

 # - treat like args to substr()
     e.g. <str#0,2> or <str#4>

For the ':' strftime formats, the field is parsed by Date::Parse, so it can handle any format that can handle.

Handling of undefined fields can be controlled with $undef_flag. If it is false (default), undefined fields are simply replace with an empty string. If set to true, the field is kept verbatim. This can be useful for multiple expansion passes.

$str = expand_stringi($template, \%fields, $undef_flag).

expand_stringi works just like expand_string, except that tokens and hash keys are treated case insensitively.

@missing = missing_values($template, \%fields, $dont_allow_undefs)

Checks to see if the template variables in a string template exist in a hash. Set $dont_allow_undefs to 1 to also check to see if the values for all such keys are defined.

Returns a list of missing keys or an empty list if no keys were missing.

$status = expand_hash($hash[, $maxdepth]);

Expand a hash of templates/values. This function will repeatedly replace templates in the values of the hash with the values of the hash they reference, until either all "<>" templates are gone, or it has iterated $maxdepth times (default 10).

Returns undef if there are unexpanded templates left, otherwise true.

AUTHOR ^

  Brian Duggan

  Curt Tilmes

SEE ALSO ^

String::Format performs a similar function, with a different syntax.

syntax highlighting: