my $out_fh; if ($to_file) { open($out_fh, '>', ...) or die(...); } else { $out_fh = \*STDOUT; } ... print $out_fh "blah\n"; #### if ($to_file) { open(my $out_fh, '>', ...) or die(...); select($out_fh); } ... print "blah\n";