Here's a simple demonstration that uses variables as the storage for output file handles. You should be able to set up this sort of HoH (or AoH?) to keep track of the distinct "output file handles" of the various child processes, and handle the resulting output data in a simple, comprehensive way.

For this example, I'm just using some random time stamps as keys for each log, but you could use anything that makes sense for your app. Again, I'd be inclined to use separate variables for stderr and stdout of each child, but maybe that's not necessary in your case.

#!/usr/bin/perl use strict; use warnings; my %logs; for ( 0 .. 2 ) { my $id = time(); open( $logs{$id}{fh}, '>', \$logs{$id}{var} ) or die "open failed +on # $_: $!\n"; sleep int(rand(3)) + 1; # (i.e. for a small but variable number o +f seconds) } printf "log-file ids are: %s\n\n", join( " ", sort keys %logs ); for ( 1 .. 12 ) { my $id = ( keys %logs )[ int(rand(3)) ]; print "Sending entry # $_ to log $id\n"; print {$logs{$id}{fh}} "this is log event # $_\n"; } print "\n"; # How many entries per log? for my $id ( sort keys %logs ) { my @entries = split( /\n/, $logs{$id}{var} ); printf "log_id %s got %d entries\n", $id, scalar @entries; } print "\n"; # Which log got entry #4? for my $id ( sort keys %logs ) { next unless ( $logs{$id}{var} =~ /4/ ); print "Here is the log for $id, containing the fourth entry:\n$log +s{$id}{var}\n"; }
(Minor update: I changed the numeric range in the second "for" loop, so that entry # 4 is also the fourth entry.)

In reply to Re^3: How to capture process and redirect STDOUT in a hash by graff
in thread How to capture process and redirect STDOUT in a hash by thanos1983

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.