Connecting Tech Pros Worldwide Help | Site Map

proc_open on windows: can't get stdin to a perl script

Newbie
 
Join Date: Aug 2007
Posts: 2
#1: Aug 9 '07
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki.

I am trying to call a perl function from within php using proc_open.

The perl script is being executed and returns a successful exit value, and I can read the results it prints to STDOUT in PHP, but the perl script does not see STDIN from PHP. Since I want to use the perl script as a filter and I don't want to create an intermediary file, this is problematic.

Any advice? Could it be some sort of permissions thing?

Here is the PHP:

[PHP]
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'r')
);

$pipes = array();
$resource = proc_open($command, $descriptorspec, $pipes);

if (!is_resource($resource)) {
$fail = "Unable to launch process";

} else {
$stdin = $pipes[0];
$stdout = $pipes[1];
$stderr = $pipes[2];

fwrite($stdin, "this\nis\na\ntest\n");
fclose($stdin);

while (!feof($stdout)) {
$ouput .= fgets($stdout);
}

while (!feof($stderr)) {
$error .= fgets($stderr);
}

fclose($stdout);
fclose($stderr);

$exit_code = proc_close($resource)
[/PHP]

And here is the simple perl script:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $output;
  7. while (<STDIN>) {
  8.     $output = "FOO: $_\n";
  9. }
  10.  
  11. print STDOUT "Test\n";
  12. print STDOUT $output;
  13.  
  14. exit 0;

All I see from the perl script in $stdout is"Test"....

Grateful for any help!
Newbie
 
Join Date: Aug 2007
Posts: 2
#2: Aug 9 '07

re: proc_open on windows: can't get stdin to a perl script


I found a way to make it work...perhaps somebody could comment on why this makes a difference:

If I change the command I am running to call perl directly:

"perl c:\....\foo.pl"

This works! Whereas:

"c:\....\foo.pl"

Did not work. Keep in mind though, it DID run it as via perl, just did not pass it the STDIN I was sending it from PHP.

Any ideas why this makes a difference?
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#3: Aug 13 '07

re: proc_open on windows: can't get stdin to a perl script


Heya, kimonp. Welcome to TSDN!

The '.pl' extension is not enough to designate a perl script. On a *n?x system, you must also specify the interpreter using what is known as a shebang ('#!'). For example:
Expand|Select|Wrap|Line Numbers
  1. #!/path/to/perl
  2.  
I have no idea how Windows handles it, but I would imagine that explicitly calling the perl executable to interpret the file (as you have done) would be the way to go.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Reply