chaos_cat has asked for the wisdom of the Perl Monks concerning the following question:
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?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
which should be read (in some kind of pseudo-code) as:URL: domain: match: http://(\w+)\.(\w+)\. replace: $2
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.apply s/http://(\w+)\.(\w+)\./$2/ to 'URL' to produce 'domain'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamically generating the replacement part of a substitution
by CountZero (Bishop) on Oct 06, 2008 at 20:25 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by JavaFan (Canon) on Oct 06, 2008 at 17:41 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by Jenda (Abbot) on Oct 06, 2008 at 17:41 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by moritz (Cardinal) on Oct 06, 2008 at 17:03 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by Andrew Coolman (Hermit) on Oct 06, 2008 at 17:22 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by leocharre (Priest) on Oct 06, 2008 at 17:43 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by AnomalousMonk (Archbishop) on Oct 06, 2008 at 23:13 UTC | |
|
Re: Dynamically generating the replacement part of a substitution
by MadCat (Initiate) on Oct 09, 2008 at 05:18 UTC |