views:

240

answers:

2

I'm continuing to work out of an outdated bioinformatics book and I'm attempting to use the XML::Smart Module.

I suspect the module's methods have changed over the course of 6 years and I'm inexperienced with perl to troubleshoot from cpan source. The commented out code proves the ncbi.gov query functions, I'm having trouble with the 'new' method - it's not parsing the XML. What am I doing wrong? Thanks!

Update Specifically I'm running into trouble with parsing and displaying the Id array: my @Id = $results->{eSearchResult}{IdList}{Id}{'@'}; I'm running this on OSX terminal and I don't see any Ids when I run this script. I am seeing the proper Count. Thanks!

#!/usr/local/bin/perl
# use lib "/Users/fogonthedowns/myperllib";
# use LWP::Simple;
use XML::Smart;
use strict;

#Set base URL for all eutils
my $utils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
my $db = "Pubmed";
my $query ="Cancer+Prostate";
my $retmax = 10;
my $esearch = "$utils/esearch.fcgi?" . 
              "db=$db&retmax=$retmax&term=";

# my $esearch_result = get($esearch.$query);
# print "ESEARCH RESULT: $esearch_result\n";
# print "Using Query: \n$esearch$query\n";
# print "hello world\n";

my $results = XML::Smart->new($esearch.$query,"XML::Parser");
my $count = $results->{eSearchResult}{Count};
my @Id = $results->{eSearchResult}{IdList}{Id}{'@'};
my $all_Id = join("\n", @Id);

print "Count = $count\n";
print "$all_Id\n";
+1  A: 

The first thing you have done wrong is to comment out use strict, the second is to use -w instead of use warnings.

With strict turned on, perl will report:

Bareword "XML::Parser" not allowed while "strict subs" in use at tmp:test.pl line 19.

This lets us trace where the problem is occurring.

The examples in the documentation say that the second argument (the parser to use) should be quoted, and you haven't quoted it.

So we change to:

my $results = XML::Smart->new($esearch.$query,"XML::Parser");

… and it runs.

(Incidentally, the language is called "Perl", not "perl" or "PERL")

David Dorward
JZ
A: 

change:

my @Id = $results->{eSearchResult}{IdList}{Id}{'@'};

to:

my @Id = $results->{eSearchResult}{IdList}{Id}('@');

JZ
This should be edited into the question, not added as an answer.
Ether
you perl people are up tight. This was an obvious mistake I made, and it is the solution that solved the problem. Thus an answer. I'm going back to my ruby book now.
JZ