Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I wrote it slightly differently, and it seems to work for me:
#!/usr/bin/perl use strict; use warnings; print "I'm the master ($$)\n"; my %pids; my $children = 2; for my $x (1 .. $children) { my ($fh, $pid); my $tries = 6; do {$pid = open $fh, "|-"; unless (defined $pid) { warn "fork: $!\n"; die "bailing out" unless $tries--; sleep 5; } } until defined $pid; if ($pid) { print "I'm master ($$) of child ($pid)\n"; $pids {$pid} = $fh; } else { printf "I'm a child ($$) of master (%d)\n", getppid; while (<>) { chomp; printf "Child ($$) received: [%s]\n", $_; } print "Child ($$) exiting\n"; exit; } } my $count = 0; while (my ($pid, $fh) = each %pids) { my $data = sprintf "[[%02d]]", ++$count; printf "Sending data %s to child (%d)\n", $data, $pid; print $fh $data, "\n"; } while (my ($pid, $fh) = each %pids) { printf "Closing handle to child (%d)\n", $pid; close $fh or warn "Failed to close handle for child ($pid)\n"; } print "Last line\n"; __END__ I'm the master (19054) I'm a child (19055) of master (19054) I'm master (19054) of child (19055) I'm a child (19056) of master (19054) I'm master (19054) of child (19056) Sending data [[01]] to child (19056) Sending data [[02]] to child (19055) Closing handle to child (19056) Child (19056) received: [[[01]]] Child (19056) exiting Closing handle to child (19055) Child (19055) received: [[[02]]] Child (19055) exiting Last line

In reply to Re^4: How to tell a child what to do? by Anonymous Monk
in thread How to tell a child what to do? by Eyck

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 chanting in the Monastery: (4)
    As of 2014-07-29 03:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (211 votes), past polls