Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!voder!pyramid!unify!csusac!ucdavis!sunny!poage From: poage@sunny.ucdavis.edu (Tom Poage) Newsgroups: comp.lang.perl Subject: Re: open pipe in and out Keywords: perl pipe Message-ID: <481@sunny.ucdavis.edu> Date: 11 Jul 90 16:38:14 GMT References: <250@carssdf.UUCP> Reply-To: poage@sunny.ucdavis.edu (Tom Poage) Organization: UCDMC Clinical Engineering, Sacto., CA Lines: 55 Here's some clips of code I've experimented with to perform multiple (sorta ad-hoc) database queries from another program using a single invocation of SQL. The query language is written to the SQL process and the results are read back from the same process. I thought it'd be efficient to start SQL only once since it is a BIG program with lots of internal initialization. BTW, I used a named pipe instead of piping stdin directly to SQL because of the vendor's (Un*fy) stupid implementation; this SQL doesn't know if it's getting input from a pipe or a tty, hence writes prompts, etc., to stdout along with query results--this makes for ugly output. The only way around it is to dupe SQL into thinking it's getting query input from a file (Un*fy, do you read this group?). Unfortunately, this example needs select(), so is not portable to all UNIX domains. In the end, I just used temporary files instead of pipes. Lots deleted .... $NAMED_PIPE = "/var/tmp/PIPE.$$"; if (system("/usr/etc/mknod $NAMED_PIPE p")) { die "mknod(named-pipe): $!\n"; } ... # The pipe open fails (hangs) if the following construct is used. # This happens on a Sun 3/150 (4.0.3)--anyone else? #if (!open(SQLOUT, "SQL $NAMED_PIPE |")) { die "open(SQL): $!\n"; } open(SQLOUT, "-|") || exec 'SQL', $NAMED_PIPE; if (!open(SQLSCRIPT, ">$NAMED_PIPE")) { die "open(named-pipe): $!\n"; } ... vec($rin, fileno(SQLOUT), 1) = 1; vec($win, fileno(SQLSCRIPT), 1) = 1; loop: for (;;) { $n = select($rout=$rin, $wout=$win, undef, undef); die "select error: $!\n" if ($n < 0); if ($wout eq $win) { # write some stuff to the process. } if ($rout eq $rin) { last loop unless ($_ = ); # Do something with what you've just read. } } close(SQLOUT); unlink($NAMED_PIPE); exit(0); -- Tom Poage, Clinical Engineering Universiy of California, Davis, Medical Center, Sacramento, CA poage@sunny.ucdavis.edu {...,ucbvax,uunet}!ucdavis!sunny!poage