虽然循环不读取Perl

时间:2016-11-06 19:49:18

标签: perl while-loop file-handling

我试图使用Perl中的代码动态生成输出文件:

#!/usr/bin/perl 
use strict;
use Data::Dumper;
use warnings;  
use Bio::Seq;
use Bio::SeqIO; 
use Bio::DB::Fasta;
open my $OUT, "> ./temporal.gff";
           my $seq = $db->get_Seq_by_id($key);
            my $length  = $seq->length;
            my $all = $coord{$key};
            foreach my $keys (@$all[0]){
                    @sorted = sort { $a->[0] <=> $b->[0] } @$all;
                    $num = scalar @sorted;
                    my @new = &infer_gaps(\@sorted, $num, $length);
                    foreach my $ln (@new){
                            my @tmp = split /\s{2, }|\t/, $ln;
                            print $OUT "$key\tFillSequencer\tinference\t$tmp[0]\t$tmp[1]\t\.\t$tmp[2]\t\.\t$specie $key:$tmp[0]\-$tmp[1]\n";
                    }
                    #print Dumper \@new;    
            }
        }

但是,当我想使用这个生成的文件时,就像使用while循环一样,它不会读取这个文件:

open my $ABC, "<./temporal.gff" or die $!;
    my $mRNA_seq;
    while (my $l = <$ABC>){
            chomp $l;
            my @arr = split /\t|\s+/, $l;
            #my $coor = $arr[0] . " " . $arr[3] . " " . $arr[4];
            $frame = $arr[6];
            my $final_seq = $db->seq( $arr[0], $arr[3], $arr[4] );
            my $output_nucleotide = Bio::Seq->new(
                    -seq => $final_seq,
                    -id  => $mRNA_name,
                    -display_id => $mRNA_name,
                    -alphabet => 'dna',
            );
            if ($frame eq '-') {
                    $output_nucleotide = $output_nucleotide->revcom();
            }
            $outfile_coor->write_seq($output_nucleotide);
    }

    close $ABC;

你有什么想法吗?当我使用-d标志运行我的代码时,它允许我确认代码跳过最后一个while循环...我不知道FILE HANDLE变量的定义是否存在问题或者是否存在任何其他问题警告。提前谢谢!

1 个答案:

答案 0 :(得分:4)

我认为您需要在开始阅读之前关闭该文件。

或者,您可以刷新输出,以确保在开始阅读之前完全写入文件。

sub flush {
   my $h = select($_[0]); my $af=$|; $|=1; $|=$af; select($h);
}

# Flush immediately without turning auto-flush on:
flush($OUT);

有关如何使用flush的示例,请参阅此答案:

http://www.perlmonks.org/bare/?node_id=489962