How can I read piped input in Perl on Windows? How can I read piped input in Perl on Windows? windows windows

How can I read piped input in Perl on Windows?


This is actually a bug in how Windows handles IO redirection. I am looking for the reference right now, but it is that bug that requires you to specify

dir | perl filter.pl

rather than being able to use

dir | filter

See Microsoft KB article STDIN/STDOUT Redirection May Not Work If Started from a File Association:

  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.
C:\Temp> cat filter.pl#!/usr/bin/perlwhile ( <> ) {    print "piped: $_";}
C:\Temp> dir | filterpiped:  Volume in drive C is MAINpiped:  Volume Serial Number is XXXX-XXXXpiped:piped:  Directory of C:\Temp>piped:piped: 2010/03/19  03:48 PM              .piped: 2010/03/19  03:48 PM              ..piped: 2010/03/19  03:33 PM                32 m.pmpiped: 2010/03/19  03:48 PM                62 filter.pl


Try:

C:\perltest>dir | perl mytee.pl


Could it be Microsoft KB #321788?

Scripts that contain standard input (STDIN) and standard output (STDOUT) may not work correctly if you start the program from a command prompt and you use a file association to start the script.