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

hello monks! Below I have posted my code. I am getting an error which says "Undefined subroutine &main::fields called at test.pl line 34, <$data> line 2." Can anyone help me?

#Written by Prasuna Dahal use warnings; use XML::Simple; use LWP::UserAgent; use HTTP::Request::Common; use URI::Escape; use Text::CSV; use Data::Dumper; my $i = 0; my @keystr; my @kwrds = {"inhibitors", "activity", "complex", "activator", "activi +ty", "activities", "activated", "proteins", "deficiency", "levels", " +functions", "reductions", "protease", "proteases"}; my $file = "proteinlist.csv"; my $ua = LWP::UserAgent->new; my $csv = Text::CSV->new({sep_char => ','}); #Open the result in a CSV file open (my $fh, ">", "test1.csv"); print $fh "Valid Proteins\n"; #open file containig protein_name #open(my $data1, '<', "proteinlist.csv"); #Open the file containing the PubMed IDs open(my $data, '<', "test.csv"); while (my $line = <$data>) { chomp $line; if ($csv->parse($line)) { #Skip 1st line next if ($. == 1); my @fields = $csv-fields(); #Replace (-) with (,) $fields[0] =~ tr/-/,/; my $id = $fields[0]; #Initialize http request my $args = "db=pubmed&id=$id&retmode=text&rettype=abstract"; my $req = new HTTP::Request POST => 'https://eutils.ncbi.nlm.nih.g +ov/entrez/eutils/efetch.fcgi'; $req->content_type('application/x-www-form-urlencoded'); $req->content($args); #Get response my $response = $ua->request($req); my $content = $response->content; $fields[0] =~ tr/,/-/; my @abstract = split (/[.]/, $content); for $abstract(@abstract){ @var1 = split ((/[ ]|[,]/), $abstract); foreach $var1(@var1){ open(my $data1, "<", $file); while (my $line1 = <$data1>) { chomp $line1; my @fields1 = split ",", $line1; $keystr[$i] = $fields1[0]; $i++; if ($var1=~ /\b$keystr[$i]\b/i){ foreach $kwrds(@kwrds){ if ($var1=~ /\b$kwrds\b/i){ my $valid_prot = $var1; } } print $fh "$valid_prot\n"; print "$valid_prot\n"; } } } } } } close($fh);

Replies are listed 'Best First'.
Re: Undefined Subroutine error
by haukex (Archbishop) on May 25, 2017 at 09:40 UTC

    Look at the line the error message is referring to: my @fields = $csv-fields();. The right hand side of that is parsed as "$csv minus fields()", I assume that should be $csv->fields(); (the arrow operator calling the fields method on the $csv object).

      thanks a lot. It works
Re: Undefined Subroutine error
by Corion (Patriarch) on May 25, 2017 at 09:43 UTC

    Have you looked at line 34?

    $csv-fields();

    What is that line supposed to do?

    Maybe you wanted to do something else there? Maybe you want to call the ->fields method on $csv? Maybe try

    $csv->fields();
Re: Undefined Subroutine error
by tobyink (Canon) on May 25, 2017 at 20:34 UTC

    Unrelated to your question, this line is not doing what I believe you want it to do:

    my @kwrds = {"inhibitors", "activity", "complex", "activator", "activi +ty", "activities", "activated", "proteins", "deficiency", "levels", " +functions", "reductions", "protease", "proteases"};

    Try replacing the {braces} with (parentheses).

      Further to tobyink's post: Also consider using the  qw// operator (please see Quote-Like Operators in perlop):

      my @kwrds = qw( inhibitors activity complex activator activity activities activate +d proteins deficiency levels functions reductions protease proteases );
      (Update: Use of  qw// here is really just for convenience.)


      Give a man a fish:  <%-{-{-{-<

Re: Undefined Subroutine error
by thanos1983 (Parson) on May 25, 2017 at 09:43 UTC

    Hello pdahal,

    Can you provide us a sample of your test.csv file to replicate the problem?

    Seeking for Perl wisdom...on the process of learning...not there...yet!