#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11123851 use warnings; use IO::Pipe; use IO::Handle; use IO::Select; my $pipe = IO::Pipe->new(); my $pid = fork(); die "failed to fork: $!" unless defined $pid; if ($pid) { $pipe->reader(); my $select = IO::Select->new(); $select->add($pipe); my $line = ''; while (1) { my @ready = $select->can_read(1); foreach my $h (@ready) { # my $line = <$h>; if( sysread $h, $line, 8192, length $line ) { # chomp $line; while( $line =~ s/(.*)\n// ) { print "PARENT sees <$1>\n"; } } else { close $pipe }; } } } else { $pipe->writer(); $pipe->autoflush(1); my $c = 0; while (1) { $c++; print "CHILD writing <$c>\n"; $pipe->print("$c\n") or die; sleep 1 unless $c % 5; # pause after each fifth line } }