use IPC::Open2;

# with named file handles
$pid = open2(\*RDR, \*WTR, $cmd_with_args);
$pid = open2(\*RDR, \*WTR, $cmd, "arg1", "arg2", ...);
*****
# with object-oriented handles
use FileHandle;
my($rdr, $wtr) = (FileHandle->new, FileHandle->new);
$pid = open2($rdr, $wtr, $cmd_with_args);
*****
use IPC::Open2;
use Symbol;

$WTR = gensym();  # get a reference to a typeglob
$RDR = gensym();  # and another one

$pid = open2($RDR, $WTR, 'bc');

while (<STDIN>) {            # read commands from user
     print $WTR $_;          # write a command to bc(1)
     $line = <$RDR>;         # read the output of bc(1)
     print STDOUT "$line";   # send the output to the user
}
*****
$pid = open2($RDR, $WTR, 'sort');

while (<STDIN>) {
     print $WTR $_;
}
close($WTR);    # finish sending all output to sort(1)

while (<$RDR>) {     # now read the output of sort(1)
     print STDOUT "$_";
}
