in reply to Perl Web Zombie problem

pg is right, and there's another thing to keep in mind: If you write too much to the PHP program, this whole process can hang.

Consider this situation. You have a big block of text in your PHP tags. You catch this code in your regexp, and send it to PHP. PHP starts reading the data you've sent it, and starts writing its output to stdout, which you're not reading yet. The OS will buffer this for a while, but when the buffer fills up, PHP's write command will block. At this point, it will cease to read, and your program's write will block. Deadlock! Here's an example of exactly this:

#!/usr/bin/perl -w use strict; use IPC::Open2; open2(\*OUT,\*IN,"cat 2>&1") or die "Couldn't run cat: $!\n"; print IN "this is a test\n" x 4096; close(IN) or die "Couln't close cat stdin: $!\n"; while (<OUT>) { print "cat: $_"; } close(OUT) or die "Couldn't close cat stdout: $!\n";

This won't be a problem if you know the amount of data you have is smaller than the size of a pipe buffer (usually at least 4K). If the data may be bigger, you'll have to do something to process both the input and output streams alternatingly. You can use select for this, or fork off a second process to handle the write and have your current process do the read:

#!/usr/bin/perl -w use strict; use IPC::Open2; open2(\*OUT,\*IN,"cat 2>&1") or die "Couldn't run cat: $!\n"; my $child2 = fork(); defined($child2) or die "fork error: $!\n"; if (!$child2) { # Child close(OUT); print IN "this is a test\n" x 4096 or die "print error: $!\n"; close(IN) or die "Couln't close cat stdin: $!\n"; exit(0); } close(IN); # Parent while (<OUT>) { print "cat: $_"; } close(OUT) or die "Couldn't close cat stdout: $!\n"; # Wait for both children wait; wait; exit(0);