in reply to multiple (different) substitutions on same $foo
foreach (qq/frog toad/, qq/man boy/, qq/woman girl/) { my ( $pattern, $substitution) = split /\s/, $_; $foo =~ s/\Q$pattern\E/$substitution/g; }
Another way would be like this:
my %testhash = ( qw/frog toad man boy woman girl/ ); foreach (keys %testhash) { $foo =~ /\Q$_\E/$testhash{$_}/; }
Note that I added "\Q" and "\E" to both regexps. This is to ensure that the variable being interpolated into the regexp doesn't have any possible metacharacters interpreted in the regexp. So if you happen to be testing 'C:\data' (a lousy example, but stay with me), the regexp engine won't see it as 'C:[0-9]ata'.
In your example, the \Q and \E quotemeta function isn't needed. And there are plenty of cases where its function is undesirable (for example, what if you WANT 'C:\data' to be interpreted as 'C:[0-9]ata'), but I included them in my example to ensure that you are matching in the most simple way.
I don't see any way of just having one iteration of $foo =~ s/// do it all though.
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|
|---|