NAME

Parse::Template - Template Processor (0.30)

SYNOPSIS

 use Parse::Template;

 my %template = 
   (
    'TOP' =>  q!before %%$self->eval('DATA')%% after!,
    'DATA' => q!Insert data: ! .
              q!1. List: %%"@list$N"%%! .
              q!2. Hash: %%"$hash{'key_value'}$N"%%! .
              q!3. Hash: %%print <FH>%%! .
              q!4. Sub: %%&SUB()$N%%!
   );

 my $tmplt = new Parse::Template (%template);
 open FH, "< foo";

 $tmplt->env('var' => '(value!)');
 $tmplt->env('list' => [1, 2, 10], 
             'N' => "\n",
             'FH' => \*FH,
             'SUB' => sub { "->content generated by a sub<-" },
             'hash' => { 'key_value' => q!It\'s an hash value! });
 print $tmplt->eval('TOP'), "\n";

DESCRIPTION

The Parse::Template class permits evaluating Perl expressions placed within a template. The template is a hash passed as an argument to the new() method : Parse::Template->new('someKey', '... text with expressions to evaluate ...'). This specification permits splitting up a template into different parts. Within a part, a sub-part can be including by means of the expression :

$self->eval('SUB_PART_NAME')

$self designates the instance of the Parse::Template class. If you specify only the name of the part, a subroutine with the name of the part will be automatically defined. In the example given in the synopsis, the insertion of the TOP part can be rewritten:

'TOP' => q!before %%DATA()%%after!

The subroutines can take arguments.

The expressions to be evaluated must be placed within %%, and they are evaluated within an environment specific to each instance of the Parse::Template class. Each instance is defined within a specific class that is a subclass of Parse::Template. This class contains the environment specific to the template and inherits from the Parse::Template class.

The env() method permits constructing the required evaluation environment. Each entry to be defined within the environment 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 the created entry (for example, a reference to an array to create an array). A scalar variable is defined by declaring a name for 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, Parse::CLex and Parse::Token.

METHODS

new HASH

Constructor for the class. HASH is a hash which defines the template text.

Example:

use Parse::Template;
$t = new Parse::Template('key' => 'associated text');
env HASH
env SYMBOL

Permits defining the environment that is specific to a template. env(SYMBOL) returns the reference associated with the symbol, or undef if the symbol is not defined. The reference that is returned is of the type indicated by the character (&, $, %, @, *) that prefixes the symbol.

Examples:

$tmplt->env('LIST' => [1, 2, 3])}   Defines a list

@{$tmplt->env('*LIST')}             Returns the list

@{$tmplt->env('@LIST')}             Ditto
eval PART_NAME

Evaluates the template part designated by PART_NAME. Returns the string resulting from this evaluation.

getPart PART_NAME

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_NAME => TEXT

A template is defined by a hash (associative array). setPart() permits defining a new entry within this hash.

Examples

See the examples directory included in this distribution.

NOTES CONCERNING THE CURRENT VERSION

This is an experimental module. I would be very interested to receive your comments and suggestions.

AUTHOR

Philippe Verdret

COPYRIGHT

Copyright (c) 1995-1999 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.