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.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317

2 Answers2

9

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

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.

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

  1. Start Registry Editor.
  2. Locate and then click the following key in the registry: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
  3. On the Edit menu, click Add Value, and then add the following registry value:
    • Value name: InheritConsoleHandles
    • Data type: REG_DWORD
    • Radix: Decimal
    • Value data: 1
  4. Quit Registry Editor.

Edit: 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!

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • I suspected this was a Windows issue and not a Perl issue. Thanks for the link. With the updated registry it works as expected. – Brian Rasmussen Aug 17 '09 at 11:04
  • If patching the registry isn't an option running `foo | perl -S bar.pl` is less annoying work-around for scripts in your PATH. – Michael Carman Jan 07 '11 at 18:02
  • Thanks, and yes the fact that that KB page claims the issue is "fixed" in XP SP1 when the registry fix still needs to be manually applied is infuriating! The main problem being that one can't assume that an arbitrary PC running >= XP SP1 will allow redirection... This is exactly the kind of thing that allows "It Works On My Machine" bugs to flourish... – j_random_hacker Apr 12 '11 at 20:51
0

I know this is an old thread, but it's important to note that Microsoft appears to have reversed the polarity of this setting in Windows 10. No idea why they would do that, but this had me pulling my hair out for a day. See my other posted question (Perl Scripts on Windows 10 run from Explorer but not Command Prompt), and especially the linked MSDN post there.

Community
  • 1
  • 1
jimtut
  • 2,366
  • 2
  • 16
  • 37