perly 0 Newbie Poster


I tried parsing a multi-record genbank file (from this site: using the code below.

The code returned an error:

readline() on unopened filehandle at line 62.

The code:

   #!/usr/local/bin/perl -w

    use strict;

    my $record;

    print "Please type in the name of a file\n";
    my $file = <STDIN>;
    chomp $file;

    while( $record = get_next_record($file) ) {

           my ($annotation, $seq) = get_dna ($record);
           open my $fh, '>', 'oufile.txt', or die "cant't open outfile:$!";
           print "Sequence:\n\n", $seq, "\n";

     close $fh or die "cant't open outfile:$!";


     sub get_dna {
          my ($file) = @_;
          my @annotation = ();
          my $seq = '';
          my $in_sequence = 0;
          open FILE, $file or die "Can't open $file, Perl says $!\n";
          while ( my $line = <FILE> )    {
              last if $line =~ m|^//\n|; # stop if line has just // on it
              $in_sequence = 1 if $line =~ m/^ORIGIN/;
                   if ($in_sequence) {
                       $seq .= $line;
                   } else {
                 push @annotation, $line;
           close FILE;
    # remove all spaces and digits from $seq. add \n to remove newlines
    $seq =~ s/[\s0-9]//g;
    # return @annotation array as a scalar reference and scalar $seq
    return \@annotation, $seq;

    sub get_next_record {

        my($file) = @_;
        my($record) = '';
        my($save_input_separator) = $/;
        $/ = "//\n";
        $record = <$file>;
        $/ = $save_input_separator;
        return $record;

Can someone help me fix this error please.
Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.