Here's a version that uses that SIGPIPE to restart a child
( with a new pipe ), but things had to be shuffled around a bit.
#!/usr/bin/perl
# http://perlmonks.org/?node_id=1174747
use strict;
use warnings;
$| = 1;
my ($from_parent, $to_child );
while(1)
{
my $msg = "hello from $$\n";
print "pipe and fork, parent $$\n";
pipe( $from_parent, $to_child ) or die "$! on pipe";
if( my $pid = fork )
{ # parent
close $from_parent;
my $more = 1;
$SIG{HUP} = sub { $pid and kill 15, $pid };
$SIG{PIPE} = sub { $more = 0 };
while($more)
{
syswrite $to_child, $msg, length $msg;
sleep 3;
}
}
elsif( defined $pid )
{ # child
close $to_child;
print "child $$ started\n";
while(1)
{
if( sysread $from_parent, my $buffer, 100 )
{
print "in child $$: $buffer";
}
}
exit;
}
else
{
die "$! on fork";
}
}