our $O; sub foo { print $O "blah\n"; } $O = $to_file ? *MYFILE : *STDOUT; foo(); # OR sub foo { print O "blah\n"; } *O = $to_file ? *MYFILE{IO} : *STDOUT{IO}; # (the {IO} is not strictly needed but this way we avoid overwriting $O etc) foo(); # or same as above but like open O, ">&=", ($to_file ? *MYFILE : *STDOUT); # or you could even use *MYFILE instead of *O # OR sub foo { my($P) = @_; pri("blah\n"); } sub pri { if ($to_file) { print MYFILE @_ } else { print @_ } }