What I want to do
I have a set of substitutions where both the pattern and the replacements are read from a config file at run time. Handling the patterns with qr// is no sweat, but the replacements pose more of a challenge, specifically in dealing with capture variables ($1 and friends).
What I've tried
use strict;
use warnings;
my $repl = '$1';
{
print "attempt 1 ";
my $str = "a,A,c,d";
$str =~ s/([a-z]),/$repl/ig;
print "yields: $str\n";
}
{
no strict 'refs';
print "attempt 2 ";
my $str = "a,A,c,d";
$str =~ s/([a-z]),/$$repl/ig;
print "yields: $str\n";
}
{
print "attempt 3 ";
my $str = "a,A,c,d";
$str =~ s/([a-z]),/eval "$repl"/ieg;
print "yields: $str\n";
}
my $code = 'sub {$_[0] =~ s/([a-z]),/'. $repl . '/ig}';
my $re = eval ($code);
if ($@) { print "eval failed: $@" }
{
print "attempt 4 ";
my $str = "a,A,c,d";
$re->($str);
print "yields: $str\n";
}
__END__
attempt 1 yields: $1$1$1d
Use of uninitialized value in substitution (s///) at zwomp.pl line 17.
attempt 2 yields: d
attempt 3 yields: aAcd
attempt 4 yields: aAcd
Attempts 3 and 4 work correctly. 3 is useless to me because this will be in a long running loop, and my goal is to expunge string evals from the loop. 4 is passable, since the eval can be done outside of the loop. That being said, 2 looks like it should work, or could be made to work, but I can't get it to. The uninitialized value warning leads me to think it's grabbing $1 from outside the s//, but I'm not really sure how to correct for this. Does anyone have any advice, or any other thoughts on ways to approach this problem?
For the curious, why I'm doing this
I'm building a text transformation engine, not entirely unlike a very simple parser (much too simple for Parse::RecDescent to be helpful). In essence, it has a collection of transformations which it applies to named input "tokens" to get named output "tokens". For example, it might have a rule to the effect of
URL:
domain:
match: http://(\w+)\.(\w+)\.
replace: $2
which should be read (in some kind of pseudo-code) as:
apply s/http://(\w+)\.(\w+)\./$2/ to 'URL' to produce 'domain'
The transformations themselves come out of a config file because several different languages need to implement a consistent set of transformations. This way, as long as each language has a reliable way of reading the input file and generating translation rules, and changes to the rules are made only in the config file, new rules or modifications will be applied in all places automatically. The goal is to prevent inconsistency between the different (language) translation processes.
Any thoughts on this would be greatly appreciated.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.