This is the effect of buffering. When outputting to the terminal, Perl (like stdio) uses string-based buffering, which will give the expected effect. When outputting to a file, it uses block buffering (usually 4k or 8k blocks), so it will only hide at the end of the program.
The difference you observe is that STDERR is unbuffered, unlike most other pens.
The easiest way to get around this problem is to enable autorun in STDOUT using the special variable $| . See perlvar for more details.
source share