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

In reply to Re: Perl Web Zombie problem by sgifford
in thread Perl Web Zombie problem by bivansc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.