Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

printing special characters taken in command line

by bagana14 (Initiate)
on May 19, 2015 at 15:45 UTC ( [id://1127146]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I want to print the text which was taken as command line input. Input can contain special characters also. For ex: perl test.pl D^JkAPE\(SjKHnNl0 >> should print D^JkAPE\(SjKHnNl0. I could not find any post related to my problem. Can someone please help me here.
  • Comment on printing special characters taken in command line

Replies are listed 'Best First'.
Re: printing special characters taken in command line
by ikegami (Patriarch) on May 19, 2015 at 16:06 UTC
Re: printing special characters taken in command line
by MidLifeXis (Monsignor) on May 19, 2015 at 17:35 UTC

    To expand on ikegami's post, you need to protect your parameters from the shell.

    When you are sitting at a command prompt, you are at the mercy of the shell itself for what characters it deems special and what its rules are for escaping those special characters. Most shells have a way to tell the shell to "just pass this data straight through to the application". ikegami showed how to do it on most unix-like shells. Under Windows CMD.EXE, you would instead use double quotes ("), unless that happened to be one of the characters you wanted to pass through, in which case (IIRC) you would need to use two double quotes ("").

    --MidLifeXis

      Under Windows CMD.EXE, you would instead use double quotes ("), unless that happened to be one of the characters you wanted to pass through, in which case (IIRC) you would need to use two double quotes ("").

      Actually, \" is the most common. Unfortunately, there is not one universal answer as each command can choose to interpret its command line differently (because it isn't CMD.EXE that breaks the command line into arguments and thus handles the quotes as would be the case with a Unix shell). But it seems that following how C does it has become pretty common.

      See Re^2: The Evil Embedded Space (system(@list)) for more details and example implementation. One thing I failed to note in that node is that you get extra divergence in interpretation if you produce something that contains a double quote but doesn't match:

      /^"( [^\\"]+ | \\+[^\\"] | (\\\\)*\\" )*"$/x

      That regex may be easier to understand if I write it in a fictional version of Perl where \ is not used to escape regex meta-characters:

      /^"( [^\"]+ | \+[^\"] | (\\)*\" )*"$/x

      So, indeed, you can sometimes get what you desire by using doubled double quotes. One convenient way to test how C splits up arguments is:

      C:\> perl -le"print for @ARGV" hi "hi" "\"hi\"" "\\hi\\" "\\hi\\\"" hi hi "hi" \\hi\ \\hi\"

      That lets one see how "...""..." works while how trying to include two literal quotes that way can fail:

      C:\> perl -le"print for @ARGV" "6""5'" 6"5' C:\> perl -le"print for @ARGV" "say ""hello"" to my little friend" say "hello to my little friend

      - tye        

Re: printing special characters taken in command line
by GotToBTru (Prior) on May 19, 2015 at 15:51 UTC

    What do you have so far?

    Dum Spiro Spero
Re: printing special characters taken in command line
by Anonymous Monk on May 19, 2015 at 15:51 UTC
    perl -MO=Deparse -le'print q(D^JkAPE\(SjKHnNl0)' > test.pl && perl test.pl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found