Just to make me clear, here's the C part:
and here's the Perl filter.pl (to be located in the same directory with the correct permissions, see execl above):/* 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; }
The result:#!/usr/bin/perl use strict; use warnings; local $/; my $in = <STDIN>; print {*STDOUT} scalar reverse $in;
poletti@PolettiX:~/sviluppo/perl/dup$ ./callfilter filter says: olluh
Update: patched C example, thanks to djp.
Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |