CC: | lars [...] thegler.dk |
Subject: | [PATCH] Structured var with capturing regex |
Template::Extract supports structured vars ("[% a.b %]") and capturing
regexes ("[% a =~ /(\d+)/ %]"), but the two cannot currently be combined.
The attached patch plus test case fixes this.
/Lars
Subject: | Template-Extract-0.41.diff |
diff -ru Template-Extract-0.41-orig/lib/Template/Extract/Compile.pm Template-Extract-0.41/lib/Template/Extract/Compile.pm
--- Template-Extract-0.41-orig/lib/Template/Extract/Compile.pm 2007-10-15 19:52:35.000000000 +0200
+++ Template-Extract-0.41/lib/Template/Extract/Compile.pm 2009-06-03 14:52:16.000000000 +0200
@@ -26,7 +26,7 @@
$template =~ s/\[%\s*(?:\.\.\.|_|__)\s*%\]/[% \/.*?\/ %]/g;
$template =~ s/\[%\s*(\/.*?\/)\s*%\]/'[% "' . quotemeta($1) . '" %]'/eg;
$template =~ s{
- \[%\s*([a-zA-z0-9]+)\s*\=\~\s*(/.*?/)\s*%\]
+ \[%\s*([a-zA-z0-9\.]+)\s*\=\~\s*(/.*?/)\s*%\]
}{
'[% SET ' . $1 . ' = "' . quotemeta($2) . '" %]'
}mxegi;
diff -ru Template-Extract-0.41-orig/t/1-basic.t Template-Extract-0.41/t/1-basic.t
--- Template-Extract-0.41-orig/t/1-basic.t 2007-10-15 19:53:32.000000000 +0200
+++ Template-Extract-0.41/t/1-basic.t 2009-06-03 14:49:03.000000000 +0200
@@ -1,7 +1,7 @@
#!/usr/bin/perl
use strict;
-use Test::More tests => 15;
+use Test::More tests => 16;
use_ok('Template::Extract');
@@ -302,3 +302,11 @@
$data = Template::Extract->new->extract( $template, $document );
is_deeply( $data, { d => 'hello' }, 'capturing regex' );
+
+$document = << '.';
+<p>hello</p>
+<p>42</p>
+.
+$template = '<p>[% a.b =~ /(\d+)/ %]</p>';
+$data = Template::Extract->new->extract( $template, $document );
+is_deeply( $data, { a => { b => '42' }}, 'structured var with capturing regex' );