in reply to Re^4: Redirect Subroutine Output
in thread Redirect Subroutine Output

Sounds like you want ambrus's 3rd solution
# Open log file once open(MYFILE, '>>', $qfn) or die("Cannot open file $qfn for appending: $!\n"); ... print_to($to_file ? *MYFILE : *STDOUT, $str);

Better yet, use a lexical

open(my $MYFILE, '>>', $qfn) or die("Cannot open file $qfn for appending: $!\n"); ... print_to($to_file ? $MYFILE : *STDOUT, $str);

Finally, if you want to reopen the file every time you call print_to,

my $MYFILE; if ($to_file) { open($MYFILE, '>>', $qfn) or die("Cannot open file $qfn for appending: $!\n"); print_to($to_file ? $MYFILE : *STDOUT, $str); } else { $MYFILE = *STDOUT; } print_to($MYFILE, $str);