6

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

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.

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

works as expected, but

whoami | cat.pl

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

everything works as expected.

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

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.

Please enlighten me.

4

2 回答 2

9

看起来这是 Windows 2000 中的一个已知错误:STDIN/STDOUT Redirection may not work If Started from a File Association

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

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

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

编辑:我应该补充一点,即使知识库文章声称它已在 XP SP1 中修复,我已经安装了 XP SP3。所以无论MS是否破坏了这个,或者从未完全修复它,我不能说!

于 2009-08-17T10:04:35.720 回答
0

我知道这是一个旧线程,但重要的是要注意微软似乎已经扭转了 Windows 10 中此设置的极性。不知道他们为什么会这样做,但这让我把头发拉了一天。请参阅我发布的其他问题(Perl Scripts on Windows 10 run from Explorer but not Command Prompt),尤其是那里的链接 MSDN 帖子。

于 2016-08-11T15:59:03.483 回答