#!/bin/perl

# Save STDIN and STDOUT for later restoration to filenos 0 and 1
# --------------------------------------------------------------
open(SI, ">&STDIN");
open(SO, ">&STDOUT");

# Unbuffer output on SO, the saved STDOUT
# ---------------------------------------
select SO; $| = 1;

# Create a pipe on 0 and 1 for inheritance across fork
# ----------------------------------------------------
pipe(STDIN, STDOUT);   # STDIN/STDOUT are read/write

if (fork == 0) {
   exec("c") || die SO,"exec failed: $!\n";
}

close(STDOUT);      # Close write side of pipe
open(STDOUT, ">&SO");  # Put stdout back
select STDOUT; $| = 1;

open(R, "<&STDIN");  # Dup stdin (read side) into R
close(STDIN);
$r = <R>;
print "Parent read: $r\n"; 
open(STDIN, "<&SI");
print $r, " to true stdout!\n";
print "Goodbye\n";
