Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

How do I print a quote with a perl one liner on dos?

by tphyahoo (Vicar)
on Mar 23, 2005 at 09:46 UTC ( [id://441723]=perlquestion: print w/replies, xml ) Need Help??

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

How do I print out a single " character with a perl one liner on dos, winxp, ActiveState perl?

None of the following worked.

REM tryToPrintQuote.bat perl -e 'print "' perl -e "print \"" perl -e "print q{"}" perl -e qq{print q{"}}

Why don't perl one-liners work on my DOS/Mac/VMS system? was no help. Has anything changed since this was written in 1999?

If not, I guess I'll be installing cygwin...

Replies are listed 'Best First'.
Re: How do I print a quote with a perl one liner on dos?
by gellyfish (Monsignor) on Mar 23, 2005 at 09:56 UTC

    You didn't go far enough through all the possible permutations ;-)

    S:\>perl -e"print '\"'"
    Works fine for me.

    /J\

Re: How do I print a quote with a perl one liner on dos?
by thinker (Parson) on Mar 23, 2005 at 09:55 UTC

    hi tphyahoo,

    Are you sure you have asked the correct question? If so, this will do what you want

    perl -le "print chr 34"

    thinker

      Thanks Thinker, I guess I didn't really ask the right question.

      What I really want is to have the same nice quoting behaviors in my one liners that I am used to in perl. Dos is weird with quotes. I was wondering if there is some kind of workaround.

      I would like to be able to do

      perl -e "$helloWorld = '"hello world"'; print "$helloWorld"";
      and have it "just work" (ie, print hello world with double quotes around it) without being in escape-with-backslash hell, ie,
      perl -e "$helloWorld = '\"hello world\"'; print "$helloWorld"";

      as, with gellyfish's help, I now know to do.

      I guess maybe this really isn't *that* "hellish" but it isn't anywhere near as heavenly as perl outside of the dos shell. So, if there is some other trick...

        when you print $whatever you don't need to print "$whatever" unless you want to print "$whatever and $something else in the same string" - that'll at least save you two backslashed double quotes.

        that said, the point is that with the windows command shell the double quote is the string quoting character so in order not to have your perl -e code string stop at the next ", you have to escape it for the shell's benefit. to illustrate - escaping outside of the -e code: perl -e "print qq(@ARGV)" \"hello world\"

        I guess the trick is that you use single quotes instead ;)

        perl -e "$helloworld = qq('hello world'); print $helloworld"

           larryk                                          
        perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
        
Re: How do I print a quote with a perl one liner on dos?
by kelan (Deacon) on Mar 23, 2005 at 14:08 UTC

    By piecing together the information in the above replies, you can find your answer. But if you are impatient, here is a more direct explanation.

    The Windows command shell uses the double-quote character as its quoting character. Anything that you don't want the shell to try and interpret needs to go between quotes, such as the Perl code being passed to -e. The problem you are facing comes in when you want to use a double-quote character in that Perl code. In order to do that without the shell trying to interpret it, you need to escape the character with a backslash, like you did in your second example. The reason that example fails is not because you got the shell quoting wrong, but because the Perl code is nonsensical as written. Perl is interpreting it like this:

    print "
    gellyfish points out in his answer that you'll need to quote the character for Perl before it will work:
    print '"'
    and translated for the shell:
    perl -e"print '\"'"

    In response to your plea to have it "just work," there's no way around it. If you wan't to use a shell's quoting character without the shell interpreting it, it needs to be escaped. This is the same for Cygwin, it just uses a different character. But if, under Cygwin, you wanted to use the single-quote character in the Perl code, you would need to escape it as well.

    One last comment on printing quote characters in one-liners. Perl includes a scarcely-mentioned ability to interpret a short sequence of characters directly as a single ASCII character: v-strings. A bareword matching /v\d+/ will be translated to the ASCII character represented by the digits following the v. This is useful particularly for Windows one-liners because you can get a newline without having to double-quote anything (which, as we have seen, is painful). For example, this prints hello world with a newline:

    perl -e"print 'hello world', v10"
    And this prints a double-quote character with a newline:
    perl -e"print v34, v10"
    Note, however, that v-strings' usefulness is limited by the fact that they only work as barewords, not inside other strings. This would print v34hello worldv34 instead of "hello world":
    perl -e"print 'v34hello worldv34'"
    Even with that limitation, I have used them often to put newlines at the end of a print in a one-liner, although this works as well and is somewhat more readable:
    perl -e"print qq[Some text ending with a newline.\n]"

      An excellent summary kelan, though i think it should be pointed out that vstrings have been deprecated since 5.8.1, and will, I believe, be removed altogether for 5.10

      cheers

      thinker

        I was vaguely aware that they had been deprecated, but I wasn't sure if it was happening in Perl 5 or not. Thanks for pointing it out.

Re: How do I print a quote with a perl one liner on dos?
by chas (Priest) on Mar 23, 2005 at 12:41 UTC
    I usually use qq (together with escaped double quotes if I want those)as in perl -e "$x='bla';print qq(\"$x\");" so that I can interpolate variables if desired. This is similar to gellyfish's solution. It is sometimes a puzzle (especially when mixing in more complicated constructions like eval...)
    chas
Re: How do I print a quote with a perl one liner on dos?
by halley (Prior) on Mar 23, 2005 at 14:54 UTC
    I vaguely recalled that in MS-DOS, two double-quotes was meant to turn into one double-quote. After continuing to experiment in WinNT's CMD interpreter, I find it's a bit inconsistent.
    U:\> perl -e "print $_,$/ for @ARGV" "" "'" '"""' '' '"' '""' " +"" "x""x" __OUTPUT__ ' '"' '' '' '"' " x"x

    --
    [ e d @ h a l l e y . c c ]

Re: How do I print a quote with a perl one liner on dos?
by jhourcle (Prior) on Mar 23, 2005 at 13:05 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found