0

The command

perl -ne "print "" """ AnyTextFile.txt

running on Windows with latest ActivePerl installed (5.020) complains Can't find string terminator '"' anywhere before EOF at -e line 1.. Other characters or variables work as expected, like

perl -ne "print ""$.-$_""" AnyTextFile.txt

I checked that double quotes are passed to perl as expected, even if it is a little weird when escape double quotes in cmd.exe. Why space cannot be shown in the above double quoted string? Using single quote could work but it loses variables interpolation functionality.

Thomson
  • 20,586
  • 28
  • 90
  • 134

1 Answers1

2
perl -ne "print \" \"" AnyTextFile.txt

Why?

A lot of programs get its arguments by means of the standard argument parser used by the C library initially used to compile the language itself, its libraries or used as a base.

For windows, in general, the "rules" for argument parsing are

Arguments are delimited by white space, which is either a space or a tab.

A string surrounded by double quotation marks is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument. Note that the caret (^) is not recognized as an escape character or delimiter.

A double quotation mark preceded by a backslash, \", is interpreted as a literal double quotation mark (").

Backslashes are interpreted literally, unless they immediately precede a double quotation mark.

If an even number of backslashes is followed by a double quotation mark, then one backslash () is placed in the argv array for every pair of backslashes (\), and the double quotation mark (") is interpreted as a string delimiter.

If an odd number of backslashes is followed by a double quotation mark, then one backslash () is placed in the argv array for every pair of backslashes (\) and the double quotation mark is interpreted as an escape sequence by the remaining backslash, causing a literal double quotation mark (") to be placed in argv.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Simpler: `perl -ne "print ' '" AnyTextFile.txt` or if you need interpolation `perl -ne "print qq(Something $with interpolation.)" AnyTextFile.txt` cuts down on the leaning toothpicks and different layers of meaning for double quotes.... – tjd Nov 28 '14 at 18:49
  • @tjd, Yes, but the problem is not perl related. I face the same situation with awk whenever i use it from command line. The "problem" is command line parsing and I think (just my opinion) that a general solution is more useful than a particular one (unless it is the only option, of course). Anyway, thank you, It is always good to know other options. – MC ND Nov 28 '14 at 19:23
  • I agree that many folks underestimate the importance of understanding the quoting/interpolation features of their chosen system's shell. I also firmly believe that a good understanding of how tool & shell features interact is important for maintainable shell scripts and system calls. In those cases, I've worked around cmd.exe landmines with perl.exe features and, I believe, improved readability. After all, no program is an island, even the shell. – tjd Nov 28 '14 at 20:42