Template::Extract - Use TT2 syntax to extract data from documents
This document describes version 0.38 of Template::Extract, released October 25, 2004.
use Template::Extract; use Data::Dumper; my $obj = Template::Extract->new; my $template = << '.'; <ul>[% FOREACH record %] <li><A HREF="[% url %]">[% title %]</A>: [% rate %] - [% comment %]. [% ... %] [% END %]</ul> . my $document = << '.'; <html><head><title>Great links</title></head><body> <ul><li><A HREF="http://slashdot.org">News for nerds.</A>: A+ - nice. this text is ignored.</li> <li><A HREF="http://microsoft.com">Where do you want...</A>: Z! - yeah. this text is ignored, too.</li></ul> . print Data::Dumper::Dumper( $obj->extract($template, $document) );
This module adds template extraction functionality to the Template toolkit. It can take a rendered document and its template together, and get the original data structure back, effectively reversing the Template::process function.
Template::process
Constructor. Currently takes no parameters.
This method takes three arguments: the template string, or a reference to it; a document string to match against; and an optional hash reference to supply initial values, as well as storing the extracted values into.
The return value is \%values upon success, and undef on failure. If \%values is omitted from the argument list, a new hash reference will be constructed and returned.
\%values
undef
Extraction is done by transforming the result from Template::Parser to a highly esoteric regular expression, which utilizes the (?{...}) construct to insert matched parameters into the hash reference.
(?{...})
The special [% ... %] directive is taken as the /.*?/s regex, i.e. ignore everything (as short as possible) between this identifier and the next one. For backward compatibility, [% _ %] and [% __ %] are also accepted.
[% ... %]
/.*?/s
[% _ %]
[% __ %]
The special [% // %] directive is taken as a non-capturing regex, embedded inside /(?:)/s; for example, [% /\d*/ %] matches any number of digits. Capturing parentheses may not be used with this directive.
[% // %]
/(?:)/s
[% /\d*/ %]
You may set $Template::Extract::DEBUG to a true value to display generated regular expressions.
$Template::Extract::DEBUG
The extraction process defaults to succeed even with a partial match. To match the entire document only, set $Template::Extract::EXACT to a true value.
$Template::Extract::EXACT
Use Template::Extract::Compile to perform the first phase of extract, by returning the regular expression compiled from $template.
extract
$template
Use Template::Extract::Run to perform the second phase of extract, by applying the regular expression on $document and returning the resulting \%values.
$document
Currently, the extract method only supports [% GET %], [% SET %] and [% FOREACH %] directives, because [% WHILE %], [% CALL %] and [% SWITCH %] blocks are next to impossible to extract correctly.
[% GET %]
[% SET %]
[% FOREACH %]
[% WHILE %]
[% CALL %]
[% SWITCH %]
[% SET key = "value" %] only works for simple scalar values.
[% SET key = "value" %]
Outermost [% FOREACH %] blocks must match at least once in the document, but inner ones may occur zero times. This is to prevent the regex optimizer from failing prematurely.
There is no support for different PRE_CHOMP and POST_CHOMP settings internally, so extraction could fail silently on extra linebreaks.
This module's companion class, Template::Generate, is still in early experimental stages; it can take data structures and rendered documents, then automagically generates templates to do the transformation. If you are into related research, please mail any ideas to me.
Template::Extract::Compile, Template::Extract::Run
Template, Template::Generate
Simon Cozens's introduction to this module, in O'Reilly's Spidering Hacks: http://www.oreillynet.com/pub/a/javascript/excerpt/spiderhacks_chap01/index.html
Mark Fowler's introduction to this module, in The 2003 Perl Advent Calendar: http://perladvent.org/2003/5th/
Autrijus Tang <autrijus@autrijus.org>
Copyright 2001, 2002, 2003, 2004 by Autrijus Tang <autrijus@autrijus.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install Template::Extract, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template::Extract
CPAN shell
perl -MCPAN -e shell install Template::Extract
For more information on module installation, please visit the detailed CPAN module installation guide.