为什么我的Perl单行代码无法在Windows上运行? [英] Why doesn't my Perl one-liner work on Windows?

查看:88
本文介绍了为什么我的Perl单行代码无法在Windows上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Windows命令提示符下,我生成目录中所有文件的文本文件:

From the Windows command prompt I generate a text file of all the files in a directory:

dir c:\logfiles /B > config.txt

输出:

0001_832ec657.log
0002_a7c8eafc.log

我需要将"config.txt"文件提供给另一个可执行文件,但是在这样做之前,我需要修改文件以添加可执行文件所需的一些其他信息.所以我使用以下命令:

I need to feed the "config.txt" file to another executable, but before I do so, I need to modify the file to add some additional information that the executable needs. So I use the following command:

perl -p -i.bak -e 's/log/log,XYZ/g' config.txt

我希望结果是:

0001_832ec657.log,XYZ
0002_a7c8eafc.log,XYZ

但是,"config.txt"文件未修改.使用"-w"选项,我收到警告消息:

However, the "config.txt" file is not modified. Using the "-w" option, I get the warning message:

在-e第1行的void上下文中无用的常量.

Useless use of a constant in void context at -e line 1.

我在做什么错了?

推荐答案

Windows cmd.exe不使用'作为字符串定界符,仅使用"作为字符串定界符.您正在做的事情等同于:

Windows cmd.exe does not use ' as string delimiters, only ". What you're doing is equivalent to:

perl -p -i.bak -e "'s/log/log,XYZ/g'" config.txt

所以-w抱怨您给了我一个字符串,但它什么也没做".

so -w is complaining "you gave me a string but it does nothing".

解决方案是改用双引号:

The solution is to use double quotes instead:

perl -p -i.bak -e "s/log/log,XYZ/g" config.txt

或简单地将其关闭,因为该命令中没有元字符可以由cmd.exe解释.

or to simply leave them off, since there's no metacharacters in this command that would be interpreted by cmd.exe.

cmd.exe只是一个非常麻烦的野兽,对于习惯于sh类外壳的任何人而言.这是有关perl调用的其他一些常见故障和解决方法.

cmd.exe is just a really troublesome beast, for anybody accustomed to sh-like shells. Here's a few other common failures and workarounds regarding perl invocation.


@REM doesn't work:
perl -e"print"
@REM works:
perl -e "print"
@REM doesn't work:
perl -e "print \"Hello, world!\n\""
@REM works:
perl -e "print qq(Hello, world!\n)"

这篇关于为什么我的Perl单行代码无法在Windows上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆