Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

s/$foo/$bar/ when $foo has metacharacters in it

by bcrowell2 (Friar)
on Dec 20, 2007 at 22:26 UTC ( #658303=perlquestion: print w/replies, xml ) Need Help??
bcrowell2 has asked for the wisdom of the Perl Monks concerning the following question:

I often want to do something like $a =~ s/$b/$c/g , where $b is a variable that may have regex metacharacters in it. Here's an example of the problem:
my $a = "$"; my $b = "$"; my $c = "x"; $a =~ s/$b/$c/g; print "a=$a\n";
This results in an error, because the string $b has a regex metacharacter, $, in it. Here's the alternative I've come up with so far:
# conceptually, the following is like $a=~s/$b/$c/, but is safe if $b +has metacharacters in it sub string_substitution_one { my $a_ref = shift; my $b = shift; my $c = shift; my $a = $$a_ref; my $debug = ($b eq '$m$'); my $i=index($a,$b); return 0 if $i == -1; my $z = ''; if ($i>0) {$z = $z . substr($a,0,$i)} $z = $z . $c; if ($i+length($b)<length($a)) {$z = $z . substr($a,$i+length($b),len +gth($a)-($i+length($b)))} $$a_ref = $z; return 1; } # conceptually, the following is like $a=~s/$b/$c/g, but is safe if $b + has metacharacters in it sub string_substitution_global { my $a_ref = shift; my $b = shift; my $c = shift; my $found = 0; while (string_substitution_one($a_ref,$b,$c)) { $found = 1; } return $found; }
Is there a simpler way to do this? I'm thinking of making this into a module, because I have several different programs that use it, but it seems like there must be an easier way to do such a common task. TIA!

Replies are listed 'Best First'.
Re: s/$foo/$bar/ when $foo has metacharacters in it
by FunkyMonk (Canon) on Dec 20, 2007 at 22:30 UTC
    You can use quotemeta, either as a function, or as \Q...\E (update: Relashed my slashes, thanks ikegami):
    $b = quotemeta $b; $a =~ s/$b/$c/g; #or $a =~ s/\Q$b/$c/g; # \E not needed at end of pattern

      Excellent -- thanks!
Re: s/$foo/$bar/ when $foo has metacharacters in it
by sh1tn (Priest) on Dec 20, 2007 at 22:44 UTC
    In addition: perldoc -q quote and quote.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://658303]
Approved by ikegami
[Cosmic37]: is anyone doing machine learning with Perl?
[Cosmic37]: I was using gneural code in C and it was interesting but did not have back propagation for neural networks
[corenth]: Cosmic, poking at it, but not really.
[Cosmic37]: I read that its possible to use the FANN library with Perl so I might try that now...
[corenth]: i have a question. I used map{ blah();blah(); blah();}@stuff; and it used up a ton of memory vs. the for(@stuff){} equivalent. What gives? (if anyone knows)
[Cosmic37]: FANN also has LGPL license which I like and its supposed to be quite a capable library from what I read
[corenth]: Cosmic, that sounds interesting. What is FANN (I could search it if I weren't so lazy about it)?
[Cosmic37]: how big was @stuff corenth?

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2018-02-20 18:15 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (274 votes). Check out past polls.