2010-12-04 32 views
4

嘗試在命令窗口中運行簡單的perl腳本並獲取錯誤:string terminator "'" anywhere before EOF at -e line 1爲什麼Perl不在Windows上單線程工作?

perl -e 'print "Hello World";' 

我在做什麼錯?

+1

我只是複製粘貼並跑這個,對我來說運行良好。 – Lazer 2010-12-04 05:03:02

+0

如果你需要/想在Windows上運行這樣的單行,我會建議安裝Cygwin:http://www.cygwin.com/ - 它給你一個類似unix的環境,包括一個合適的shell(bash)和perl ... – slu 2010-12-04 07:29:24

回答

11

哪個平臺?如果是Windows和CMD.EXE,那麼各種各樣的事情可能會出錯。在類Unix平臺上,這應該可以正常工作。最後沒有換行符,所以很可能您的提示符似乎以'Hello World'開頭,但僅此而已。


隨着評論說是Windows,那麼麻煩的是Windows CMD.EXE的不解析命令行一樣的Unix和你不能簡單地用周圍參數單引號;你必須使用雙引號。請嘗試:

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

有一個適度的機會它會爲你工作。

6

perldoc perlfaq3 - Why don't Perl one-liners work on my DOS/Mac/VMS system?

The problem is usually that the command interpreters on those systems have rather different ideas about quoting than the Unix shells under which the one-liners were created. On some systems, you may have to change single-quotes to double ones, which you must NOT do on Unix or Plan9 systems. You might also have to change a single % to a %% . For example:

# Unix (including Mac OS X) 
perl -e 'print "Hello world\n"' 

# DOS, etc. 
perl -e "print \"Hello world\n\"" 

# Mac Classic 
print "Hello world\n" 
(then Run "Myscript" or Shift-Command-R) 

# MPW 
perl -e 'print "Hello world\n"' 

# VMS 
perl -e "print ""Hello world\n""" 

The problem is that none of these examples are reliable: they depend on the command interpreter. Under Unix, the first two often work. Under DOS, it's entirely possible that neither works. If 4DOS was the command shell, you'd probably have better luck like this:

perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>"" 

Under the Mac, it depends which environment you are using. The MacPerl shell, or MPW, is much like Unix shells in its support for several quoting variants, except that it makes free use of the Mac's non-ASCII characters as control characters.

Using qq() , q() , and qx() , instead of "double quotes", 'single quotes', and backticks , may make one-liners easier to write. There is no general solution to all of this. It is a mess.

0

嘗試:perl -e " print 'Hello..'; " 這個工程在引用不規範POSIX在Windows CMD.EXE控制檯。

相關問題