perl error: 'use of uninitialized value in concatenation (.) or string' using hash

2.3k views Asked by At

I have a tab-delimited file: abc.txt. which has data like:

Pytul_T015270   Protein of unknown function
Pytul_T015269   Protein of unknown function
Pytul_T015255   Protein of unknown function
Pytul_T015297   Protein of unknown function

I am creating a parser which takes this abc.txt and 2 other files as input and parses the files by calling different subroutines from a package: utility.pm

The subroutine to parse abc.txt is defined in my package, utility.pm goes as follows:

use strict;

sub readblast{

my $fileName = shift;
my %hash;
my %geneNameHash;

open PRED, $fileName or die "Can't open file $!\n";
while (my $line=<PRED>) {

    chomp $line;
    #print $line,"\n";
    (my $gene,my $desc) =  split /\t/, $line;

    $hash{$gene} = $desc;
}

close(PRED);

return %hash;
}

And my parser.pl script, which uses the hash is as follows:

my %blast=&utility::readblast($ARGV[2]);
for my $mRNA(keys %{ $featureHash{$scaffold}{$gene}}){
my $desc = $blast{$mRNA};
}

Here $featurehash is another hash I made from another file. And $mRNA has the key values of the file abc.txt.

But output of $desc is blank and I am getting error:

Use of uninitialized value $desc in concatenation (.) or string at parser.pl

What is wrong with my $desc = $blast{$mRNA}; And why won't it store the 2nd column of abc.txt?

1

There are 1 answers

2
Sinan Ünür On

The following guards against trailing blank lines and possible non-tab separators (by using split with a limit):

#!/usr/bin/env perl

package My::Utility;

use strict;
use warnings;

sub read_blast {
    my $fh = shift;

    my %hash;

    while (my $line = <$fh>) {
        chomp $line;
        last unless $line =~ /\S/;
        my ($key, $value) = split ' ', $line, 2;
        $hash{ $key } = $value;
    }

    return \%hash;
}

package main;

my $blast = My::Utility::read_blast(\*DATA);
while (my ($k, $v) = each %$blast) {
    print "'$k' => '$v'\n";
}

__DATA__
Pytul_T015270   Protein of unknown function
Pytul_T015269   Protein of unknown function
Pytul_T015255   Protein of unknown function
Pytul_T015297   Protein of unknown function