Without going into details; I have the need to pass a string containing double backslashes \\ from the DOS command line and convert these to single backslashes in the code.

The problem is if I do this from within perl code directly it is no problem but when I attempt to pass a string from the command line, it seems to lose something in the translation and when I try to do the replace it does not work. So the question is HOW to pass the string from the command line so it works. Here is the sample code that works without passing from the command line:

perl source code = test_string_replace21.pl:

Code: ( text ) #!/usr/bin/perl -w use strict; # replacing double back slash... my $string1 = "-----\\---------"; $string1 =~ s/"\\"/"\"/g; print "The resulting value is : $string1 \n";
This of course works:
Code: ( text ) C:\> C:\>test_string_replace21.pl The resulting value is : -----\---------
As you can see, the double back slash is changed to a single one as expected. But now use the following code which should do the same thing as above but takes the string as argument from the command line: perl source code = test_string_replace2.pl:
Code: ( text ) #!/usr/bin/perl -w use strict; my $string1 = $ARGV[0]; $string1 =~ s/"\\"/"\"/g; print "The resulting value is : $string1 \n";
I run it from DOS command line:
Code: ( text ) C:\>test_string_replace2.pl "-----\\-----" The resulting value is : -----\\-----
As you can see, the double back slash is still there!!!!! Why????? And how do I get around it?

Thanks to anyone who can help.


In reply to How can I CORRECTLY pass strings as arguments from command line? by Zygor1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.