Why does this single-line Perl not work on Windows?

I try to run a simple Perl script on the command line and get an error message:

terminator string '' 'anywhere before EOF in line -e 1

Code

perl -e 'print "Hello World";' 

What am I doing wrong?

+4
source share
3 answers

Which platform? If it was Windows and CMD.EXE, then all sorts of things could go wrong. On a Unix-like platform, this should work fine. There is no new line at the end, so most likely your invitation will start with “Hello World,” but that’s it.


With the comment that this is Windows, the problem is that Windows CMD.EXE does not parse the command line just like Unix, and you cannot just use single quotes around the arguments; you need to use double quotes. Try:

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

There will be a modest chance that it will work for you.

+11
source

From perldoc perlfaq3 - Why don't single-line Perl work on my DOS / Mac / VMS system?

Typically, the problem is that command interpreters on these systems have quite different ideas about quoting than the Unix shells that were created as single-line ones. On some systems, you may have to change the single quotation marks to double quotes, which you should NOT do on a Unix or Plan9 system. You may also have to change one % to a %% . For instance:

 # 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 shell. On Unix, the first two often work. In DOS, it is possible that neither works. If 4DOS was a shell, you probably would have been lucky with this:

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

On a Mac, it depends on which environment you are in with. The MacPerl or MPW shell is the same as the Unix shell in its support for several citation options, except that it uses Mac non-ASCII characters for free as control characters.

Using qq() , q() and qx() instead of double quotes, single quotes, and backticks can make single-line files easier to write. There is no general solution to all of this. It's a mess.

+6
source

Try: perl -e " print 'Hello..'; " This works in the Windows console CMD.EXE , where quoting is not standardized by POSIX.

0
source

All Articles