Article: 2278 of comp.lang.perl
Xref: feenix.metronet.com comp.lang.perl:2278
Newsgroups: comp.lang.perl
Path: feenix.metronet.com!news.utdallas.edu!tamsun.tamu.edu!cs.utexas.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!bogus.sura.net!udel!news.intercon.com!psinntp!shellgate!shell!rjohnson
From: rjohnson@shell.com (Roy Johnson)
Subject: Re: Novice question about piping
In-Reply-To: mostow@watson.ibm.com's message of Fri, 16 Apr 1993 21:03:56 GMT
Message-ID: <RJOHNSON.93Apr20101216@conus.shell.com>
Sender: usenet@shellgate.shell.com (USENET News System)
Organization: Houston, TX
References: <C5LH6K.MGJ@watson.ibm.com>
Date: Tue, 20 Apr 1993 16:12:16 GMT
Lines: 31

mostow@watson.ibm.com (MA.Mostow;333123) writes:
   I want my Perl program to generate a number of lines of output,
   pipe them to a command, and store the standard output of that command
   in a Perl variable. Using filehandles I would seem to need:
     open(FIL, "| command |")

This works nicely.

#!/usr/local/bin/perl

pipe(RH, WH);
if (fork()) {
  #Parent
  close(WH);
  # Read desired result from RH
  while($_=<RH>) {
    print;
  }
} else {
  #Child
  close(RH);
  open(STDOUT, ">&WH");  #Make STDOUT go to RH;
  open(OUT, "|wc");
  # Write data to OUT
  print(OUT "Do the hoochy-coochy\nline 2\nline 3");
  close(OUT);
  exit 0;
}
--
Roy Johnson     Shell Development Company (but speaking for myself)
"Nothing says 'I love my country' like higher taxes." -- Ed Ipser


