> Nope, I don't think so. $text is being munged each time around until it
> shrinks to zero length and drops out the bottom of the while loop.
>
> Or have I missed something?
You are absolutely right - forgot to view that it is actually munging the
$text string.
Here is another code snippet that you might consider. It has the unfortunate
item of needing to be copied twice for global. But it does handle $10. It
also handles $10 more similarly to Perl. The problem is that if I have $10
in my string - but I have less than 10 patterns, then I will end up with
"mymatch0" rather than "" should $1 contain mymatch. Also, it should be
decided about what to do with $1 variables in the replacement portion should
there not be any matches. The version you had given leaves them there -
though I don't think that is desired behavior.
Any way - here is a quick code snippet - that I think I may use in some of my
applications.
sub vmethod_replace {
my ($str, $pat, $replace, $global) = @_;
$str = '' if ! defined $str;
$pat = '' if ! defined $pat;
$replace = '' if ! defined $replace;
$global = 1 if ! defined $global;
if ($global) {
$str =~ s{$pat}{
my @start = @-;
my @end = @+;
my $copy = $replace;
$copy =~ s{ (?<!\\) \$ (\d+) }{
($1 > $#start || $1 == 0) ? '' : substr($str, $start[$1],
$end[$1] - $start[$1]);
}exg;
$copy;
}eg;
} else {
$str =~ s{$pat}{
my @start = @-;
my @end = @+;
my $copy = $replace;
$copy =~ s{ (?<!\\) \$ (\d+) }{
($1 > $#start || $1 == 0) ? '' : substr($str, $start[$1],
$end[$1] - $start[$1]);
}exg;
$copy;
}e;
}
return $str;
}
Paul
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates