Cannot include space in string in one line program mode for Perl on Windows?

95 views Asked by At

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.

1

There are 1 answers

3
MC ND On
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.