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

I m trying to parse the HTML page using the following code:
#!/usr/bin/perl use Modern::Perl; use URI::Escape; use HTML::TreeBuilder; use Try::Tiny; use LWP::Simple; use HTML::TreeBuilder::XPath; use Data::Dumper; use strict; use warnings; my $debug=1; my $base='http://www.costacrociere.it'; my $url='/it/lista_crociere/capitali_nord_europa-201207-2.html'; my $page = get($base.$url) ; my $p = HTML::TreeBuilder::XPath->new_from_content( $page ); binmode( STDOUT, ':utf8'); my @trips= $p->findnodes( '//div[@class="info-cruise"]'); foreach my $trip (@trips){ my $title = $trip->findvalue( './/div[@class="sx"]/h3'); print "Trip name: $title\n"; my $price = $trip->findvalue( './/span[@class="new-price"]'); print "price: $price\n"; my $includes = $trip->findvalue('.//p[@class="info-price"]/spa +n[6]'); #I added this line print "Includes: $includes\n"; foreach my $info ( $trip->findnodes( './/p[@class="itinerari-i +nfo"]//span[@class != "note" and @class != "strike"]')){ my $info_title= $info->findnodes( './b')->[0]; print $info_title->as_text(); $info_title->detach; my $info_value= $info->as_text; print ":", $info_value, "\n"; } my $pic = $trip->findvalue('.//img[@class="image_map"]/@src'); # I + added this line. print "Picture: $base$pic\n"; print "\n"; }
But it keeps throwing the error saying the parse() is un-initialized in the TreeBuilder.Pm file under the HTML library.I m using perl version : 5.16.3.1603 Please Help with some suggestions.
  • Comment on Use of uninitialized value in subroutine entry at c:/Perl/lib/HTML/TreeBuilder.pm
  • Download Code

Replies are listed 'Best First'.
Re: Use of uninitialized value in subroutine entry at c:/Perl/lib/HTML/TreeBuilder.pm
by Corion (Patriarch) on Oct 16, 2013 at 11:47 UTC

    To help us help you better, please tell us the exact error message. Perl error messages usually include a line number.

    Also tell us what steps you have already taken to determine the cause of the error and what the outcomes were.

Re: Use of uninitialized value in subroutine entry at c:/Perl/lib/HTML/TreeBuilder.pm
by nonsequitur (Pilgrim) on Oct 16, 2013 at 11:41 UTC

    I can't check further because the formatting of the code is so bad but adding square brackets seems to get rid of most of the problems.

    Like this my @trips = $p->findnodes('//div[@class="info-cruise"]');

    Instead of this my @trips = $p->findnodes('//div@class="info-cruise"');