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

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.

Replies are listed 'Best First'.
Re: How can I CORRECTLY pass strings as arguments from command line?
by almut (Canon) on Jul 16, 2008 at 02:47 UTC

    First, my $string1 = "-----\\---------"; doesn't actually declare a string with two backslashes (that would be "-----\\\\---------"  — remember the backslash is special in string literals (even if single quoted '-----\\---------' it doesn't produce two backslashes)).  IOW, it's a single backslash to start with in your first case...

    Second, you don't want the quotes in the substitution: s/"\\"/"\"/g;.  Try

    $string1 =~ s/\\\\/\\/g;
Re: How can I CORRECTLY pass strings as arguments from command line?
by graff (Chancellor) on Jul 16, 2008 at 03:22 UTC
    Try this version of your first snippet, to understand almut's first point a little better:
    #!/usr/bin/perl -w use strict; # replacing double back slash... my $string1 = "-----\\---------"; print "The starting value is: $string1\n"; # add this line $string1 =~ s/"\\"/"\"/g; print "The resulting value is: $string1\n";
    You'll see that your use of s/// didn't really change anything. So when you face a problem like this, it's good to print both the "before" and the "after". In terms of handling command line args from @ARGV, you might want to try some tests first with a script like this:
    #!/usr/bin/perl use strict; use warnings; print "Contents of ARGV:\n>=", join( "=<\n>=", @ARGV), "=<\n";
    Run that a few times with different patterns of interest as command-line args, and see what happens. It's really just a way of finding out what the shell is doing to the args before passing them on to the perl script, which is a useful thing to know. Then add some s/// operations to alter the args, and see how those work.
Re: How can I CORRECTLY pass strings as arguments from command line?
by ikegami (Patriarch) on Jul 16, 2008 at 05:44 UTC

    s/"\\"/"\"/g means "replace each occurence of the three character sequence double-quote, backslash, double-quote with the two character sequence double-quote, double-quote."

    The solution has already been provided.