in reply to how can i use one handle for multifple files
Lots of things you could do differently, depending on where you want to go today!
If I read your example correctly, you print a whole lot of stuff into FILE and then more of the same stuff to either FILE or TEMPFILE? This is probably not what you really are trying to do?
FileHandle is a good way of working with filehandles 8-)
Subs are a good way of organising lots of repeated lines of code
You can pass parameters to subs to make them behave differently - for example:
use strict; use FileHandle; my $outFile = "tmp.txt"; my $outFile1 = "tmpo.txt"; my $fhOut = FileHandle->new(">$outFile") or die "Cannot open file $ou +tFile: $!"; my $fhTemp = FileHandle->new(">$outFile1") or die "Cannot open file $o +utFile1: $!"; 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... }
|
|---|