Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) 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:

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

In reply to Re: Streaming to Handles by bart
in thread Streaming to Handles by crabbdean

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2014-12-22 06:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (110 votes), past polls