open FH, "> filename.txt" or die "Cannot open filename.txt: $!"; my $curr_fh = select (FH); # do your stuff; select $curr_fh; # now you can print!