in reply to why?

Here's code adapted from the Cookbook that may do what you want:
use IO::Handle; pipe(READER, WRITER); WRITER->autoflush(1); if ($pid = fork) { # if fork returned a PID, this must be the parent close READER; # so the child can use it print WRITER "Your message here, call 555-8467, extension $$!"; close WRITER; waitpid($pid, 0); # let the kid die } else { # if $pid is empty, this must be the child close WRITER; # so the parent can use it my $line = <READER>; print "What does this mean?\n\t>>$line<<\n"; close READER; # don't have to be explicit exit; }
The close() is important because fork tends to duplicate open filehandles between the parent and child. We take advantage of some of this, though.

I don't know how well it will work passing variables instead of explicit filehandle names to the pipe call. You might have better results with:

local *$read; local *$write;

Replies are listed 'Best First'.
RE: Re: why?
by Anonymous Monk on May 05, 2000 at 07:34 UTC
    Well,I think you didn't know my meaning. I don't suspect fork() and pipe() can run well on activeperl (package 613). My trouble is parent can not have two children at the same time and one of the children are waiting for a filehandle! Thanks.
      Oh, I see what you mean now.

      Forking works fine on ActivePerl 613 on NT, at least for me. The trouble is with the program flow. The first time through, you connect the parent's filehandles together. Fine. Then you fork off a child, let it keep the reader (and the parent the writer) and you send a message from parent to child. Child displays it, receives the EOF, and then exits. Perl closes the reader implicitly. The second time through, the parent uses pipe again, making another child. It then iterates through a hash of children, trying to send them messages.

      Unfortunately, the other end of the pipe connected to the first child is gone. That'll cause some trouble.

      Either close the writer at the end of the loop, or keep your children alive.