Tristan Fiedler wrote: > > I posted the following on PerlMonks but have not found a solution. > Could someone please comment on solutions to the following error. I am > using the Library of Congress driver to look up book info based on ISBN > numbers. I am not clear on how to combine the driver pm file with the > script, etc. I have also tested several other Driver files. > > use WWW::Scraper::ISBN::Driver; > use WWW::Scraper::ISBN::LOC_Driver; > $driver = WWW::Scraper::ISBN::LOC_Driver->new(); > $driver->search("0764516965"); > $driver->verbosity(1); > my $book = $driver->book(); > print $book('title'); > print $driver->error; > > and here is the result : > > Can't use an undefined value as a symbol reference at isbn_lookup2.pl > +line 9. {the 'title' line}
Hi Tristan. The error is because the search fails and so $driver->book returns undef, which will not suffice as a symbol reference in the print statement. There are many problems here. First of all in your own code: - Always 'use strict' and 'use warnings', and declare all of your variables. It clears up 90% of coding problems without you even ahving to try. - Enable verbosity /before/ doing the search. Then it tells you why the search fails. It says: :: Error extracting data from LOC result page - Why is the module returning this error? Because the response from the LOC server has a status of '301 Moved Permanently' and the real answer is at the URL specified in the 'Location' header of this first response. Presumably the LOC site has changed since the module was written and, instead of following the new link, it tries to parse the attached vestigial HTML which just says 'The document has moved' and fails. I shouldn't really tell you how to fix this as it's reprehensible and morally detestible, but if you open the LOC_Driver.pm file and change the line that says my $ua = new LWP::UserAgent; to my $ua = new LWP::UserAgent(requests_redirectable => [qw(GET HEAD POST)]); then you have yourself an interim solution that will suffice until the author Andy Schamp gets around to it :) - The book() method returns a hash reference, so the title field must be accessed by print $book->{title}, "\n"; (note the curly brackets instead of your parentheses) Once all this has been fixed, the program looks like this: use strict; use warnings; use WWW::Scraper::ISBN::Driver; use WWW::Scraper::ISBN::LOC_Driver; my $driver = WWW::Scraper::ISBN::LOC_Driver->new; $driver->verbosity(1); $driver->search("0764516965"); my $book = $driver->book; print $book->{title}, "\n"; and outputs: Bioinformatics for dummies which is presumably what you expected? HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>