|Perl Monk, Perl Meditation|
Re: Streaming to Handlesby bart (Canon)
|on May 06, 2004 at 03:29 UTC||Need Help??|
It's unclear to me what you're actually after, but I have a feeling you're trying to implement coroutines. It is, basically, a form of threading, where two threads run independently, and the output of one is the input to the other, and the former one is slowed down when the latter one can't follow. I'm sorry, Perl5 doesn't do coroutines. Perhaps Perl6 might, I recall having heard the term fall on the Perl6 mailing lists, when I still followed the discussion there, years ago.
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:
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.
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