Got burned with this long time ago when writing CGIs in perl.
You need to autoflush stdout. Like $|=1; .
(At least whenever calling anything else from perl, otherwise the headers may get corrupted and the server will block the output with a 500 error.)