( while read command param; do # whatever method files=`command param` # to gather the files for file in $files; do # process files echo $file done done) | maincode.pl #### #!/usr/bin/perl use IPC::Open2; my $cmd = 'whatever'; # really, I have no idea what you are doing $pid = open2(\*CHLD_OUT, \*CHLD_IN, $cmd) or die "oops: $!\n"; while() { chomp; my $file = $_; # now do whatever with $file. open(I,'<', $file); while (my $line = ) { ... # do whatever with each line in the file } }