开发者

Why am I getting "Can't find string terminator "'" anywhere before EOF at -e line 1" when I try to run a Perl one-liner on Windows? [duplicate]

This question开发者_Go百科 already has answers here: Why doesn't this Perl one-liner work on Windows? (3 answers) Closed 3 years ago.

I am trying to run the following on Windows with 5.14.2

C:\Perl>perl -e 'print "Hello World \n"'
Can't find string terminator "'" anywhere before EOF at -e line 1.

What am I missing?


You're missing a decent shell with sane and well-defined quoting rules. On Windows, only the double quote is considered a quote, and the escaping rules are poorly defined and inconsistent. Try:

perl -e "print qq{Hello World \n}"

I strongly recommend avoiding anything but the very simplest one-liners on Windows. (Another problem with Windows one-liners is that the Windows shell doesn't expand wildcards. If you use *.txt on the command line, it'll look for a file named literally *.txt. You'll run into that later.)

On Windows, what you typed is equivalent to:

perl -e "'print" "Hello World \n'"

That is, the code Perl is trying to execute is 'print with @ARGV containing the single string Hello World \n'. (That's not a newline, that's a backslash followed by n).


On Windows, the quotation marks should be reversed. So, rather than:

C:\Perl>perl -e 'print "Hello World \n"'

it should be:

C:\Perl>perl -e "print 'Hello World \n'"

(attribution Learning Perl, 6th edition, p. 295)


I also found this to work. I am using Windows 7 using c:\Windows\system32\cmd.exe

perl -e "$a=2; print(\"$a \n \");"

I'm using the escape slash in my print statement to get the quotes to appear \"


On Windows the following command works:

perl -e "print \"Hello world\""

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜