in reply to Confusion with two-way pipes

It's not clear from your question if you want to do additional processing in perl after you get the output from foo. If you get to the point of needing pipes to and from a command, I suggest you look at IPC::Run, which is reputed to be safer than IPC::Open2 or IPC::Open3. Here is some example code adapted from the IPC::Run man page:
use strict; use IPC::Run qw( start finish ) ; my @cat = qw( cat ) ; # Create pipes for you to read / write (like IPC::Open2 & 3). my $h = start \@cat, '<pipe', \*IN, '>pipe', \*OUT, '2>pipe', \*ERR or die "cat returned $?" ; print IN "some input\n" ; close IN ; print <OUT>, <ERR> ; finish $h ;