在perl中获取请求并使用未初始化的值

时间:2018-12-24 19:05:12

标签: perl

my $url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=journal+of+medical+virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]&usehistory=y";
  print "\n before url \n";
  print $url;
  #post the esearch URL
  my $output = get($url);
  print $output;

我以前从未使用过perl。

如果我在浏览器中单击此URL,则可以获取XML。 但是,根据我在脚本输出中看到的,$output为空,并且

print $output;

返回

Use of uninitialized value in print at ./extractEmails.pl line 48.

请提出问题和解决方法

编辑:

根据建议,完成代码:

#!/usr/bin/perl -w
# A perlscript written by Joseph Hughes, University of Glasgow
# use this perl script to parse the email addressed from the affiliations in PubMed

use strict;
use LWP::Simple;

my ($query,@queries);
#Query the Journal of Virology from 2014 until the present (use 3000)
$query = 'journal+of+virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Journal of General Virology
$query = 'journal+of+general+virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Virology
$query = 'virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Archives of Virology
$query = 'archives+of+virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Virus Research
$query = 'virus+research[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Antiviral Research
$query = 'antiviral+research[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Viruses
$query = 'viruses[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';
push(@queries,$query);
#Journal of Medical Virology
$query = 'journal+of+medical+virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]';

# global variables
push(@queries,$query);
my %emails;
my $emailcnt=0;
my $count=1;
#assemble the esearch URL
foreach my $query (@queries){
  my $base = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/';
  #my $url = $base . "esearch.fcgi?db=pubmed&term=$query&usehistory=y";
  my $url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=journal+of+medical+virology[journal]+AND+2014[Date+-+Publication]:3000[Date+-+Publication]&usehistory=y";
  print "\n before url \n";
  print $url;
  #post the esearch URL
  my $output = get($url);
  print "\n before output \n";
  print get($url);
  print $output;
  #parse WebEnv, QueryKey and Count (# records retrieved)
  my $web = $1 if ($output =~ /<WebEnv>(\S+)<\/WebEnv>/);
  my $key = $1 if ($output =~ /<QueryKey>(\d+)<\/QueryKey>/);
  my $count = $1 if ($output =~ /<Count>(\d+)<\/Count>/);

  #retrieve data in batches of 500
  my $retmax = 500;
  for (my $retstart = 0; $retstart < $count; $retstart += $retmax) {
    my $efetch_url = $base ."efetch.fcgi?db=pubmed&WebEnv=$web";
    $efetch_url .= "&query_key=$key&retmode=xml";
    my $efetch_out = get($efetch_url);
    my @matches = $efetch_out =~ m(<Affiliation>(.*)</Affiliation>)g;
    #print "$_\n" for @matches;
    for my $match (@matches){
      if ($match=~/\s([a-zA-Z0-9\.\_\-]+\@[a-zA-Z0-9\.\_\-]+)$/){
        my $email=$1;
        $email=~s/\.$//;
        $emails{$email}++;
      }     
    }
  }
  my $cnt= keys %emails;
  print "$query\n$cnt\n";
}

print "Total number of emails: ";
my $cnt= keys %emails;
print "$cnt\n";
my @email = keys %emails;
my @VAR;
push @VAR, [ splice @email, 0, 100 ] while @email;

my $batch=100;
foreach my $VAR (@VAR){
    open(OUT, ">Set_$batch\.txt") || die "Can't open file!\n";
    print OUT join(",",@$VAR);
    close OUT;
    $batch=$batch+100;
}    

1 个答案:

答案 0 :(得分:4)

出于任何原因,我建议不要使用LWP :: Simple,因为无法对其进行配置或有效地处理错误。无论如何,使用包装的LWP::UserAgent几乎一样简单(尽管错误处理有些复杂)。以下示例将替换use LWP::Simple;my $output = get($url);行。

use strict;
use warnings;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(timeout => 30);
my $response = $ua->get($url);
unless ($response->is_success) {
  # the Client-Warning, Client-Aborted, and X-Died headers each may be set on client/transport errors
  die $response->status_line;
}
my $output = $response->decoded_content;

HTTP::Tiny的核心也很简单。

use strict;
use warnings;
use HTTP::Tiny;
my $ua = HTTP::Tiny->new;
my $response = $ua->get($url);
unless ($response->{success}) {
  die $response->{status} == 599 ? $response->{content} : "$response->{status} $response->{reason}";
}
my $output = $response->{content};

如果您真的希望使用LWP :: Simple方法至少会报告传输错误,请尝试从Mojolicious的ojo

perl -Mojo -E'say g(shift)->text' http://example.com

在脚本而不是oneliner中,您可以直接使用Mojo::UserAgent,还可以处理上述HTTP错误:

use strict;
use warnings;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $response = $ua->get($url)->result;
unless ($response->is_success) {
  die $response->code . ' ' . $response->message;
}
my $output = $response->text;