14

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?

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
R.D
  • 4,781
  • 11
  • 41
  • 58
  • if you are using perl 5.10+, use the `-E` option rather than `-e`, which turns on the new features, among them `say` which is `print` but with an implicit newline at the end. `perl -E "say 'hello world'"` – Eric Strom Oct 14 '11 at 00:01
  • It doesn't work @Eric. Not in this case. Because the problem here is the quotation marks which need to be reversed. (I wonder if it works for you ...) – Apostolos Dec 23 '17 at 09:41
  • 1
    Possible duplicate of *[Why doesn't my Perl one-liner work on Windows?](https://stackoverflow.com/questions/660624/why-doesnt-my-perl-one-liner-work-on-windows)* – Peter Mortensen Jul 12 '19 at 19:01

4 Answers4

29

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).

cjm
  • 61,471
  • 9
  • 126
  • 175
  • 3
    Actually, I think you are overstating. I never felt too crippled writing one-liners in Windows. – TLP Oct 13 '11 at 22:25
  • 1
    In Windows, it's the application's job (not the shell's job) to parse the command line. The OPs problem looks to me as if it represents a bug in the Windows release of Perl? – Harry Johnston Oct 14 '11 at 00:58
  • 1
    @HarryJohnston, it's actually both. First, the shell parses the command line (looking for pipes, redirection, etc.). Then, the application parses the command line. (Usually, it's actually the app's C/C++ runtime library that parses it.) That's why the Windows command line is such a mess; the parsing rules aren't well defined, and different C libraries parse things slightly differently. – cjm Oct 14 '11 at 03:45
  • @cjm, in this case I'm pretty sure the shell is passing the command line to perl.exe untouched, so it looks to me as if perl.exe isn't parsing it correctly. – Harry Johnston Oct 15 '11 at 04:53
  • 2
    @HarryJohnston, on Windows, Perl parses the command line according to Windows rules. (Rather, the C library that Perl was linked with parses the command line according to Windows rules.) The one-liner was written with Unix shell quoting rules, and that doesn't work on Windows. – cjm Oct 15 '11 at 05:14
  • @cjm, ideally Perl would parse the command line in such a way that the synopsis at http://perldoc.perl.org/perl.html was correct. (There are no "Windows rules", each app is expected to parse the command line according to its own specification.) But I see the actual behaviour is documented at /perlwin32.html so I guess this is a "feature" rather than a bug. :-) – Harry Johnston Oct 16 '11 at 00:09
  • 1
    @HarryJohnston, the app isn't parsing the command line in isolation. The shell is also parsing it. [This question](http://stackoverflow.com/q/36772633/8355) provides a nice example: `perl -ne 'print if (rand() < .01)' train.csv`. You get an error when the shell tries to redirect input from a file named `.01)'` – cjm Apr 21 '16 at 21:59
  • @cjm: sure, but that's a separate step. The OP's problem didn't involve any of cmd.exe's special characters, so they're not really relevant to this question. (And when they are relevant, they're fairly straightforward to deal with: just prefix every special character, including quote marks, with a caret.) – Harry Johnston Apr 21 '16 at 22:14
4

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)

2

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 \"

Jason D
  • 8,023
  • 10
  • 33
  • 39
Glenn
  • 21
  • 1
0

On Windows the following command works:

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131