{ my $i = 1; # Function static var... ooooh. sub Whatever { my $select = (defined $_[0]) ? $_[0] : 0; my $old_fh; local *FH; open FH, ">>$file" or die "Failed to open $file, $!"; if( $select ) { $old_fh = select FH }; print FH "$i This will go to $file\n"; print "$i This will go to $file if \$select($select) is true.\n"; if( $select ) { select $old_fh } close FH or die "Failed to close $file, $!"; $i++; } } Whatever(0); Whatever(1); # Now the second print goes to FH.