in reply to Streaming to Handles
Like some of the other people have suggested, if you don't share variables between the two threads apart from this stream, I'd separate them into two programs, and pipe the output of the former into the input of the latter. The following works on Linux, but not on Windows98. I think it works pretty much like you envisioned:
This prints 100 lines like these:#!/usr/bin/perl -w if(my $pid = open STDOUT, "|-") { local $\ = "\n"; print for 1 .. 100; close STDOUT; } elsif (!defined $pid) { die "Cannot fork: $!"; } else { while(<STDIN>) { chomp; print "I got: '$_'\n"; } }
I got: '1' I got: '2' ... I got: '98' I got: '99' I got: '100'Don't forget to close STDOUT in the, eh, "parent" — the first branch, or it'll hang.
For some more info on this and related modes for open, see the docs on open, perlopentut and perlipc.
If this has to run on Windows, and the above doesn't work (it just might on NT/XP), you can use open2(), from IPC::Open2 (or open3() from IPC::Open3), and have the script launch itself. For an example script that works this way, see Win32::GUI Chatterbox client, in particular, the sub initServer(), where the programs launches a copy of itself with open2() with an equivalent command list (as for system) of ($^X, $0, $flags) — $^X is the name of the perl executable, $0 the name of the script, and $flags a special command line switch ("-s") to make the launched script behave differently — see the lines that test
.if($opt_server) {
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Streaming to Handles
by crabbdean (Pilgrim) on May 06, 2004 at 08:29 UTC |