http://www.perlmonks.org?node_id=72504


in reply to fixstyle script in PERL COOKBOOK

That is odd...
#!/usr/bin/perl $string = "a\@b.c\n"; $string =~ s#\Qa@b.c\E#email-address#g; print $string; $string2 = "a\@b.c\n"; $string2 =~ s#a\@b\.c#email-address#g; print $string2; $string3 = "a\@b.c\n"; $qm3 = quotemeta('a@b.c'); $string3 =~ s#$qm3#email-address#g; print $string3;
Output:
a@b.c email-address email-address
So it seems that method won't work correctly. Perhaps another Monk can enlighten.

Corion suggests that \Q$var\E should work as normal; perhaps change the line that reads:
$code .= "s{\\Q$in\\E}{$out}g"
to read:
$code .= "\$var=quotemeta q{$in}; s{\$var}{$out}g"
for all to be well with the world. And it appears to, as well.

Update:D'oh, messed up the replacement line,fixed
Update:Corion once again points out that the reason for this is because @b gets interpolated before \Q takes effect. I should have realized this was the case.

Replies are listed 'Best First'.
Re: Re: fixstyle script in PERL COOKBOOK
by Anonymous Monk on Apr 20, 2001 at 23:05 UTC
    Thanks a lot !! It works... But I also had to escape the @ in the output. By the way, I don't know what \Q \E means... could you tell me why? Thanks...
      They are explained in perlre.

      What \Q does is disable a lot of the "metacharacters" in REs until the next \E. This is often important when substituting variables in an RE because you have little control over the contents of the variable.

      An alternate solution is to quote all possible meta characters with quotemeta.