NAME
Parse::Template - Template Processor (0.3)
SYNOPSIS
use Parse::Template;
my %template =
(
'TOP' => q!before <<$template->eval('DATA')>> after!,
'DATA' => q!Insert data: ! .
q!1. List: <<"@list$N">>! .
q!2. Hash: <<"$hash{'key_value'}$N">>! .
q!3. Sub: <<&SUB()$N>>!
);
my $tmplt = new Parse::Template (\%template);
open FH, "< foo";
$tmplt->env('var' => '(value!)');
$tmplt->env('list' => [1, 2, 10],
'N' => "\n",
'tmplt' => \$tmplt,
'FH' => \*FH,
'SUB' => sub { "->content generated by a sub<-" },
'hash' => { 'key_value' => q!It's an hash value! });
print $tmplt->eval('TOP'), "\n";
DESCRIPTION
Parse::Template permits evaluating Perl expressions placed within a template. These expressions must be surrounded by <<
and >>
, and they are evaluated within an environment specific to each instance of the Parse::Template
class.
Parse::Template
creates a class specific to each instance. This class inherits from the class Parse::Template
and contains the environment that is specific to the template.
The env()
method permits constructing the required evaluation environment. Each entry must be specified using a key consisting of the name of the symbol to be created, associated with a reference whose type is that of entry to be created (for example, a reference to an array to create an array). A scalar variable is defined by declaring the name of the variable, associated with its value. A scalar variable containing a reference is defined by writing 'var' => \$variable
, where $variable
is a lexical variable that contains the reference.
This package was initially created to serve as a code generator for the Parse::Lex
class. You will find examples of its use in the classes Parse::Lex
et Parse::CLex
.
METHODS
- new HASH
-
Constructor for the class.
- env HASH
- env SYMBOL
-
Permits defining the environment that is specific to a template.
env(SYMBOL)
returns the reference associated with the symbol, orundef
if the symbol is not defined. The reference that is returned is of the type indicated by the character (&, $, %, @, *
) that prefixes the symbol.Example:
$tmplt->env('LIST' => [1, 2, 3])} Defines a list @{$tmplt->env('*LIST')} Returns the list @{$tmplt->env('@LIST')} Ditto
- getPart PART_LABEL
-
Returns the designated part of the template.
- ppregexp REGEXP
-
Preprocesses a regular expression so that it can be inserted into a template where the regular expression delimiter is either the character "/" or the character "!".
- setPart PART_LABEL => TEXT
-
A template is defined by a hash (associative array).
setPart()
permits defining a new entry within this hash. - undef
-
Permits destroying a template instance (see BUGS).
NOTES CONCERNING THE CURRENT VERSION
This is an experimental module. Send me your comments.
BUGS
An instance is not destroyed when the variable within which it is placed is itself destroyed. This is because this instance is also referenced within its class. Therefore, don't use this class to create a large number of instances, or if possible, use the undef()
method.
AUTHOR
Philippe Verdret
COPYRIGHT
Copyright (c) 1995-1998 Philippe Verdret. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.