Dynamically change STDOUT and STDERR redirections

perl

Solution

Just reopen STDOUT

open(STDOUT, '>', $qfn)

Problem

In Perl I use standard commands for printing: ``` print "text"; # STDOUT print STDOUT "text"; print STDERR "text"; ``` How can I dynamically change redirection of `STDOUT` and `STDERR` to different files? Let's say I have a simple loop with `$i` going from `$min` to `$max` and I want to dynamically change redirections to files `"out_$n"` and `"err_$n"` files where `$n = int($i/1000)`. I don't want to change my existing `print` commands, so I am looking for solution that would just add this redirection functionality without changing of existing code.

Original source