String::MatchInterpolate can perform named regexp capture and variable interpolation from the same template.
SYNOPSIS
use String::MatchInterpolate;
my $smi = String::MatchInterpolate->new( 'My name is ${NAME/w+/}' );
my $vars = $smi->match( "My name is Bob" );
my $name = $vars->{NAME};
print $smi->interpolate( { NAME => "Jim" } ) . "n";
This module provides an object class which represents a string matching and interpolation pattern. It contains named-variable placeholders which include a regexp pattern to match them on. An instance of this class represents a single pattern, which can be matched against or interpolated into.
Objects in this class are not modified once constructed; they do not store any runtime state other than data derived arguments passed to the constructor.
Template Format
The template consists of a string with named variable placeholders embedded in it. It looks similar to a perl or shell string with interpolation:
A string here with ${NAME/pattern/} interpolations
The embedded variable is delmited by perl-style ${ } braces, and contains a name and a pattern. The pattern is a normal perl regexp fragment that will be used by the match() method. This regexp should not contain any capture brackets ( ) as these will confuse the parsing logic.
Outside of the embedded variables, the string is interpreted literally; i.e. not as a regexp pattern. A backslash may be used to escape the following character, allowing literal backslashes or dollar signs to be used.
The intended use for this object class is that the template strings would come from a configuration file, or some other source of "trusted" input. In the current implementation, there is nothing to stop a carefully-crafted string from containing arbitrary perl code, which would be executed every time the match() or interpolate() methods are called. (See "SECURITY" section). This fact may be changed in a later version.
Product's homepage
Requirements:
· Perl