gansvv has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am trying to get 14 child processes to talk with the parent and send back data. I have 14 pipes with dynamic handles - using a hash, with hostname as the key (because I need to be able to uniquely identify which child process sent the data too). However, I am unable to read the data back in the parent. All I get are GLOB values. Code snippets are below. Any help is greatly appreciated!

foreach $host (@hosts) { pipe($fhr{$host}, $fhw{$host}); if (fork() == 0) { # child process spawned #do stuff and then write to pipe like this: close $fhr{$host}; print { $fhw{$host} } "$mystr"; close $fhw{$host}; } } while (wait() != -1) { #wait for children to finish } foreach $host (@hosts) { close $fhw{$host}; chomp ($mystring=<$fhr{$host}>); close $fhr{$host}; print "DATA from $host: $mystring"; }
I think the problem is with the chomp line. Am I reading from the pipe correctly?

Replies are listed 'Best First'.
Re: Perl IPC using dynamic handles in hash
by BrowserUk (Patriarch) on Jun 08, 2011 at 00:44 UTC

    Switch that to:

    chomp( $mystring = readline( $fhr{ $host } ) );

    In theory, you can also use curlies to disambiguate:

    chomp( $mystring = < {$fhr{$host}} > );

    but I've encountered situations when that didn't seem to work. readline seems preferable anyway.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Yes, I had tried the curlies and they did not work.

      Readline works like a charm. Thanks a ton!