Path: utzoo!utgpu!watserv1!watmath!att!rutgers!tut.cis.ohio-state.edu!ucbvax!iwarp.intel.com!news
From: merlyn@iwarp.intel.com (Randal Schwartz)
Newsgroups: comp.lang.perl
Subject: Re: problem redirecting STDOUT to pipe
Message-ID: <1990Jul16.180207.28646@iwarp.intel.com>
Date: 16 Jul 90 18:02:07 GMT
References: <1990Jul16.161925.9869@uvaarpa.Virginia.EDU>
Sender: news@iwarp.intel.com
Reply-To: merlyn@iwarp.intel.com (Randal Schwartz)
Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA
Lines: 53
In-Reply-To: worley@compass.com (Dale Worley)
In article <1990Jul16.161925.9869@uvaarpa.Virginia.EDU>, worley@compass (Dale Worley) writes:
| While we're at it, what's the method for creating a pipe pair in Perl?
| I can't find anything in the manual that does not also cause forking.
| I'm sure this has been discussed before, but I don't remember the
| answer and now I'm in the throes of a "redirect both STDIN and STDOUT
| to a subprocess" program...
Here's the hack I produced when this subject came up last time.
You have to watch your synchronization carefully, or use select():
==================================================
#!/local/usr/bin/perl
$| = 1;
sub make_child {
local($read,$write,@cmd) = @_;
# shouldn't need eval in next two lines...
eval "pipe(CHILD_READ,$write)" || die "no pipe: $!";
eval "pipe($read,CHILD_WRITE)" || die "no pipe: $!";
local($pid) = fork;
unless ($pid) { # child:
close($read);
close($write);
open(STDIN,"<&CHILD_READ") || die "cannot dup: $!";
open(STDOUT,">&CHILD_WRITE") || die "cannot dup: $!";
exec @cmd;
die "cannot exec @cmd: $!";
} else { # parent:
die "no fork: $!" unless defined $pid;
close(CHILD_READ);
close(CHILD_WRITE);
local($os) = select($write); $| = 1; select($os);
}
}
&make_child("BR","BW","/bin/sh");
print BW "date\n";
$x =
; print $x;
close(BW);
print
;
==================================================
And yes, Larry, look at those lines marked "shouldn't need eval". Why
do I need eval? I thought a scalar was always a good replacement for
a filehandle...
print "Just another Perl hacker,"
--
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/