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

Hi fellow Monks,
I am using HTML::Parser do parse html data and struggling to figure out how I would make sure that "some text" appears on the html page - i.e. I need to parse the html page and while parsing do a regex to make sure that "some text" is indeed present in the page. Here is a snippet of my code with the text subroutine.
$parser = HTML::Parser->new(api_version => 3) $parser->handler(start => \&start,'self,tagname,attr'); $parser->handler(text => \&text,'self,text'); @todo = ["", $starturl]; while(@todo) { my ($refer, $url) = @{shift @todo} next if (exists $done{$url}); $request = GET $url, [referer=>$refer]; if ($response->is_success()) { $done{$url} = 1; $parser->{base} ||= $response->base; $parser->{browser} ||= $browser; $parser->parse($response->content); $parser->eof(); } .. } sub text { my ($self, $text)= @_; print $text . "\n"; }
Now I am not sure if I can pass my $text_to_match variable to the subroutine text or do I return $text and where abouts in the code would I do it?

Replies are listed 'Best First'.
Re: Need Help on HTML::Parser
by pg (Canon) on Nov 21, 2002 at 02:06 UTC
    Yes, you can. Simply use anonymous sub:
    $parser->handler(text => sub {text(@_, $text_to_match);},'self,text');