Windows 上的 Perl、文件关联和 I/O 重定向 [英] Perl on Windows, file associations and I/O redirection

查看:46
本文介绍了Windows 上的 Perl、文件关联和 I/O 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释通过文件关联调用 perl 脚本与通过 perl.exe 显式调用相同脚本之间的区别吗?

Can someone explain the difference between calling a perl script via file association versus calling the same script explicitly via perl.exe?

显然,当通过文件关联调用脚本时,I/O 重定向不能很好地工作,我真的很想知道为什么.

Apparently I/O redirection doesn't work very well when the script is called via file association, and I would really like to know why.

例如查看 Windows 上 Perl 的 Activestate 常见问题解答.只要脚本不通过重定向接收输入,cat 文件 示例就可以完美运行.所以

E.g. take a look at the Activestate FAQ for Perl on Windows. The cat file example works perfectly as long as the script doesn't receive its input via redirection. So

cat file.txt

按预期工作,但

whoami | cat.pl

没有.不仅需要 .pl 扩展名,而且 whoami 的输出显然没有通过管道传输到脚本中.脚本运行(可以通过修改示例cat.pl 脚本来验证),但由于某种原因它没有接收whoami 的输出作为输入.

does not. Not only is the .pl extension needed, but apparently the output of whoami isn't piped into the script. The script is run (which can be verified by modifying the example cat.pl script), but for some reason it doesn't receive the output of whoami as input.

但是,如果我这样调用脚本:

However, if I call the script like this:

whoami | perl cat.pl

一切都按预期进行.

显然,通过文件关联运行脚本与使用脚本显式调用 perl.exe 之间存在重要区别.

So apparently there is an important difference between running the script via file association and explicitly calling perl.exe with the script.

FAQ 提到了这个问题,并指出使用 pl2bat 为脚本生成一个 bat 文件封面可以解决这个问题,但我不明白为什么这是必要的.

The FAQ mentions the problem and points out that using pl2bat to generate a bat file cover for the script fixes the problem, but I don't understand why this is necessary.

请赐教.

推荐答案

它看起来像是 Windows 2000 中的一个已知错误:如果从文件关联开始,STDIN/STDOUT 重定向可能不起作用.

It looks like it was a known bug in Windows 2000: STDIN/STDOUT Redirection May Not Work If Started from a File Association.

我得到了你在 WinXP 上用 Strawberry Perl 描述的相同行为,但是一旦我创建了上面文章中描述的注册表项(即使 reg 项是针对 Win2K 的),stdin 将作为预期.

I get the same behaviour you describe with Strawberry Perl on WinXP, however once I created the registry entry described in the above article (even though the reg entry is targetted at Win2K), stdin works as expected.

为了完整起见,如果上面的链接消失了,它建议创建的 reg 条目是:

For completeness, in case the link above goes away, the reg entry it suggests creating is:

  1. 启动注册表编辑器.
  2. 在注册表中找到并单击以下项:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
  3. 在编辑菜单上,单击添加值,然后添加以下注册表值:
    • 值名称:InheritConsoleHandles
    • 数据类型:REG_DWORD
    • 基数:十进制
    • 值数据:1

我应该补充一点,尽管知识库文章声称它已在 XP SP1 中修复,但我已经安装了 XP SP3.所以,MS 是否破坏了这个,或者从未完全修复它,我不能说!

I ought to add that even though the KB article claims it was fixed in XP SP1, I've got XP SP3 installed. So whether MS have broken this, or never fixed it completely, I can't say!

这篇关于Windows 上的 Perl、文件关联和 I/O 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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