JTomb has asked for the wisdom of the Perl Monks concerning the following question:

I have the following:
$puid="fmt/412"; $file_path="C:\Service Record.docx";

I'm trying to substitute plain text for the variables as in:

$cont=~s/FilePath/\Q$file_path\E/g; $cont=~s/PUID/\Q$puid\E/g;

Problem is I end up with:

fmt\/412
and
C\:\\Service\ Record\.docx

From what I can tell, Perl wants to add escape characters, or something along those lines.

Is there a way to get around this?
Thanks!

Joe
  • Comment on Problem with variable that contains certain characters in replacement side of regex substitution.
  • Select or Download Code

Replies are listed 'Best First'.
Re: Problem with variable that contains certain characters in replacement side of regex substitution.
by choroba (Cardinal) on May 11, 2013 at 01:35 UTC
    Perl wants to add escape characters
    No, Perl does not want to, you tell it to do so by using \Q. \Q is usually only needed in the pattern, not in the replacement part of the substitution. Remove it and Perl will behave.

    BTW, backslash must be backslashed in double quotes.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks! That did it. Not sure why I thought to use \Q the way I did.

      Joe