Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Streaming to Handles

by bart (Canon)
on May 06, 2004 at 03:29 UTC ( #350977=note: print w/ replies, xml ) Need Help??


in reply to Streaming to Handles

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:

#!/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"; } }
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.

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) {
.
HTH.


Comment on Re: Streaming to Handles
Select or Download Code
Re: Re: Streaming to Handles
by crabbdean (Pilgrim) on May 06, 2004 at 08:29 UTC
    Thanks for taking the time with that reply bart. Problem is (which I hate) the "|-" syntax isn't supported on Win32 systems. A real pain in the ar$3. I'm not sure how a similar structure/syntax is achieved in Win32. Anyone?

    Coroutines sounds VERY interesting and I'd be interested in reading more into that, although at present it sounds like its still in the experimental stage (is that true?) and I'd rather stick with a solution that is (eh hem..) "standard" perl.

    Piping is probable although implementation into a module isn't so easy to achieve. Infact, based on the responses in this discussion nah it be too difficult or impossible. Hence your solution shows me an example of piping, which I can do, but not how to implement this into my problem. In this case the context of the problem puts a spin on the ability of piping out of its normal context, hence my post to the group. In general the whole piping solution to me doesn't seem "intuitively simple" if you get my meaning.

    Thanks also for all the links at the bottom. I'm interested to have a look through some of the code examples suggested. So thanks. ++

    Dean
    The Funkster of Mirth
    Programming these days takes more than a lone avenger with a compiler. - sam
    RFC1149: A Standard for the Transmission of IP Datagrams on Avian Carriers

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://350977]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-08-23 16:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (175 votes), past polls