The example was clearly only a "proof of concept", you can modify it to suit your needs. In particular you can close the pipes' ends when you really need it. In my example:
else { /* In the parent */ /* close unneeded endpoints */ close(reader[1]); close(writer[0]); /* Send something to the filter */ write(writer[1], msg, strlen(msg)); close(writer[1]); /* HERE I CLOSE PARENT->FILTER */ /* Read response from the filter */ printf("filter says:\n"); while ((nread = read(reader[0], buf, 9)) > 0) { buf[nread] = '\x0'; printf("%s", buf); } if (nread < 0) { perror("read(): "); return 1; } printf("\n"); close(reader[0]); /* HERE I CLOSE FILTER->PARENT */ }
I closed the file descriptors immediately when they were not needed any more. I wanted to send only one string, then I closed the writer[1]. But you can keep it open until you're done (note: the following code is "high-level"):
else { /* In the parent */ /* close unneeded endpoints */ close(reader[1]); close(writer[0]); while (msg = next_string_to_filter()) { /* Send something to the filter */ write(writer[1], msg, strlen(msg)); /* Read response from the filter */ read_and_consume_answer(reader[0]); } /* Close communication with filter */ close(writer[1]); close(reader[0]); }
A little note is due here about read_and_consume: be very, very careful about synchronisation between the two processes. You probably want to use the filter in a line-oriented fashion (from what I see from your post): be careful that read() blocks! Otherwise, you could prepare all the input (i.e. all the lines that you have to filter), send them all to the filter at once and read the answers. But I'll cut it here before someone reminds me that we're in PerlMonks, not CMonks ;)
Any recommendations on where to RTFM about agnostic filters in perl?
With agnostic I tried to express the fact that the filter does not have to know anything more than "read from STDIN, write to STDOUT", which is what any program normally does. I fear that you aren't going to find much about "agnostic filters" in any language :)

More than this, be careful of buffering, use strict; and use warnings; ;)

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Re^3: How to hook up C's socketpair() with Perl's? by polettix
in thread How to hook up C's socketpair() with Perl's? by mhutch7714

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.