in reply to Re: How to hook up C's socketpair() with Perl's?
in thread How to hook up C's socketpair() with Perl's?

Frodo72,

You have nearly what I need! Perhaps I've made the problem more difficult than it needs to be.

When I substitute my "filter.pl", it nearly works as needed.

Can I trouble you for advice on how to trigger the pipe to "swallow" additional strings without closing writer1 (on your original line 48). I'll need to read a series of address strings from a database and attempt substitution on each.

I test my filter.pl by firing from the comandline, after which I'm able to enter text, <cr>, and the substituted string appears. I enter <cr>, and it's ready for the next substitution. Ctl-D terminates it.

When I invoke it from callfilter, the msg line substitutes, but later lines do not. Is this behavior due to closure of the file/pipe?

Thanks in advance,

mhutch

(p.s. -- Any recommendations on where to RTFM about agnostic filters in perl?)
  • Comment on Re^2: How to hook up C's socketpair() with Perl's?

Replies are listed 'Best First'.
Re^3: How to hook up C's socketpair() with Perl's?
by polettix (Vicar) on Jul 19, 2006 at 12:17 UTC
    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.