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

hello. i am trying to replace all of the " with \" in a variable called $foo. i am using
$foo=~s/\"/\\\"/g;
but this is just wiping out $foo. what am i doing wrong?

Replies are listed 'Best First'.
Re: I am having a problem with search and replace regarding "
by GrandFather (Saint) on May 30, 2006 at 02:29 UTC
    use strict; use warnings; my $str = 'A string with a "double quoted" string in it'; (my $quoted = $str) =~ s/\"/\\\"/g; print "$str\n$quoted\n";

    Prints:

    A string with a "double quoted" string in it A string with a \"double quoted\" string in it

    Where's the problem?


    DWIM is Perl's answer to Gödel
Re: I am having a problem with search and replace regarding "
by Joost (Canon) on May 30, 2006 at 11:06 UTC
Re: I am having a problem with search and replace regarding "
by merlyn (Sage) on May 30, 2006 at 02:26 UTC
    If this is the same person that just asked the identical question on freenode #Perl, you really ought to declare that you're taking resources from parallel locations at once, so we don't waste time answering a question that has already been answered in another forum.

    If it isn't, you should have been on #perl just now. {grin}

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      you really ought to declare that you're taking resources from parallel locations at once, so we don't waste time answering a question that has already been answered in another forum.

      I disagree. Such an advisory is meaningless, useless, and a waste of time to those who inhabit only the one space or the other (e.g., nearly everyone on PerlMonks); and for you few who inhabit both spaces, the advisory would be redundant.

      Besides, it's not a problem to ask in a channel/chatroom/chatterbox and (possibly later) in a persistent forum like this. Quite the contrary, as the PerlMonks site docs show.

      We're building the house of the future together.

      Be interesting to see if OP gets a better answer here than there. :)


      DWIM is Perl's answer to Gödel