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

Hi, I have a query regarding easiest way to insert backslash for each backslash (escape)in string: for e,g: $path="c:\abs\cbz" I want to do like this $path="c:\\abs\\cbz". Could any one help me towards this? Thanks in advance, Sunil
  • Comment on how to prefix\insert back slash to string (windows) path in perl

Replies are listed 'Best First'.
Re: how to prefix\insert back slash to string (windows) path in perl
by ikegami (Patriarch) on Sep 06, 2010 at 06:35 UTC
    I cannot come up with a scenario where you'd need to escape the slashes and nothing else. What are you actually trying to do?
Re: how to prefix\insert back slash to string (windows) path in perl
by suhailck (Friar) on Sep 06, 2010 at 06:24 UTC
    Reg. your question, this will work

    perl -le '$path=q{c:\abs\cbz};$path=~s/\\/\\\\/g;print $path' c:\\abs\\cbz
Re: how to prefix\insert back slash to string (windows) path in perl
by suhailck (Friar) on Sep 06, 2010 at 06:17 UTC
      Thanks Suhail. BTW, I have one more query. Hope this approach will work for 'n' coinsurances of special character too. -Sunil
Re: how to prefix\insert back slash to string (windows) path in perl
by cdarke (Prior) on Sep 06, 2010 at 12:31 UTC
    Also note that very often this is not necessary. Most file access in Windows can use either / or \ as a directory seperator. Some shells, like Windows Explorer and cmd.exe, only accept \, but the Windows API accepts either.
Re: how to prefix\insert back slash to string (windows) path in perl
by talexb (Chancellor) on Sep 07, 2010 at 00:56 UTC

    I recommend File::Spec for any path and filename work that you need to do. That will do The Right Thing on whichever platform you're on.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: how to prefix\insert back slash to string (windows) path in perl
by imrags (Monk) on Sep 06, 2010 at 11:16 UTC
    Try this:
    $path =~ s/\\/\\\\/g;
    Raghu