[Perl] examples of IO::Pipe and IO:Handle

Mikhael Goikhman migo at homemail.com
Mon Oct 28 11:21:10 PST 2002


On 28 Oct 2002 11:45:55 +0200, Offer Kaye wrote:
> 
> On Monday, October 28, 2002 11:27, David Baird wrote:
> >
> > I want to use the builtin function read() to capture the input from
> > the child. I also cannot wait for the child process to terminate before
> > acting on the output, which is why I cannot use the open() function with
> > a pipe. The child process is also a Perl script, and I cannot change
> > it, like to change $| to 1.
> 
> ?
> You don't have to wait for the child process to terminate to read the output
> when using open with a pipe- as soon as the child start to output, the
> parent can read it - at least, that's the way I understood it when you do an
> implicit fork. See:
> http://cs.haifa.ac.il/~shlomo/perl/lecture6/slide19.html
> 
> The code from that page:
> #########################
> die "Can't fork: $!" unless defined($pid = open(KID, "-|"));
> if ($pid) {           # parent
> 	while (<KID>) {
> 		# do something
> 	}
> 	close KID;
> } else {
> 	exec 'myprog', 'arg1', 'arg2' or die "can't exec myprog: $!";
> }

David wants to read character by character, not line by line.
So he needs to replace <KID> with read(KID, $char, 1) here.

And of course, $| = 1 in _both_ the parent and the child is needed,
otherwise the child may write and the parent may read only full lines.

Regards,
Mikhael.



More information about the Perl mailing list