$pid = open2($RDR, $WTR, 'sort'); while () { print $WTR $_; } close($WTR); # finish sending all output to sort(1) while (<$RDR>) { # now read the output of sort(1) print STDOUT "$_"; }