my $str = "abcdefghijafjafjkagjakg";
my $pat = '(a.)';
my $repl = '$1 ';
print munge($str,$pat,$repl);
=head2 munge( STRING, PATTERN, REPLACEMENT )
The munge function takes three arguments and returns a string.
The first argument is the STRING to be modified.
The modification is performed by s/PATTERN/REPLACEMENT/g.
The main advantage of this function is that REPLACEMENT
can contain $1, $2 etc that have been captured by PATTERN
These values are safely interpolated prior to the substitution
being made.
=cut
sub munge {
my($str, $pat, $repl) = @_;
$str =~ s/$pat/_safeswitch($repl)/eg;
return $str;
}
# used by munge function to safely interpolate
# $1, $2 etc into the replacement string
sub _safeswitch {
my @P = (undef,$1,$2,$3,$4,$5,$6,$7,$8,$9);
$_[0] =~ s/\$(\d)/$P[$1]/g;
$_[0];
}
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|