Path: utzoo!attcan!uunet!wuarchive!emory!hubcap!mephisto!mcnc!uvaarpa!mmdf From: martin%easby.durham.ac.uk@CUNYVM.CUNY.EDU (Martin Ward) Newsgroups: comp.lang.perl Subject: (none) Message-ID: <1990Jul17.095951.8569@uvaarpa.Virginia.EDU> Date: 17 Jul 90 09:59:51 GMT Sender: mmdf@uvaarpa.Virginia.EDU (Uvaarpa Mail System) Reply-To: martin%easby.durham.ac.uk@CUNYVM.CUNY.EDU Organization: The Internet Lines: 32 John Watson writes: | I have a perl script that needs to send a few lines to a filter | process and read a few lines back. (I have no sockets, forget that.) This was exactly the problem I had with my "file viewer" program, "t-rayc@microsoft.uucp" suggested the technique used below to pass the global string $buf through a filter passed to &filterbuf. More generally you could pass the buffer as another argument and return the filtered result. sub filterbuf # Pass $buf through the given filter. local ($filter) = @_; unless(open(GOODS, "-|")) # produces the goods # child is here, with its stdout attached to parent's GOODS # child sends output of filter to stdout to be picked up by parent # Now the child redirects its STDIN to the output of a grandchild # which prints $buf to stdout (unbuffered): unless(open(STDIN,"-|")) $| = 1; print $buf; exit 0; # Now the child can run the filter - its stdin reads the $buf chars, # and its stdout is redirected to paren't GOODS filehandle: open(STDERR, ">/dev/null"); $| = 1; exec $filter; die "Couldn't exec. ($!)"; read(GOODS, $buf, 1024); # read up to 1024 filtered chars close(GOODS); # wait for child to terminate. Martin. My ARPANET address is: martin%EASBY.DUR.AC.UK@CUNYVM.CUNY.EDU OR: martin%uk.ac.dur.easby@nfsnet-relay.ac.uk UUCP:...!mcvax!ukc!easby!martin JANET: martin@uk.ac.dur.easby BITNET: IN%"MARTIN@EASBY.DURHAM.AC.UK"