http://qs321.pair.com?node_id=856049

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

How to replace single backslash in windows path to double backslashes $path="c:\abc\xyz" == > "c:\\abc\\xyz" I tried ~s/\\/\\\\/g but it didn't work Thanks for help

Replies are listed 'Best First'.
Re: windows path problem
by toolic (Bishop) on Aug 19, 2010 at 15:08 UTC
    Neither did you show actual Perl code, nor did you describe how it did not work for you. This will perform the substitution you requested:
    use strict; use warnings; my $path = 'c:\abc\xyz'; $path =~ s/\\/\\\\/g; print $path; __END__ c:\\abc\\xyz

    The double quotes interpolate. If you use strict and warnings, you will get a warning message.

    On a side note, when posting, just include code within code tags, not all of your text.

      Thanks for solving the problem.
Re: windows path problem
by ikegami (Patriarch) on Aug 19, 2010 at 15:34 UTC

    I'm not sure why you'd need to do that. Normally, you need to double the slashes in a string literal so it produces a string with single backslashes.

    >perl -le"print qq{c:\\abc\\xyz};" c:\abc\xyz

    You do not want to pass doubled slashes to open.

      But you might want double quotes if you're going to be using the shell, as in
      system( "dir $path" );
      rather than bypassing the shell
      system( 'dir', $path )';
      Disclaimer: I'm a Unix guy. This would be true on Unix; does "MS-DOS 2010" still have a shell?

      --
      TTTATCGGTCGTTATATAGATGTTTGCA

        No, you wouldn't. "\" is not special to the cmd shell, and it doesn't use "\" for escaping.
Re: windows path problem
by murugu (Curate) on Aug 19, 2010 at 15:33 UTC
    anu_2,

    Your substituion regex is correct but as you have given $path value in double quotes it got interpolated. Change it to single quotes and it will work fine.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);

      Thanks a lot