Jeri has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I want my temp file to be overwritten every time I use it, but instead it keeps appending my data to it. How do I go about emptying it out?

forever thanks
#!/usr/bin/perl5.8.8 use strict; use warnings; tempProteinFamFileCreator(); sub tempProteinFamFileCreator { print "Enter file to process\t"; my $count = <>; #captures input from STNDIN and finds the file associa +ted with that number chomp $count; my $TEMPfa; #file handle to temp file which stores a protein family my $infile = $count."_ProFam"; #iterates through all the protein famil +y files with the count variable open (my $INFILE,"<", $infile); open ($TEMPfa,">","temp"); #opens temp file while(<$INFILE>) { #putting a single protein family into a temp file and processing i +t with two more subroutines if ($_ =~/^##UniRef90_([\w\d]+) Protein Family/) { close ($TEMPfa); MuscleHMMERsearch(); TableParser(); open ($TEMPfa,">","temp"); #opens temp file } if ($_ =~/^>UniRef90_[\w\d]+\.UniRef100_[\w\d]+/) { chomp $_; print $TEMPfa "$_\n"; } if ($_ =~/^[\w\d]+/) { print $TEMPfa $_; } if ($_ =~/^>File/) #indicates end of file { close ($TEMPfa); MuscleHMMERsearch(); TableParser(); } } close ($INFILE); #closes the infile handle } sub MuscleHMMERsearch { #checking if the temp file has data or is empty if ($_ =~/^>(UniRef90_[\w\d]+)\.UniRef100_[\w\d]+/) { # MUSCLE creating a multiple alignment file my $Fprot = "temp"; #temp file is sent to MUSCLE my $maFprot = $1."_ProFam.afa"; my $cmd = "/home/vcg/Documents/Trial/muscle -in $Fprot -out $maFpr +ot"; print $cmd. "\n"; system ($cmd); if ($?) {die "command $cmd failed\n"}; #system (rm /chongle/jeri/bin/temp.fa); # HMMER creating a profile HMM from a multiple alignment file my $hmm_Fprot = $1."_ProFam.hmm"; $cmd = "/home/vcg/Documents/Trial/hmmbuild --informat afa $hmm_Fpr +ot $maFprot"; print $cmd. "\n"; system ($cmd); if ($?) {die "command $cmd failed\n"}; #system (rm /chongle/jeri/bin/$maFprot); # Searching the sequence database with hmmsearch my $results = $1."_PFhmmResults.out"; $cmd = "/home/vcg/Documents/Trial/hmmsearch --tblout HMMtable.tbl +--cpu 1 -E .001 $hmm_Fprot uniref100.fasta"; #HMMtable is not being b +uilt #Is only one family going through and only outputting one empty +file? print $cmd. "\n"; system ($cmd); if ($?) {die "command $cmd failed\n"}; #system (rm /chongle/jeri/bin/$hmm_Fprot); } else {print "File is empty\n";} } sub TableParser { open (my $TABLE,"<","table.tbl"); open (my $OUTFILE,">","FamDATA"); print $OUTFILE "target name\tE-value\tscore\n"; while(<$TABLE>) #space delimited file { if ($_=~/^#/){next;} my @data = split(" ", $_); if ($_=~/^UniRef100/) { my $TargetName = $data[0]; my $E_value = $data[4]; my $BitScore = $data[5]; print $OUTFILE "$TargetName\t$E_value\t$BitScore\n"; } } close ($TABLE); close ($OUTFILE); }

Replies are listed 'Best First'.
Re: Appending and empty files
by zentara (Cardinal) on Sep 20, 2011 at 18:41 UTC
    See File Handles or File::Temp or use lexical filehandles.
    # instead of # my $TEMPfa; # create it in scope open (my $TEMPfa, ">", "temp"); #opens temp file
    it looks like you are running into a glitch by declaring $TEMPfa a global scalar at the start of the script. It should at least be classified a filehandle
    use FileHandle; my $TEMPfa = FileHandle->new;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks zentara, I will def fix that. Jeri
Re: Appending and empty files
by Jeri (Scribe) on Sep 20, 2011 at 18:43 UTC

    I'm going to just going to give this code a shot. Any better ideas?

    $cmd = ">temp"; system ($cmd);
    all the best!
        Yeah, just figured that out, I'm resolving the issues you previously stated now.
        I found that it doesn't work. Could you explain to me why? Thanks!