14/esearch.pl


#!/usr/bin/perl -w

use LWP::Simple;

my $db     = "nucleotide";
my $query  = "h1n1 \"segment 6\" influenza a virus";
my $report = "fasta";
my $retmax = "100";    # this number should be made larger to
                       # retrieve a complete set of entries

my $Base_URL = "http://www.ncbi.nlm.nih.gov/entrez/eutils";

my $esearch =
  "$Base_URL/esearch.fcgi?" . "db=$db&retmax=1&usehistory=y&term=$query";

my $esearch_result = get($esearch);

$esearch_result =~
  m|<Count>(\d+)</Count>.*<QueryKey>(\d+)</QueryKey>.*<WebEnv>(\S+)</WebEnv>|s;

my $Count    = $1;
my $QueryKey = $2;
my $WebEnv   = $3;

# uncomment these print statements in case you
# want see the values in the output
#
# print "Count $Count\n";
# print "QueryKey $QueryKey\n";
# print "WebEnv $WebEnv\n";

my $efetch =
    "$Base_URL/efetch.fcgi?"
  . "db=$db&rettype=$report&retmax=$retmax"
  . "&query_key=$QueryKey&WebEnv=$WebEnv";

my $efetch_result = get($efetch);

print "$efetch_result\n";