cowgirl has asked for the wisdom of the Perl Monks concerning the following question:
I was wondering how to do replace certain denoted names dynamically in a template file, such as in this context: <b><u><i>$var</i></u></b> and replacing this with the value of $var. I would think this would be possible by using a reference to the regexp match in a regexp like: $string = s/\$XXXXXXXX/${$1}/; Of course this does not work, I do not know what to replace XXXXXXXX with and the $1 variable would return the entire variable name, however we need it without the $ sign. This must be really easy. I would love if someone could help me out without pointing to the use of a module. Thanks a lot!sub prog_replace_string { my $replace_this = shift; my $with_this = shift; my $string = shift; my $length = length($string); my $target = length($replace_this); for(my $i=0; $i<$length - $target + 1; $i++) { if(substr($string,$i,$target) eq $replace_this) { $string = substr($string,0,$i) . $with_this . substr($stri +ng,$i+$target); } } return $string; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replacing string with regexp without using modules
by pc88mxer (Vicar) on Aug 05, 2008 at 22:51 UTC | |
by cowgirl (Acolyte) on Aug 06, 2008 at 00:37 UTC | |
by almut (Canon) on Aug 06, 2008 at 01:58 UTC |