2

我正在尝试从 Windows 8 的 PowerShell 中的命令行运行多个 Perl 命令。

这有效

perl -e "print 'Joe'";

这打印:

Joe

这不起作用

perl -e "my $string = 'Joe'; print $string;"

这给了我一个错误

perl : syntax error at -e line 1, near "my  ="
At line:1 char:1
+ perl -e "my $string = 'Joe'; print $string;"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (syntax error at -e line 1, near "my  =":String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Execution of -e aborted due to compilation errors.

有人可以指出我遗漏的明显错误吗?谢谢。我通常在 UNIX 上执行此操作,但使用反引号。作为包装而不是双引号。

4

1 回答 1

8

试试这个,但我无法测试

perl -e 'my $string = ''Joe''; print $string;'

在 powershell$string中是一个变量,在双引号中,变量使用分配的值进行扩展。在单引号中被视为文字。要'在单引号中转义,请将其加倍为''

或尝试:

perl -e  "my `$string = 'Joe'; print `$string;"

` 是 powershell 中的转义字符。

于 2013-03-25T21:11:32.267 回答