Perl IO redirection problems on Windows

Posted by kevinw on 2007-03-20 11:17
OS: Windows | Product: ActivePerl | tags: activeperl io Perl pipe redirect windows
Question: 

On Unix I can run commands like "foo.pl | bar.pl" and have the output of foo.pl be the input of bar.pl. This doesn't seem to work on Windows. How can I make this work?

Answer: 

The Windows command interpreter cmd.exe does not support IO redirection for programs started via shell associations, like those created for .pl files during the ActivePerl installation. It only works for .bat, .com, .cmd, and .exe files.

You need to write:

perl foo.pl | perl bar.pl

Or if the files are not in your current directory but are on the PATH:

perl -S foo.pl | perl -S bar.pl

Alternatively, you can wrap your .pl files into .bat scripts using pl2bat:

pl2bat foo.pl
pl2bat bar.pl

Then run them as:

a | b

This is also the case for > and <.

Arctific | Wed, 2007-10-31 08:37

In UNIX I can run the following

$result = system("$sql_cmd > $output_file") ;
if(!open(SQL, " < $output_file ")) {
print "Error: output file not opened\n" ;
exit 1 ;
}
#
### Process file should happen here... but does not.
#

Why does this not work in Windows?
A) File exists.
B) Even a for loop trying to make successive open's does not work.
C) Files quoted well, all backslashes accounted for.

What can I do to make this $output_file visible to windows?

Best Wishes,

Don Turnblade
MS, CISSP

San | Mon, 2008-08-18 03:18

Probably windows command prompt don't like white space in file names unless you encase them in quotes, but talking to your database via command prompt is not very secure anyway. Can you not get the output from within your script using DBI?