ParseTemplate-3.08/000075500000000000000000000000001202355537000142125ustar00rootroot00000000000000ParseTemplate-3.08/Changes000064400000000000000000000040031202355537000155020ustar00rootroot000000000000003.08 2012-02-28 Bug Fixes * Carp 1.25 now has one dot more, addapt regexp matching $@ to cope with it. 3.07 2010-10-15 Bug Fixes * Solved problem with test scripts: done_testing() requires Test::More 0.96 3.06 2010-10-12 Bug Fixes * Solved problem with test scripts: Bareword "done_testing" not allowed while "strict subs" in use 3.05 2010-08-14 Bug Fixes * Test FAIL ParseTemplate-3.04 v5.6.2 Linux due to different error message for mal-formed regexp 3.04 2010-08-13 Bug Fixes * Solve RT #58128: regular expression parsing is broken for some special characters 3.03 2010-04-03 Bug Fixes * Solve warning: v-string in use/require non-portable at blib/lib/Parse/Template.pm line 3. 3.02 2010-03-26 Bug Fixes * Solve RT #55977 (parse::template should not be part of parselex) * Bump version number to force re-index, after new ParseLex was uploaded without Parse::Template 3.01 2010-03-26 Bug Fixes * Ignore whitespace in the comparison run in the tests, to avoid tests breaking caused by blanks at the end of result lines or CR-LF vs LF differences. * Add -Iblib/lib to all test program executions, so that our library is used in the test; otherwise tests fail if Parse::Template is not already installed in the machine 3.00 2010-03-25 Bug Fixes * Bump version to 3.00, to be higher than the last ParseLex version that had it own (older) version of Parse::Template (2.18). This older version does not compile in Perl 5.11 due to the usage of a deprecated feature : Use of inherited AUTOLOAD for non-method %s() is deprecated A new ParseLex will be uploaded without the older Parse::Template inside, and with a dependency to this Parse::Template instead. * Updated the existing English translation of the POD from the older version, replaced the French POD by the English one. * Minor changes due to warnings. 0.37 2001-06-13 Other * Last version published by the original author, Philippe Verdret. ParseTemplate-3.08/MANIFEST000064400000000000000000000011501202355537000153400ustar00rootroot00000000000000Changes examples/delegation2.pl examples/derived.pl examples/die.pl examples/error.pl examples/html_generator.pl examples/html_generator2.pl examples/html_template.pl examples/includes.pl examples/inheritance.pl examples/recursive.pl examples/synopsis.pl examples/synopsis_bis.pl lib/Parse/Template.pm Makefile.PL MANIFEST README t/debug.t t/rt_58128.t t/test1.t t/test2.t t/test3.t t/test4.t t/test5.t t/test6.t t/W.pm META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) ParseTemplate-3.08/META.json000064400000000000000000000017231202355537000156360ustar00rootroot00000000000000{ "abstract" : "Processor for templates containing Perl expressions", "author" : [ "Philippe Verdret " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.120351", "license" : [ "unknown" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "ParseTemplate", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Symbol" : "0", "Test::More" : "0.96" } } }, "release_status" : "stable", "version" : "3.08" } ParseTemplate-3.08/META.yml000064400000000000000000000010561202355537000154650ustar00rootroot00000000000000--- abstract: 'Processor for templates containing Perl expressions' author: - 'Philippe Verdret ' build_requires: ExtUtils::MakeMaker: 0 configure_requires: ExtUtils::MakeMaker: 0 dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 6.62, CPAN::Meta::Converter version 2.120351' license: unknown meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: ParseTemplate no_index: directory: - t - inc requires: Symbol: 0 Test::More: 0.96 version: 3.08 ParseTemplate-3.08/Makefile.PL000064400000000000000000000011631202355537000161650ustar00rootroot00000000000000use ExtUtils::MakeMaker; # See the Camel, page 409- require 5.000; WriteMakefile( $] >= 5.005 ? (AUTHOR => 'Philippe Verdret ', ABSTRACT => 'Processor for templates containing Perl expressions') : (), NAME => 'Parse::Template', DISTNAME => "ParseTemplate", VERSION_FROM => 'lib/Parse/Template.pm', clean => { 'FILES' => 'err testlog doc/pod2html-*' }, dist => { COMPRESS => 'gzip', SUFFIX => 'gz' }, PREREQ_PM => { 'Test::More' => 0.96, # needs done_testing() 'Symbol' => 0, }, ); ParseTemplate-3.08/README000064400000000000000000000010361202355537000150720ustar00rootroot00000000000000 Parse::Template Parse::Template was initially created to serve as a code generator for the Parse::Lex class. The class is now available as a separate module. Copyright (c) 1995-2001 Philippe Verdret. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -- Philippe Verdret _________________________________ How to BUILD, TEST and INSTALL it % perl Makefile.PL % make % make test # or make test TEST_VERBOSE=1 or 2 % make install ParseTemplate-3.08/examples/000075500000000000000000000000001202355537000160305ustar00rootroot00000000000000ParseTemplate-3.08/examples/delegation2.pl000064400000000000000000000021411202355537000205600ustar00rootroot00000000000000use Parse::Template; my %ancestor = ( 'ANCESTOR' => q!%%"ANCESTOR/$part ->" . SUB_PART()%%!, 'SUB_PART' => q!ANCESTOR/ %%"$part\n"%%!, ); my %child = ( 'CHILD' => q!CHILD/ %%"$part"%% -> %%ANCESTOR()%%!, 'SUB_PART' => q!CHILD/ %%"$part\n"%%!, ); my $A = new Parse::Template (%ancestor); my $C = $A->new(%child); #print '$A->ANCESTOR(): ', $A->ANCESTOR(); #print '$C->SUB_PART(): ', $C->SUB_PART(); print '$A->ANCESTOR(): ', $A->ANCESTOR(); print '$C->CHILD(): ', $C->CHILD(); print '$C->SUB_PART(): ', $C->SUB_PART(); #print '$C->ANCESTOR(): ', $C->ANCESTOR(); #print '$C->SUB_PART(): ', $C->SUB_PART(); exit; # ??? print '$C->ANCESTOR() ', $C->ANCESTOR(); print '$C->CHILD()', $C->CHILD(); print '$C->SUB_PART()', $C->SUB_PART(); print $A->ANCESTOR(); print '$C->CHILD()', $C->CHILD(); print $C->ANCESTOR(); print $A->ANCESTOR(); print $C->ANCESTOR(); print $C->CHILD(); print $A->SUB_PART(); print $C->CHILD(); __END__ $A->ANCESTOR(): ANCESTOR/ANCESTOR ->ANCESTOR/ SUB_PART $C->CHILD(): CHILD/ CHILD -> ANCESTOR/ANCESTOR ->CHILD/ SUB_PART ParseTemplate-3.08/examples/derived.pl000075500000000000000000000011311202355537000200060ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; use lib '../lib'; use Parse::Template; my %ancestor = ( 'TOP' => q!ANCESTOR template: %%"'$part' part ->\n" . CHILD()%%!, 'ANCESTOR' => q!ANCESTOR template: %%"'$part' part"%%!, ); my %parent = ( 'PARENT' => q!PARENT template: %%"'$part' part ->\n" . ANCESTOR()%%!, ); my %child = ( 'CHILD' => q!CHILD template: %%"'$part' part ->\n" . PARENT() . "\n"%%!, ); my $A = new Parse::Template (%ancestor); my $P = $A->new(%parent); my $C = $P->new(%child); print $C->TOP(); 1; __END__ ParseTemplate-3.08/examples/die.pl000075500000000000000000000007231202355537000171330ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; #use diagnostics; use Carp; BEGIN { unshift @INC, "../lib"; } use Parse::Template; $|++; $Parse::Template::CONFESS = 1; Parse::Template->new( #'TOP' => q!%%$_[0] < 10 ? '[' . TOP($_[0] + 1) . ']' : '' %%! #'TOP' => q!%%$_[0] < 10 ? '[' . TOP($_[0] + 1) . ']' : Carp::confess() %%! 'TOP' => q!%%$_[0] < 1 ? '[' . TOP($_[0] + 1) . ']' : die() %%! )->eval('TOP', 0); ParseTemplate-3.08/examples/error.pl000075500000000000000000000011741202355537000175240ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; #use diagnostics; use Carp; BEGIN { unshift @INC, "../lib"; } use Parse::Template; $|++; $Parse::Template::CONFESS = 0; eval { Parse::Template->new( 'TOP' => q!%%$_[0] < 3 ? '[' . TOP($_[0] + 1) . ']' : DIE() %%!, 'ERROR' => q!%% problem++ %%!, 'DIE' => q!%%die()%%!, )->eval('TOP', 0); }; __END__ exit; $Parse::Template::CONFESS = 0; print STDERR "---\n"; eval { Parse::Template->new( 'TOP' => q!%%$_[0] < 10 ? '[' . TOP($_[0] + 1) . ']' : ERROR() %%!, 'ERROR' => q!%% problem++ %%!, )->eval('TOP', 0); }; die; ParseTemplate-3.08/examples/html_generator.pl000075500000000000000000000032761202355537000214120ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; use Parse::Template; my $T = new Parse::Template('HTML' => '%%"<$part>" . $N . HEAD() . $N . BODY() . $N . "$N"%%', 'HEAD' => '%%"<$part>" . $N . "$N"%%', 'BODY' => '%%$N . CONTENT() . "$N"%%', 'CONTENT' => '

A very simple document: %%ORDERED_LIST(0)%%', 'ORDERED_LIST' => q!%%$_[0] < 4 ? "$N

  1. $_[0]" . ORDERED_LIST($_[0] + 1) . "
  2. $_[0]$N
$N" : ''%%!, ); $T->env('N' => "\n"); print $T->eval('HTML'); my $ELT_CONTENT = q!%%join '', @_%%!; my $HTML_T1 = new Parse::Template( 'DOC' => '%%H1(B("text in bold"), I("text in italic"))%%', 'H1' => qq!

$ELT_CONTENT

!, 'B' => qq!$ELT_CONTENT!, 'I' => qq!$ELT_CONTENT!, ); print $HTML_T1->eval('DOC'), "\n"; $ELT_CONTENT = q!%%"<$part>" . join('', @_) . ""%%!; my $HTML_T2 = new Parse::Template( 'DOC' => '%%H1(B("text in bold"), I("text in italic"))%%', 'H1' => qq!$ELT_CONTENT!, 'B' => qq!$ELT_CONTENT!, 'I' => qq!$ELT_CONTENT!, ); print $HTML_T2->eval('DOC'), "\n"; my $DOC = q!H1(B("text in bold"), I("text in italic"))!; $ELT_CONTENT = q!%%"<$part>" . join('', @_) . ""%%!; my $HTML_T3 = new Parse::Template( 'DOC' => qq!%%$DOC%%!, map { $_ => $ELT_CONTENT } qw(H1 B I) ); print $HTML_T3->eval('DOC'), "\n"; $ELT_CONTENT = q!%%shift(@_); "<$part>" . join('', @_) . ""%%!; my $HTML_T4 = new Parse::Template(map { $_ => $ELT_CONTENT } qw(H1 B I)); print $HTML_T4->H1( $HTML_T4->B("text in bold"), $HTML_T4->I("text in italic") ), "\n"; ParseTemplate-3.08/examples/html_generator2.pl000075500000000000000000000005731202355537000214710ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; use Parse::Template; my $ELT_CONTENT = q!%%"<$part>" . join('', @_) . ""%%!; my $G = new Parse::Template( map { $_ => $ELT_CONTENT } qw(H1 B I) ); # how to find this more nicely? @main::ISA = ref($G); *AUTOLOAD = \&Parse::Template::AUTOLOAD; print H1(B("text in bold"), I("text in italic")); __DATA__ ParseTemplate-3.08/examples/html_template.pl000075500000000000000000000015241202355537000212310ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; use Parse::Template; my %template = ('DOC' => <<'END_OF_DOC;', 'SECTION_PART' => <<'END_OF_SECTION_PART;'); %% my $content; for (my $i = 0; $i <= $#section_content; $i++) { $content .= SECTION_PART($i); } $content; %% END_OF_DOC; %% $section_content[$_[0]]->{Content} =~ s/^/

/mg; join '', '

', $section_content[$_[0]]->{Title}, '

', $section_content[$_[0]]->{Content}; %% END_OF_SECTION_PART; my $tmplt = new Parse::Template (%template); $tmplt->env('section_content' => [ { Title => 'First Section', Content => 'Nothing to write' }, { Title => 'Second section', Content => 'Nothing else to write' } ] ); print $tmplt->eval('DOC'), "\n"; ParseTemplate-3.08/examples/includes.pl000075500000000000000000000012151202355537000201750ustar00rootroot00000000000000#!/usr/local/bin/perl -w # Process a document including directives like this: # # %%include("body.htm")%% # \_____________________/ # require 5.004; use strict; use Parse::Template; use constant TRACE => 1; my $T = new Parse::Template(); $T->env('include' => sub { shift if ref $_[0]; print STDERR "include $_[0]\n" if TRACE; local *FH; open FH, "< $_[0]" or die "unable to open '$_[0]': $!"; my $text = join '', ; $T->setPart(INCLUDE => $text); $T->INCLUDE(); }); if (@ARGV) { print $T->include($ARGV[0]); } else { print $T->include('root.htm'); } ParseTemplate-3.08/examples/inheritance.pl000075500000000000000000000006721202355537000206660ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; BEGIN { unshift @INC, "../lib"; } use Parse::Template; my %template = ( 'TOP' => q!%%$self->method(@_)%%! ); my $t1 = new Parse::Template (%template); $t1->env( 'method' => sub { print ref shift, " args: @_\n"; }, ); $t1->eval('TOP', qw/a b c/); my $t2 = $t1->new(%template); # 't2' is a sub-class of 't1' $t2->eval('TOP', qw'x y z'); ParseTemplate-3.08/examples/recursive.pl000075500000000000000000000004131202355537000203750ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; BEGIN { unshift @INC, "../lib"; } use Parse::Template; # recursive calls print Parse::Template->new( 'TOP' => q!%%$_[0] < 10 ? '[' . TOP($_[0] + 1) . ']' : ''%%! )->eval('TOP', 0); ParseTemplate-3.08/examples/synopsis.pl000075500000000000000000000012041202355537000202540ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; BEGIN { unshift @INC, "../lib"; } use Parse::Template; my %template = ( 'TOP' => q!Text before%%$N . SUB_PART(1)%%Text after!, 'SUB_PART' => q!Inserted part from %%"$part(@_)"%% 1. List: %%"@list"%% 2. Hash: %%"$hash{'some_key'}"%% 3. Sub: %%&SUB(1,2,3,'soleil')%%! ); my $tmplt = new Parse::Template (%template); $tmplt->env('var' => 'scalar value!'); $tmplt->env('list' => [1, 2, 10], 'N' => "\n", 'SUB' => sub { "arguments: @_\n" }, 'hash' => { 'some_key' => q!It\'s an hash value! }); print $tmplt->eval('TOP'), "\n"; ParseTemplate-3.08/examples/synopsis_bis.pl000075500000000000000000000013041202355537000211120ustar00rootroot00000000000000#!/usr/local/bin/perl -w require 5.004; use strict; BEGIN { unshift @INC, "../lib"; } use Parse::Template; my %template = ( 'TOP' => q!Text before %%DATA(1)%%Text after!, 'DATA' => q!Inserted data: %%"@_$N"%%! . q!1. List: %%"@list$N"%%! . q!2. Hash: %%"$hash{'key_value'}$N"%%! . q!3. Sub: %%&SUB(1,2,3,'soleil')%%! ); { my $tmplt = new Parse::Template (%template); $tmplt->env('var' => '(value!)'); $tmplt->env('list' => [1, 2, 10], 'N' => "\n", 'SUB' => sub { "arguments: @_\n" }, 'hash' => { 'key_value' => q!It\'s an hash value! }); print $tmplt->eval('TOP'), "\n"; print "END OF BLOCK\n"; } print "after the BLOCK\n"; ParseTemplate-3.08/lib/000075500000000000000000000000001202355537000147605ustar00rootroot00000000000000ParseTemplate-3.08/lib/Parse/000075500000000000000000000000001202355537000160325ustar00rootroot00000000000000ParseTemplate-3.08/lib/Parse/Template.pm000064400000000000000000000422301202355537000201440ustar00rootroot00000000000000use strict; use warnings; require 5.006; package Parse::Template; $Parse::Template::VERSION = '3.08'; use Carp; use constant DEBUG => 0; use vars qw/$AUTOLOAD/; sub AUTOLOAD { my($class, $part) = ($AUTOLOAD =~ /(.*)::(.*)$/); no strict 'refs'; *$AUTOLOAD = sub { (ref $_[0] || $class)->eval("$part", @_) }; goto &$AUTOLOAD; } use Symbol qw(delete_package); { my $id = 0; sub getid { $id++ } } my $PACKAGE = __PACKAGE__; sub new { my $receiver = shift; my $class = $PACKAGE . '::Sym' . getid(); my $self = bless {}, $class; # absolutely nothing in $self no strict 'refs'; %{"${class}::template"} = (); # so no 'used only once' warning ${"${class}::ancestor"} = ''; # so no 'used only once' warning @{"${class}::ISA"} = ref $receiver || $receiver; ${"${class}::ancestor"} = $receiver; # reverse the destruction order *{"${class}::AUTOLOAD"} = \&AUTOLOAD; # so no warning for procedural calls %{"${class}::template"} = @_ ; $self; } use constant TRACE_ENV => 0; sub env { my $self = shift; my $class = ref $self || $self; my $symbol = shift; if ($symbol =~ /\W/) { Carp::croak "invalid symbol name: $symbol" } no strict; if (@_) { do { my $value = shift; print STDERR "${class}::$symbol\t$value\n" if TRACE_ENV; if (ref $value) { *{"${class}::$symbol"} = $value; } else { # scalar value *{"${class}::$symbol"} = \$value; } $symbol = shift if @_; if ($symbol =~ /\W/) { Carp::croak "invalid symbol name: $symbol"; } } while (@_); } elsif (defined *{"${class}::$symbol"}) { # borrowed from Exporter.pm return \&{"${class}::$symbol"} unless $symbol =~ s/^(\W)//; my $type = $1; return $type eq '*' ? *{"${class}::$symbol"} : $type eq "\$" ? \${"${class}::$symbol"} : $type eq '%' ? \%{"${class}::$symbol"} : $type eq '@' ? \@{"${class}::$symbol"} : $type eq '&' ? \&{"${class}::$symbol"} : do { Carp::croak("Can\'t find symbol: $type$symbol") }; } else { undef; } } sub DESTROY { print STDERR "destroy(@_): ", ref $_[0], "\n" if DEBUG; delete_package(ref $_[0]); } # Purpose: validate the regexp and replace "!" by "\!", and "/" by "\/" # if not already escaped # Arguments: a regexp # Returns: the preprocessed regexp sub ppregexp { # my $self = $_[0]; # useless my $regexp = $_[1]; eval { '' =~ /$regexp/ }; if ($@) { $@ =~ s/\s+at\s+[^\s]+\s+line\s+\d+[.]\n$//; # annoying info Carp::croak $@; } for ($regexp) { s{ ( (?: \G | [^\\] ) (?: \\{2} )* ) # even number of back-slashes ( [!/\"] ) # used delimiters }{$1\\$2}xg; # replace back exceptions (?!...), (?getPart($part); } $text; } sub setPart { my $self = shift; my $part = shift; my $class = ref $self || $self; no strict 'refs'; ${"${class}::template"}{$part} = shift; } $Parse::Template::CONFESS = 1; my $Already_shown = 0; my $__DIE__ = sub { if (not($Parse::Template::CONFESS) and $Already_shown) { # Reset when the eval() processing is finished $Already_shown = 0 if defined($^S); return; } # evaluated expressions are not always available in (caller(1))[6]; if (defined($1) and $1 ne '') { my $expr = $1; # what is the template expression? { package DB; # what is the part name? @DB::caller = caller(1); @DB::caller = caller(2) unless @DB::args; }; #local $1; $expr =~ s/package\s+${PACKAGE}::\w+\s*;//o; my $line = 0; $expr =~ s/^/sprintf "%2s ", ++$line/egm; $expr =~ s/\n;$//; my $part = defined $DB::args[1] ? $DB::args[1] : ''; if ($Already_shown) { print STDERR "call from part '$part':\n$expr\n"; } else { print STDERR "Error in part '$part':\n$expr\n"; } } else { print STDERR "\$1 not defined"; } print STDERR "\$1: $1\n"; # ignore Already_shown if you won't confess your exception $Already_shown = 1 unless $Parse::Template::CONFESS; }; $Parse::Template::SIG{__WARN__} = sub { # don't know how to suppress this: print STDERR "$_[0]" unless ($_[0] =~ /^Use of uninitialized value in substitution iterator/) }; use constant EVAL_TRACE => 0; use constant SHOW_PART => 0; use constant SIGN_PART => 0; $Parse::Template::SIGN_START = "# Template %s {\n"; # not documented $Parse::Template::SIGN_END = "# } Template %s\n"; # not documented my $indent = 0; my @part = (); sub eval { print STDERR do { local $" = q!', '! ; '..' x ++$indent, "=>eval('@_')\n" } if EVAL_TRACE; my $self = shift; my $part = shift; # can't declare $part in eval() push @part, $part; my $class = ref $self || $self; my $text = $self->getPart($part); print STDERR qq!$part content: $text\n! if SHOW_PART; if (SIGN_PART) { # not documented $text =~ s!^!sprintf $Parse::Template::SIGN_START, $part!e; $text =~ s!$!sprintf $Parse::Template::SIGN_END, $part!e; } local $SIG{__DIE__} = $__DIE__; # eval expression in class $text =~ s( %% (.*?) %% ){ # the magical substitution print STDERR '..' x $indent, "Eval part name: $part\n" if EVAL_TRACE; print STDERR '..' x $indent, " expr: package $class;\n$1\n" if EVAL_TRACE; "package $class; $1"; }eegsx; print STDERR "after: $class - $1\n" if EVAL_TRACE; die "$@" if $@; # caught by __DIE__ pop @part; $part = $part[-1]; --$indent if EVAL_TRACE; $text; } 1; __END__ =head1 NAME Parse::Template - Processor for templates containing Perl expressions =head1 SYNOPSIS use Parse::Template; my %template = ( 'TOP' => q!Text before %%$self->eval('DATA')%% text after!, 'DATA' => q!Insert data: ! . q!1. List: %%"@list$N"%%! . q!2. Hash: %%"$hash{'key'}$N"%%! . q!3. File content: %%%%! . 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' => q!It\'s an hash value! }); print $tmplt->eval('TOP'), "\n"; =head1 DESCRIPTION The C class evaluates Perl expressions placed within a text. This class can be used as a code generator, or a generator of documents in various document formats (HTML, XML, RTF, etc.). The principle of template-based text generation is simple. A template consists of a text which includes expressions to be evaluated. Interpretation of these expressions generates text fragments which are substituted in place of the expressions. In the case of C the expressions to be evaluated are Perl expressions placed within two C<%%>. Evaluation takes place within an environment in which, for example, you can place data structures which will serve to generate the parts to be completed. TEMPLATE Text + Perl Expression | +-----> Evaluation ----> Text(document or program) | Subs + Data structures ENVIRONMENT The C class permits decomposing a template into parts. These parts are defined by a hash passed as an argument to the class constructor: CEC. Within a part, a sub-part can be included by means of an expression of the form: $self->eval('SUB_PART_NAME') C<$self> designates the instance of the C class. In an expression you can also use the C<$part> which contains the part of the template where the expression is found. Within an expression it is possible to specify only the name of a part to be inserted. In this case a subroutine with the name of this part is generated dynamically. In the example given in the synopsis, the insertion of the C part can thus be rewritten as follows: 'TOP' => q!Text before %%DATA()%% text after! C is placed within C<%%> and is in effect treated as an expression to be evaluated. The subroutines take arguments. In the following example, the argument is used to control the depth of recursive calls of a template: print Parse::Template->new( 'TOP' => q!%%$_[0] < 10 ? '[' . TOP($_[0] + 1) . ']' : ''%%! )->eval('TOP', 0); C<$_[0]> initially contains 0. C is included as long as the argument is less than 10. For each inclusion, 1 is added to the argument. The C method permits constructing the environment required for evaluation of a template. Each entry to be defined within this 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 entry to be created within this environment (for example, a reference to an array to create an array). A scalar variable is defined by associating the name of the variable with its value. A scalar variable containing a reference is defined by writing C<'var'=>EC<\$variable>, where C<$variable> is a lexical variable that contains the reference. Each instance of C is defined within a specific class, a subclass of C. The subclass contains the environment specific to the template and inherits methods from the C class. If a template is created from an existing template (i.e. calling C as a method of the existing template), it inherits all the parts defined by its ancestor. In case of a syntax error in the evalutaion of an expression, C tries to indicate the template part and the expression that is "incriminated". If the variable C<$Parse::Template::CONFESS> contains the value TRUE, the stack of evaluations is printed. =head1 METHODS =over 4 =item new HASH Constructor for the class. C is a hash which defines the template text. Example: use Parse::Template; $t = new Parse::Template('key' => 'associated text'); =item env HASH =item env SYMBOL Permits defining the environment that is specific to a template. C returns the reference associated with the symbol, or C if the symbol is not defined. The reference that is returned is of the type indicated by the character (C<&, $, %, @, *>) that prefixes the symbol. Examples: $tmplt->env('LIST' => [1, 2, 3])} Defines a list @{$tmplt->env('*LIST')} Returns the list @{$tmplt->env('@LIST')} Ditto =item eval PART_NAME Evaluates the template part designated by C. Returns the string resulting from this evaluation. =item getPart PART_NAME Returns the designated part of the template. =item ppregexp REGEXP Preprocesses a regular expression so that it can be inserted into a template where the regular expression delimiter is either a "/" or a "!". =item setPart PART_NAME => TEXT C permits defining a new entry in the hash that defines the contents of the template. =back =head1 EXAMPLES The C class can be used in all sorts of amusing ways. Here are a few illustrations. =head2 HTML Generator The first example shows how to generate an HTML document by using a data structure placed within the evaluation environment. The template consists of two parts, C and C
. The C
part is called within the C part to generate as many sections as there are elements in the array C. my %template = ('DOC' => <<'END_OF_DOC;', 'SECTION' => <<'END_OF_SECTION;'); %% my $content; for (my $i = 0; $i <= $#section_content; $i++) { $content .= SECTION($i); } $content; %% END_OF_DOC; %% $section_content[$_[0]]->{Content} =~ s/^/

/mg; join '', '

', $section_content[$_[0]]->{Title}, '

', $section_content[$_[0]]->{Content}; %% END_OF_SECTION; my $tmplt = new Parse::Template (%template); $tmplt->env('section_content' => [ { Title => 'First Section', Content => 'Nothing to write' }, { Title => 'Second section', Content => 'Nothing else to write' } ] ); print $tmplt->eval('DOC'), "\n"; =head2 HTML generation using functional notation The second example shows how to generate an HTML document using a functional notation, in other words, obtaining the text:

text in boldtext in italic

from: P(B("text in bold"), I("text in italic")) The functions P(), B() and I() are defined as parts of a template. The Perl expression that permits producing the content of an element is very simple, and reduces to: join '', @_ The content to be evaluated is the same regardless of the tag and can therefore be placed within a variable. We therefore obtain the following template: my $ELT_CONTENT = q!%%join '', @_%%!; my $HTML_T1 = new Parse::Template( 'DOC' => '%%P(B("text in bold"), I("text in italic"))%%', 'P' => qq!

$ELT_CONTENT

!, 'B' => qq!$ELT_CONTENT!, 'I' => qq!$ELT_CONTENT!, ); print $HTML_T1->eval('DOC'), "\n"; We can go further by making use of the C<$part> variable, which is defined by default in the environment of evaluation of the template: my $ELT_CONTENT = q!%%"<$part>" . join('', @_) . ""%%!; my $HTML_T2 = new Parse::Template( 'DOC' => '%%P(B("text in bold"), I("text in italic"))%%', 'P' => qq!$ELT_CONTENT!, 'B' => qq!$ELT_CONTENT!, 'I' => qq!$ELT_CONTENT!, ); print $HTML_T2->eval('DOC'), "\n"; Let's look at another step which automates the production of expressions from the list of HTML tags which are of interest to us: my $DOC = q!P(B("text in bold"), I("text in italic"))!; my $ELT_CONTENT = q!%%"<$part>" . join('', @_) . ""%%!; my $HTML_T3 = new Parse::Template( 'DOC' => qq!%%$DOC%%!, map { $_ => $ELT_CONTENT } qw(P B I) ); print $HTML_T3->eval('DOC'), "\n"; To benefit from the possibility of using the template parts as procedures, we can inherit from the generated template class: use Parse::Template; my $ELT_CONTENT = q!%%"<$part>" . join('', @_) . ""%%!; my $G = new Parse::Template( map { $_ => $ELT_CONTENT } qw(H1 B I) ); @main::ISA = ref($G); *AUTOLOAD = \&Parse::Template::AUTOLOAD; print H1(B("text in bold"), I("text in italic")); The reference to C avoids the warning message: Use of inherited AUTOLOAD for non-method %s() is deprecated Not very elegant. =head2 HTML generation by method call With a slight transformation it is possible to use a method-invocation notation: my $ELT_CONTENT = q!%%shift(@_); "<$part>" . join('', @_) . ""%%!; my $HTML_T4 = new Parse::Template( map { $_ => $ELT_CONTENT } qw(P B I) ); print $HTML_T4->P( $HTML_T4->B("text in bold"), $HTML_T4->I("text in italic") ), "\n"; The C permits getting rid of the template object, which we don't need within the expression. =head2 Inheritance of parts In the following example the child template C<$C> inherits the parts defined in its parent template C<$A>: my %ancestor = ( 'TOP' => q!%%"Use the $part model and -> " . CHILD()%%!, 'ANCESTOR' => q!ANCESTOR %%"'$part' part\n"%%!, ); my %child = ( 'CHILD' => q!CHILD %%"'$part' part"%% -> %%ANCESTOR() . "\n"%%!, ); my $A = new Parse::Template (%ancestor); my $C = $A->new(%child); print $C->TOP(); The part defined in C<$A> can be called directly from C<$C>, that derives from C<$A>. =head2 Other examples C was initially created to serve as a code generator for the C class. You will find other examples of its use in the classes C, C and C. =head1 NOTES CONCERNING THE CURRENT VERSION I would be very interested to receive your comments and suggestions. =head1 BUGS Instances are not destroyed. Therefore, do not use this class to create a large number of instances. =head1 AUTHOR Philippe Verdret (with translation of documentation into English by Ocrat) =head1 COPYRIGHT Copyright (c) 1995-2001 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. =cut ParseTemplate-3.08/t/000075500000000000000000000000001202355537000144555ustar00rootroot00000000000000ParseTemplate-3.08/t/W.pm000064400000000000000000000144531202355537000152300ustar00rootroot00000000000000# Examples: # make test TEST_FILES=t/test1.t TEST_VERBOSE=2 # verbose levels: # 1 : print configuration and major operations # 2 : more details # 3 : print the execution result require 5.004; use strict; package W; # Test::Wrapper use vars qw($VERBOSE $LOG); $W::VERSION = '3.08'; $W::VERBOSE = $ENV{TEST_VERBOSE} || 0; $W::LOG = $ENV{TEST_LOG} ? 'testlog' : 0; if ($LOG) { if (open(LOG, ">>$LOG")) { print STDERR "see informations in the '$LOG' file\n"; } else { warn "unable to open '$LOG' ($!)"; $LOG = ''; } } sub new { my $self = shift; my $class = ref $self || $self; my $param = shift; my $range = ''; if (defined $param) { unless (ref($param) eq 'HASH') { $param = { Range => $param, PerlOpts => @_ ? shift : '', Program => @_ ? shift : '', }; } } else { # defaults $param = { Range => '1..1', PerlOpts => '', Program => '', }; } print "\n"; print "Verbosity level: $VERBOSE\n" if $VERBOSE; print "$param->{Range}\n"; print "Program to test: $param->{Program}\n" if $VERBOSE; print "Perl Options: $param->{PerlOpts}\n" if $VERBOSE; bless $param, $class; } sub result { my $self = shift; my $cmd = shift; my @result; my @err; my $result; if ($cmd) { my $popts = $self->{PerlOpts}; print "Execution of: $^X -Iblib/lib $popts $cmd\n" if $VERBOSE; die "unable to find '$cmd'" unless -f $cmd; # the following line doesn't work on Win95 (ActiveState's Perl, build 516): # open( CMD, "$^X -Iblib/lib $cmd 2>err |" ) or warn "$0: Can't run. $!\n"; # corrected by Stefan Becker: local *SAVED_STDERR; #local $| = 1; open( SAVED_STDERR, ">&STDERR" ); open( STDERR, "> err" ) or warn "$0: can't open 'err'"; open( CMD, "$^X -Iblib/lib $popts $cmd |" ) or warn "$0: Can't run '$^X -Iblib/lib $popts $cmd' ($!)\n"; @result = ; close CMD; close STDERR; open(STDERR, ">&SAVED_STDERR"); if (open( CMD, "< err" )) { @err = ; close CMD; } else { warn "$0: Can't open 'err' ($!)\n"; } push @result, @err if @err; $self->{Result} = join('', @result); if ($LOG) { print LOG "=" x 80, "\n"; print LOG "Execution of $^X -Iblib/lib $popts $cmd 2>err\n"; print LOG "=" x 80, "\n"; print LOG "* Result:\n"; print LOG "-" x 80, "\n"; print LOG $self->{Result}; } if ($VERBOSE > 2) { print $self->{Result}; } } else { $self->{Result}; } } sub expected { my $self = shift; my $ref = shift; if ($ref) { if (fileno $ref) { $self->{Expected} = join('', <$ref>); } else { $self->{Expected} = $ref; } if ($LOG) { print LOG "-" x 80, "\n"; print LOG "* Expected:\n"; print LOG "-" x 80, "\n"; print LOG $self->{Expected}; } } else { $self->{Expected}; } } sub assert { my $self = shift; my $onwhat = shift; my $regexp = @_ ? shift : die "regexp not defined\n"; if ($self->{$onwhat} !~ /$regexp/) { die "'$regexp' doesn't match $onwhat string"; } } sub report { my $self = shift; my $label = shift; my $sub = shift; unless (ref $sub eq 'CODE') { die "'$sub' not a coderef"; } my $s = $self->$sub(@_); $s ? "ok $label\n" : "not ok $label\n"; } my $delim_start = ">>>>\n"; my $delim_end = "\n<<<<"; # W->new()->detector("abc", "acv"); sub detector { my $self = shift; my $s1 = shift; my $s2 = shift; #print STDERR length($s1), "\n"; #print STDERR length($s2), "\n"; my ($c1, $c2); my $l = 1; while ( ($s1 =~ /\G(.)/gc) or (($s1 =~ /\G(.)/gcs) and $l++) ) { $c1 = $1; $s2 =~ /(.)/gs; $c2 = $1; #$c1 = '\n' if $c1 =~ /\n/; #$c2 = '\n' if $c2 =~ /\n/; #print STDERR "|$c1|$c2|\n"; unless ($c1 eq $c2) { print STDERR "At line: $l\n"; print STDERR ">>>", substr($s1, pos($s1) - 1, 20), "\n"; print STDERR ">>>", substr($s2, pos($s2) - 1, 20), "\n"; return 1; } } if (my $rest = substr($s2, pos($s2))) { print STDERR ">>>$rest\n"; } return 0; } sub comparator { my $self = shift; my $detector = @_ && defined $_[0] ? shift : $self->can('detector'); my $red = @_ ? shift : ''; # edit the result my $eed = @_ ? shift : ''; # edit the reference #print STDERR "->$red<-$eed<\n"; my $expected = $self->expected; my $result = $self->result; $expected =~ s:\n+$::; $result =~ s:\n+$::; # could be a specific editor $expected =~ s/$eed/(...deleted...)/g unless $eed eq ''; $result =~ s/$red/(...deleted...)/g unless $red eq ''; # ignore whitespace differences (my $expected_cmp = $expected) =~ s/\s+/ /g; $expected_cmp =~ s/\s+$//; (my $result_cmp = $result) =~ s/\s+/ /g; $result_cmp =~ s/\s+$//; if ($VERBOSE || $expected_cmp ne $result_cmp) { print STDERR "\n"; print STDERR ">>>Expected:\n$expected\n"; print STDERR ">>>Effective:\n$result\n"; } unless ($expected_cmp eq $result_cmp) { print STDERR "not equals\n" if $VERBOSE; if ($VERBOSE >= 2 and defined $detector) { print STDERR "Difference between expected and effective result: \n"; $self -> $detector($expected, $result); } elsif ($VERBOSE) { } 0; } else { print STDERR "equals\n" if $VERBOSE; 1; } } # todo: defined named parameters sub test { my $self = shift; my $label = @_ ? shift : 1; # specific label for the test # or see $self->{Program} my $prog_to_test = @_ ? shift : undef; # filename of the program to test my $reference = @_ ? shift : undef; # string or filehandle my $comparator = @_ ? shift : undef; # sub, compare result with a ref and say yes or no my $detector = @_ ? shift : undef; # sub, localize the first difference my $r_ed = @_ ? shift : ''; # regexp for editing the effective result my $e_ed = @_ ? shift : ''; # regexp for editing the expected result $self->result("$prog_to_test") if defined $prog_to_test; $self->expected($reference) if defined $reference; $comparator = $self->can('comparator') unless defined $comparator; $detector = $self->can('detector') unless defined $detector; $self->report($label, $comparator, $detector, $r_ed, $e_ed); } "End of Package" ParseTemplate-3.08/t/debug.t000075500000000000000000000005701202355537000157350ustar00rootroot00000000000000#!perl use strict; use warnings; use Test::More; use_ok 'Parse::Template'; is Parse::Template->DEBUG, 0, "DEBUG is off"; is Parse::Template->TRACE_ENV, 0, "TRACE_ENV is off"; is Parse::Template->EVAL_TRACE, 0, "EVAL_TRACE is off"; is Parse::Template->SHOW_PART, 0, "SHOW_PART is off"; is Parse::Template->SIGN_PART, 0, "SIGN_PART is off"; done_testing(); ParseTemplate-3.08/t/rt_58128.t000075500000000000000000000036061202355537000160460ustar00rootroot00000000000000#!perl use strict; use warnings; use Test::More; use_ok 'Parse::Template'; # RT#58128 check is Parse::Template->ppregexp('blarg(?!blarg)'), 'blarg(?!blarg)', "RT#58128"; is Parse::Template->ppregexp('blarg(?ppregexp('(') }; like $@, qr/^Unmatched \( .*?\/ at t.rt_58128\.t line \d+\.?\n/, "badly formed"; is Parse::Template->ppregexp('[a-z]'), '[a-z]', "normal"; is Parse::Template->ppregexp('!1'), '\\!1', "! not escaped"; is Parse::Template->ppregexp('/1'), '\\/1', "/ not escaped"; is Parse::Template->ppregexp('"1'), '\\"1', "\" not escaped"; is Parse::Template->ppregexp('\\!1'), '\\!1', "! escaped - no change"; is Parse::Template->ppregexp('\\/1'), '\\/1', "/ escaped - no change"; is Parse::Template->ppregexp('\\"1'), '\\"1', "\" escaped - no change"; is Parse::Template->ppregexp('\\\\!1'), '\\\\\\!1', "! not escaped preceeded by double backslash - escape"; is Parse::Template->ppregexp('\\\\/1'), '\\\\\\/1', "/ not escaped preceeded by double backslash - escape"; is Parse::Template->ppregexp('\\\\"1'), '\\\\\\"1', "\" not escaped preceeded by double backslash - escape"; is Parse::Template->ppregexp('\\\\\\!1'), '\\\\\\!1', "! escaped preceeded by double backslash - no change"; is Parse::Template->ppregexp('\\\\\\/1'), '\\\\\\/1', "/ escaped preceeded by double backslash - no change"; is Parse::Template->ppregexp('\\\\\\"1'), '\\\\\\"1', "\" escaped preceeded by double backslash - no change"; is Parse::Template->ppregexp('\\\\\\\\!1'), '\\\\\\\\\\!1', "! escaped preceeded by quad backslash - escape"; is Parse::Template->ppregexp('\\\\\\\\/1'), '\\\\\\\\\\/1', "/ escaped preceeded by quad backslash - escape"; is Parse::Template->ppregexp('\\\\\\\\"1'), '\\\\\\\\\\"1', "\" escaped preceeded by quad backslash - escape"; done_testing(); ParseTemplate-3.08/t/test1.t000075500000000000000000000004571202355537000157130ustar00rootroot00000000000000#!/usr/local/bin/perl BEGIN { push(@INC, './t') } # where is W.pm use W; print W->new()->test('test1', "examples/synopsis.pl", *DATA); __END__ Text before Inserted part from SUB_PART(1) 1. List: 1 2 10 2. Hash: It\'s an hash value 3. Sub: arguments: 1 2 3 soleil Text after ParseTemplate-3.08/t/test2.t000075500000000000000000000004401202355537000157040ustar00rootroot00000000000000#!/usr/local/bin/perl BEGIN { push(@INC, './t') } # where is W.pm use W; print W->new()->test('test2', "examples/html_template.pl", *DATA); __DATA__

First Section

Nothing to write

Second section

Nothing else to write ParseTemplate-3.08/t/test3.t000075500000000000000000000004371202355537000157130ustar00rootroot00000000000000#!/usr/local/bin/perl BEGIN { push(@INC, './t') } # where is W.pm use W; print W->new()->test("test3", "examples/derived.pl", *DATA); __DATA__ ANCESTOR template: 'TOP' part -> CHILD template: 'CHILD' part -> PARENT template: 'PARENT' part -> ANCESTOR template: 'ANCESTOR' part ParseTemplate-3.08/t/test4.t000075500000000000000000000002551202355537000157120ustar00rootroot00000000000000#!/usr/local/bin/perl BEGIN { push(@INC, './t') } # where is W.pm use W; print W->new()->test('test4', "examples/recursive.pl", *DATA); __DATA__ [[[[[[[[[[]]]]]]]]]] ParseTemplate-3.08/t/test5.t000075500000000000000000000010051202355537000157050ustar00rootroot00000000000000#!/usr/local/bin/perl BEGIN { push(@INC, './t') } # where is W.pm use W; print W->new()->test('test5', "examples/html_generator.pl", *DATA); __DATA__

A very simple document:

  1. 0
    1. 1
      1. 2
        1. 3
        2. 3
      2. 2
    2. 1
  2. 0

text in boldtext in italic

text in boldtext in italic

text in boldtext in italic

text in boldtext in italic

ParseTemplate-3.08/t/test6.t000075500000000000000000000004571202355537000157200ustar00rootroot00000000000000#!/usr/local/bin/perl BEGIN { push(@INC, './t') } # where is W.pm use W; print W->new()->test('test6', "examples/delegation2.pl", *DATA); __DATA__ $A->ANCESTOR(): ANCESTOR/ANCESTOR ->ANCESTOR/ SUB_PART $C->CHILD(): CHILD/ CHILD -> ANCESTOR/ANCESTOR ->CHILD/ SUB_PART $C->SUB_PART(): CHILD/ SUB_PART