in reply to Issue with downloading files

Your problem is you aren't doing this

my( $headers, $contents ) = DoStuff( $cgi ); print $headers, $contents;

Each HTTP request is headers + contents, and so is each HTTP response

When you have  if( $this ){ print $that } elsif( $that }{ print $other; } ... when you're mixing it all up, when you don't separate the things your program does into ThisPage() ThatPage() OtherPage(), you get confused and forget how HTTP wants to have things, headers + contents, not headers + headers