in reply to perl logic request for an IO scenario
The program to do this already exists: join. There is a similar program written in Perl which might also suit your needs.