Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!mcsun!hp4nl!fwi.uva.nl!casper From: casper@fwi.uva.nl (Casper H.S. Dik) Newsgroups: comp.lang.perl Subject: Re: Double-ended pipes Keywords: pipe Message-ID: <1991Jun14.111448.25093@fwi.uva.nl> Date: 14 Jun 91 11:14:48 GMT References: <1991Jun10.180301.7925@NCoast.ORG> <14054@pasteur.Berkeley.EDU> Sender: news@fwi.uva.nl Organization: FWI, University of Amsterdam Lines: 65 Nntp-Posting-Host: ophelia.fwi.uva.nl lars@yosemite.berkeley.edu (Lars E. Thon) writes: >One feature that I have been missing from perl is having a double-ended >pipe. This could be useful f.ex. if you want to feed a lot of commands >to a csh and then analyze output and/or status from the commands. For a >simple (and contrived) example, consider the following: >#! /usr/local/gnu/bin/perl >doublepipe_open(CSH_IN, "| /bin/csh -f |", CSH_OUT); >#Silly way of checking whether user ~blah exists >print CSH_IN "echo ~blah"; >$pathname= ; >print CSH_IN "echo \$status"; >$errcode= ; >if ($errcode) { > print "User blah does not exist ...\n"; >} ># END >I haven't seen a way to do this in perl, except for the obvious (but >unsatisfactory) method of using a temp file to store the csh output. >Any ideas about this? >-- You can easily roll your own: sub double_pipe { local($in,$cmd,$out) = @_; # Create an in and an out pipe. Make them unbuffered. pipe($out,WTMP); pipe(RTMP,$in); select($in); $| = 1; select($out); $| = 1; select(STDOUT); unless (fork) { # close the unused sides in the child. close($in); close($out); open(STDIN,"<&RTMP"); open(STDOUT,">&WTMP"); # Join stdin and stdout. open(STDERR,">&WTMP"); close(RTMP); close(WTMP); exec $cmd; die "Can't execute $cmd\n"; } # close the unused sides in the parent. close(WTMP); close(RTMP); } &double_pipe(SH_IN,"sh",SH_OUT); print SH_IN "ls -l\n"; # NOTE: trailing \n, and no |'s. close(SH_IN); while () { print $_ ; } #Casper -- | Casper H.S. Dik RELIGION KILLS | casper@fwi.uva.nl