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

hello, I am trying to replace variables in a string (indicated by /\$\w+/), but am for some reason unsucessfull. This will probbably illustrate the problem best:
DB<7> x $sql 0 'This string represents a SQL statement $replaceme' DB<8> x $what 0 '$replaceme' DB<9> x $with 0 'value' DB<10> $sql=~s/$what/$with/ DB<11> x $sql 0 'This string represents a SQL statement $replaceme'
What i am trying to achieve is
$sql eq 'This string represents a SQL statement value'
Any suggestions what i am doing wrong here?

Replies are listed 'Best First'.
Re: substitution op. (s///) won't work
by Abigail-II (Bishop) on Sep 30, 2003 at 08:45 UTC
    The problem is the content of the variable $what. It contains $replaceme, and for the regular expression engine, this means you want to replace a string 'replaceme' that follows the end of the string, because that's what $ means. Of course, there is no such string.

    You might want to use something like:

    $sql =~ s/\Q$what/$with/;

    Abigail