Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

windows path problem

by anu_1 (Acolyte)
on Aug 19, 2010 at 14:59 UTC ( #856049=perlquestion: print w/replies, xml ) Need Help??

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 (Pope) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://856049]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2021-12-07 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (34 votes). Check out past polls.

    Notices?