[Israel.pm] Interprocess using Safe Pipes Opens

Shmuel Fomberg semuelf at 012.net.il
Thu Aug 2 14:30:15 PDT 2007

Hello There.

This version behave a little better, (all the childs get their output)
But the child print their answer only after the script end, and in
reverse order. (child 3 print first) I can't figure why.


#!/usr/bin/perl -w
use strict;
use warnings;
use Carp;
use Data::Dumper;
use English '-no_match_vars';
use FileHandle;
$| = 1;

my $no_of_childs = 3;

my @fh;
my $index = 0;
while ($index < $no_of_childs) {
	print "PID $PID\n";
	my $pid;
	$fh[$index]{'fh'} = new FileHandle;
	$pid = $fh[$index]{'fh'}->open("|-");
	if ($pid) { # parent
		$fh[$index]{'pid'} = $pid;
		print "parent pid $PID creating child " . $index++ .
	} else {    # child
		print "child pid $PID creating child " . $index++ .
		while (1) {
			# Here we will do the child work, and read input
from STDIN
			print "$PID checking for input\n";
			my $line = <STDIN>;
			last unless defined $line;
			chomp $line;
			print "$PID got something from father !!!
			sleep 1;

print Dumper \@fh;

my $fh;

sleep 5;
$fh = $fh[0]{'fh'};
print "send hi to child 1\n";
print $fh "shalom yeled 1";

sleep 2;
$fh = $fh[1]{'fh'};
print "send hi to child 2\n";
print $fh "shalom yeled 2";

sleep 2;
$fh = $fh[2]{'fh'};
print "send hi to child 3\n";
print $fh "shalom yeled 3";

More information about the Perl mailing list