perl - While 循环不读取 Perl 代码生成的文件

标签 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 变量的定义是否存在问题或任何其他不会生成的问题警告。提前致谢!

最佳答案

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

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

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

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

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

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

关于perl - While 循环不读取 Perl 代码生成的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40453782/

相关文章:

ruby - Ruby 中的 While 循环和转换为函数

以读写模式打开文件时c++文件流问题

c - 修改c中文件的现有内容

c - C 中的文件处理 : can't fathom the output

csv - Perl Text::CSV_XS 当注释是最后一行时将其视为数据

相当于 perl hmac_sha1_hex 的 python

JavaScript - 最大公约数 - 陷入无限循环

perl - 枚举 perl 解释器中的所有引用

mysql - Perl - mysql select * 仅返回数据库中的第一个字段

java - While 循环与 hasNext();没有完全循环