Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: [Win32] Escaping quotes in perl one liner (updated)

by AnomalousMonk (Archbishop)
on Nov 28, 2020 at 07:05 UTC ( [id://11124307]=note: print w/replies, xml ) Need Help??


in reply to [Win32] Escaping quotes in perl one liner

Some | The CMD MSDOS/Windows command line interpreter (God love it) requires, for some reason, that certain characters like & (there may be others (update: I believe & \ < > ^ | are all the characters that must be carat-escaped between backslash-escaped double-quotes)) that are between escaped double-quotes be escaped with a ^ (carat). Go figure.

Win8 Strawberry 5.8.9.5 (32) Sat 11/28/2020 2:08:16 C:\@Work\Perl\monks\MikeTaylor >perl -Mstrict -Mwarnings -le "print \"&\", '\"&\"', '&';" Can't find string terminator '"' anywhere before EOF at -e line 1. '\", '\"' is not recognized as an internal or external command, operable program or batch file. '\"', '&';"' is not recognized as an internal or external command, operable program or batch file. Win8 Strawberry 5.8.9.5 (32) Sat 11/28/2020 2:08:48 C:\@Work\Perl\monks\MikeTaylor >perl -Mstrict -Mwarnings -le "print \"^&\", '\"^&\"', '&';" &"&"&

Update: Changed example code to provide more instances of printing & | the presence of & inside versus outside escaped double-quotes.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: [Win32] Escaping quotes in perl one liner
by syphilis (Archbishop) on Nov 28, 2020 at 09:07 UTC
    that certain characters like & (there may be others) that are between escaped double-quotes be escaped with a ^ (carat).

    Yes ... that was the bit of information I was missing.
    I had tried using the stick on the on the > and the &, to no avail:
    C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = \"\>\&STDOUT\"; Dump + $arg;" The system cannot find the path specified. The system cannot find the path specified.
    But using the carrot works fine:
    C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = \"^>^&STDOUT\"; Dump + $arg;" SV = PV(0x2dcd2c) at 0x1c90564 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x1c8ff64 ">&STDOUT"\0 CUR = 8 LEN = 10 COW_REFCNT = 1
    Perhaps when the shell saw a backslash in those contexts, it might have been inclined to think "directory separator", so they had to come up with a different escape symbol.

    UPDATE: It seems that, even with q()and qq(), there are still times when the carat needs to be invoked as an escape:
    C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = q(">&STDOUT"); Dump +$arg;" >& was unexpected at this time. C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = q("^>^&STDOUT"); Dum +p $arg;" SV = PV(0x6bcd2c) at 0x57b30c REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x58056c ">&STDOUT"\0 CUR = 8 LEN = 10 COW_REFCNT = 1 C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = qq(">&STDOUT"); Dump + $arg;" >& was unexpected at this time. C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = qq("^>^&STDOUT"); Du +mp $arg;" SV = PV(0x6ccd2c) at 0x1e30fe4 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x1e6f47c ">&STDOUT"\0 CUR = 8 LEN = 10 COW_REFCNT = 1 C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = q(\">&STDOUT\"); Dum +p $arg;" >& was unexpected at this time. C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = q(\"^>^&STDOUT\"); D +ump $arg;" SV = PV(0x55cd2c) at 0x650274 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x690f5c "\">&STDOUT\""\0 CUR = 10 LEN = 12 COW_REFCNT = 1 C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = qq(\">&STDOUT\"); Du +mp $arg;" >& was unexpected at this time. C:\_32>perl -Mstrict -MDevel::Peek -le "my $arg = qq(\"^>^&STDOUT\"); +Dump $arg; " SV = PV(0x34cd34) at 0x1d8afbc REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x1d905ac "\">&STDOUT\""\0 CUR = 10 LEN = 12 COW_REFCNT = 1 C:\_32>


    Cheers,
    Rob
      Perhaps when the shell saw a backslash in those contexts, it might have been inclined to think "directory separator", so they had to come up with a different escape symbol.

      Ah-ha. Yes, I think you're right! I've wondered about that for a long time. Thank you for dispelling my perplexity.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-24 09:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found