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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kimonp
    New Member
    • Aug 2007
    • 2

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

    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($comm and, $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\nt est\n");
    fclose($stdin);

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

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

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

    $exit_code = proc_close($res ource)
    [/PHP]

    And here is the simple perl script:

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $output;
    while (<STDIN>) {
    	$output = "FOO: $_\n";
    }
    
    print STDOUT "Test\n";
    print STDOUT $output;
    
    exit 0;

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

    Grateful for any help!
  • kimonp
    New Member
    • Aug 2007
    • 2

    #2
    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?

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      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:
      [code=perl]
      #!/path/to/perl
      [/code]

      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 :)

      Comment

      Working...