Hi, others (notably jesuashok) have already pointed out the pipe solution in case of colocated processes (which should be the case given the fork/exec approach). I'd only suggest to use dup or dup2 in the child process on the pipe handles, just before calling exec, to redirect the two pipes on STDIN and STDOUT. This would allow you to write an "agnostic" filter, i.e. a filter that reads from STDIN and writes on STDOUT - something that you could use in a canned sequence of pipes on the command line :)

Just to make me clear, here's the C part:

/* gcc callfilter.c -o callfilter */ #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <string.h> int main (int argc, char *argv[]) { int writer[2]; /* From the parent's point of view */ int reader[2]; /* From the parent's point of view */ int pid; char *msg = "hullo"; char buf[10]; int nread; /* Get pipes on */ if (pipe(reader)) { perror("reader pipe(): "); return 1; } if (pipe(writer)) { perror("writer pipe(): "); return 1; } pid = fork(); if (pid < 0) { perror("fork(): "); return 1; } else if (pid == 0) { /* In the child */ /* close unneeded endpoints */ close(reader[0]); close(writer[1]); /* Duplicate handles on STDIN and STDOUT for child */ dup2(writer[0], 0); dup2(reader[1], 1); /* call filter */ execl("./filter.pl", "./filter.pl", NULL); /* patched, thanks to + djp */ perror("execl(): "); return 1; } 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]); /* 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]); } return 0; }
and here's the Perl filter.pl (to be located in the same directory with the correct permissions, see execl above):
#!/usr/bin/perl use strict; use warnings; local $/; my $in = <STDIN>; print {*STDOUT} scalar reverse $in;
The result:
poletti@PolettiX:~/sviluppo/perl/dup$ ./callfilter filter says: olluh

Update: patched C example, thanks to djp.

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

Don't fool yourself.

In reply to Re: 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.