Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How do I keep the command line from eating the backslashes?

by Anonymous Monk
on Feb 11, 2005 at 21:16 UTC ( [id://430281]=perlquestion: print w/replies, xml ) Need Help??

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

Silly easy novice question: How do I preserve \" passed in on the commandline?
i.e: foo.pl -bar=\"donteatem\"
I get $ARGV[0] as -bar="donteatem", where i really do want to keep the backslashes.

Thanks in advance.

20050212 Edit by castaway: Changed title from 'Easy @ARGV question'

  • Comment on How do I keep the command line from eating the backslashes?

Replies are listed 'Best First'.
Re: How do I keep the command line from eating the backslashes?
by revdiablo (Prior) on Feb 11, 2005 at 22:15 UTC

    This feels like an XY problem. You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X [your later posts hint at X, but never really describe it]. The problem is, there might be a better solution, but we can't know that unless you describe what X is.

    In other words, I would be surprised if you really need the literal '\"' to remain intact. What is the actual problem you're trying to solve?

Re: How do I keep the command line from eating the backslashes?
by amw1 (Friar) on Feb 11, 2005 at 21:19 UTC
    assuming bash or similar shell (unknown on windows)
    # \\ escapes the slash # \" escapes the quote # so \\\" escapes a single slash and a quote foo.pl -bar=\\\"donteatem\\\"
    perl should get -bar=\"donteatem\"
      Ahh... I'd love to be able to do that... But what if I cannot control the input? I'm reading input destined for another program, and I (most unfortunately) don't have control of what's generating the commandline.

      So, I'm always going to get \" instead of \\"
        Two thoughts.

        First, it's the shell that's eating the quotes and backslashes. If you change the shell to a program of your own, you can process these characters however you want. Many programs use the SHELL environment variable to select the shell to run, so changing that might help.

        Second, in order for arguments to have gotten through the shell, they must have been quoted properly. quotemeta can quote them properly, even if it's not the same way they were originally quoted.

Re: How do I keep the command line from eating the backslashes?
by ikegami (Patriarch) on Feb 11, 2005 at 22:44 UTC
    In Windows, >perl -e "print $ARGV[0]" "test" test >perl -e "print $ARGV[0]" \"test\" "test" >perl -e "print $ARGV[0]" "\"test\"" "test" Ug, this is very broken: >perl -e "print $ARGV[0]" "te\st" te\st >perl -e "print $ARGV[0]" "te\\st" te\\st >perl -e "print $ARGV[0]" "te\"st" te"st >perl -e "print $ARGV[0]" "te\\"st" te\st Alternate (older?) quoting mechanism: >perl -e "print $ARGV[0]" """test""" "test" >perl -e "print $ARGV[0]" """Nevermore"""", quoted the raven." "Nevermore", quoted the raven.
Re: How do I keep the command line from eating the backslashes?
by artist (Parson) on Feb 11, 2005 at 21:20 UTC
    Apply Escape char '\' to both of them.
    $a = qq(\\\"hello world\\\"); print $a;
    gives
     \"hello world\"
Re: How do I keep the command line from eating the backslashes?
by sh1tn (Priest) on Feb 11, 2005 at 21:22 UTC
    assuming the ugly win32 cli:
    program.pl "\"argument\""
Re: How do I keep the command line from eating the backslashes?
by DrHyde (Prior) on Feb 14, 2005 at 14:22 UTC
    You start by asking on shellmonks instead of perlmonks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found