use strict; use FileHandle; my $outFile = "tmp.txt"; my $outFile1 = "tmpo.txt"; my $fhOut = FileHandle->new(">$outFile") or die "Cannot open file $outFile: $!"; my $fhTemp = FileHandle->new(">$outFile1") or die "Cannot open file $outFile1: $!"; my $test = "TRUE"; saveStuff( $fhOut ); if($test =~ /\bTRUE\b/){ saveStuff( $fhOut ); }elsif($test =~ /\bFALSE\b/){ saveStuff( $fhTemp ); } $fhOut->close(); $fhTemp->close(); sub saveStuff { my $fh = shift; my ( @things ) = qw( apples pears peaches cream ); # You can use join() to add tabs between all the things you # are printing rather than printf $fh->print( join("\t", @things ) . "\n"); #100 lines of similar code here... }