Hi Monks,I have to write lot of data to a file but during this I check for a condition and If I satisfy that condition I have to write that specific block of code in separate file,How do I do this ?
$outFile = "tmp.txt";
$outFile1 = "tmpo.txt";
open FILE, "> $outFile" or die "Cannot open file: $!";
open TEMPFILE, "> $outFile1" or die "Cannot open file: $!";
$test = "TRUE";
printf FILE "\t%s", "connect(";
printf FILE "\t%s", "parameter(";
..
.. #100 lines of similar code here
..
if($test =~ /\bTRUE\b/){
printf FILE "\t%s", "connect(";
printf FILE "\t%s", "parameter(";
..
.. #100 lines of similar code here
..
}elsif($test =~ /\bFALSE\b/){
printf TEMPFILE "\t%s", "connect(";
printf TEMPFILE "\t%s", "parameter(";
..
.. #100 lines of same code here
..
}
So you see there is lot of repetition.Is there any way so that I just make the handle point to other file ? something like this :
$outFile = "tmp.txt";
$outFile1 = "tmpo.txt";
open FILE, "> $outFile" or die "Cannot open file: $!";
$test = "TRUE";
printf FILE "\t%s", "connect(";
..
.. #100 lines of similar code here
..
if($test =~ /\bTRUE\b/){
FILE = $outfile;
}elsif ($test =~ /\bFALSE\b/){
FILE = $outfile1;
}
printf FILE "\t%s", "connect(";
printf FILE "\t%s", "parameter(";
..
.. #100 lines of similar code here
..
}