[comp.lang.perl] Breaking a pipe

blarsen@spider.uio.no (Bjorn Larsen) (04/11/90)

I have a Perl script that reads from a filehandle opened
with
             open(F, "tail -f somefile |");

and at some point I want to close the filehandle and reopen it.


However, according to the manpage,

                             Also, closing a pipe will wait for
             the process executing on the pipe to complete, in
             case you want to look at the output of the pipe
             afterwards.                                         

So, naturally my script goes blissfully to sleep waiting
for the pipe to complete.

How do I break a pipe if I *don't* want to look at the output of
the pipe afterwards?


--
Bjorn Larsen                University of Oslo,  Norway
                                             bl@nuug.no
                                         +47 2 45 35 30

merlyn@iwarp.intel.com (Randal Schwartz) (04/11/90)

In article <BLARSEN.90Apr11084353@spider.uio.no>, blarsen@spider (Bjorn Larsen) writes:
| I have a Perl script that reads from a filehandle opened
| with
|              open(F, "tail -f somefile |");
| 
| and at some point I want to close the filehandle and reopen it.
| 
| However, according to the manpage,
| 
|                              Also, closing a pipe will wait for
|              the process executing on the pipe to complete, in
|              case you want to look at the output of the pipe
|              afterwards.                                         
| 
| So, naturally my script goes blissfully to sleep waiting
| for the pipe to complete.
| 
| How do I break a pipe if I *don't* want to look at the output of
| the pipe afterwards?

Opening a pipe returns the process ID (PID) of the spawned shell, so
hang on to the result of the open, as in:

	$Fpid = open(F,"tail -f somefile |");

and later when you want to make it go away, say:

	kill 9, $Fpid; close(F);

The kill makes the process die, so the close returns immediately.

eval q(eval q-eval q: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!"=/